• Shiro授权以及注解开发


    目录

    一,shiro授权角色,权限

    二,shiro的注解式开发


    一,shiro授权角色,权限

    分析:

    1.分析用户具备哪些角色

    2.分析用户具备什么权限

    1.sql语句编写

    1. --角色:用户具备哪些角色
    2. select roleid FROM t_shiro_user u,t_shiro_user_role ur where u.userid = ur.userid and u.username = 'zdm'
    3. --权限:用户具备的权限
    4. SELECT rp.perid from t_shiro_user u,t_shiro_user_role ur,t_shiro_role_permission rp
    5. where u.userid = ur.userid and ur.roleid = rp.roleid and u.username = 'ls'

     

    2.自动生成xml文件配置

    1. <select id="selectgetPersByUserName" resultType="java.lang.String" parameterType="java.lang.Integer">
    2. SELECT rp.perid from t_shiro_user u,t_shiro_user_role ur,t_shiro_role_permission rp
    3. where u.userid = ur.userid and ur.roleid = rp.roleid and u.username = #{userName}
    4. select>

    Service层编写

    1. package com.dengxiyan.ssm.Biz;
    2. import com.dengxiyan.ssm.model.User;
    3. import org.apache.ibatis.annotations.Param;
    4. import java.util.Set;
    5. public interface UserBiz {
    6. Set<String> selectRolesByUserName (String userName);
    7. Set<String> selectgetPersByUserName(String userName);
    8. }

    实现类

    1. package com.dengxiyan.ssm.Biz.impl;
    2. import com.dengxiyan.ssm.Biz.UserBiz;
    3. import com.dengxiyan.ssm.mapper.UserMapper;
    4. import com.dengxiyan.ssm.model.User;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. import java.util.Set;
    8. /**
    9. * @author dxy
    10. * @site www.javadxy.com
    11. * @company ds公司
    12. * @create  2022-08-25 13:49
    13. */
    14. @Service("userBiz")
    15. public class UserBizImpl implements UserBiz {
    16. @Override
    17. public Set<String> selectRolesByUserName(String userName) {
    18. return userMapper.selectRolesByUserName(userName);
    19. }
    20. @Override
    21. public Set<String> selectgetPersByUserName(String userName) {
    22. return userMapper.selectgetPersByUserName(userName);
    23. }
    24. }

    Reaml

    注意:角色与权限的结果要与spring-shiro.xml中的配置保持一致

    1. package com.dengxiyan.ssm.shiro;
    2. import com.dengxiyan.ssm.Biz.UserBiz;
    3. import com.dengxiyan.ssm.mapper.UserMapper;
    4. import com.dengxiyan.ssm.model.User;
    5. import org.apache.shiro.authc.AuthenticationException;
    6. import org.apache.shiro.authc.AuthenticationInfo;
    7. import org.apache.shiro.authc.AuthenticationToken;
    8. import org.apache.shiro.authc.SimpleAuthenticationInfo;
    9. import org.apache.shiro.authz.AuthorizationInfo;
    10. import org.apache.shiro.authz.SimpleAuthorizationInfo;
    11. import org.apache.shiro.realm.AuthorizingRealm;
    12. import org.apache.shiro.subject.PrincipalCollection;
    13. import org.apache.shiro.util.ByteSource;
    14. import java.util.Set;
    15. /**
    16. * @author dxy
    17. * @site www.javadxy.com
    18. * @company ds公司
    19. * @create  2022-08-25 13:56
    20. */
    21. public class MyRealm extends AuthorizingRealm {
    22. private UserBiz userBiz;
    23. public UserBiz getUserBiz() {
    24. return userBiz;
    25. }
    26. public void setUserBiz(UserBiz userBiz) {
    27. this.userBiz = userBiz;
    28. }
    29. /**
    30. * 授权
    31. * @param principals
    32. * @return
    33. */
    34. @Override
    35. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    36. System.out.println("用户授权~~~");
    37. String username = principals.getPrimaryPrincipal().toString();//获取用户名
    38. Set<String> roles = userBiz.selectRolesByUserName(username);
    39. Set<String> pers = userBiz.selectgetPersByUserName(username);
    40. SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    41. //将当前登入的 权限 交给 shiro的授权器
    42. info.setStringPermissions(pers);
    43. //将当前登录的 角色 交给 shiro授权器
    44. info.setRoles(roles);
    45. return info;
    46. }
    47. }

    二,shiro的注解式开发

    常用注解介绍:

              @RequiresAuthenthentication:表示当前Subject已经通过login进行身份验证;即 Subject.isAuthenticated()返回 true
             @RequiresUser:表示当前Subject已经身份验证或者通过记住我登录的 ------(身份认证)
              @RequiresGuest:表示当前Subject没有身份验证或者通过记住我登录过,即是游客身份
              @RequiresRoles(value = {"admin","user"},logical = Logical.AND):表示当前Subject需要角色admin和user       --------(角色认证)
              @RequiresPermissions(value = {"user:delete","user:b"},logical = Logical.OR):表示当前Subject需要权限user:delete或者user:b       --------(权限配置)

    使用

    Controller层

    对应的注解添加到指定需要权限控制的方法上

    1. package com.dengxiyan.ssm.controller;
    2. import org.apache.shiro.authz.annotation.Logical;
    3. import org.apache.shiro.authz.annotation.RequiresPermissions;
    4. import org.apache.shiro.authz.annotation.RequiresRoles;
    5. import org.apache.shiro.authz.annotation.RequiresUser;
    6. import org.springframework.stereotype.Controller;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. import javax.servlet.http.HttpServletRequest;
    9. /**
    10. * @author dxy
    11. * @site www.javadxy.com
    12. * @company ds公司
    13. * @create  2022-08-26 18:58
    14. */
    15. @Controller
    16. public class ShiroController {
    17. //表示登入后才可以访问 等价于Spring-shiro.xml中的user/updatePwd.jsp=配置
    18. @RequiresUser
    19. @RequestMapping("/passUser")
    20. public String passUser(HttpServletRequest request){
    21. return "admin/addUser";
    22. }
    23. // @RequiresRoles 当前方法只有 具备指定的角色才能访问
    24. // 相当于spring-shiro.admin/*.jsp=roles[4]配置
    25. @RequiresRoles(value = {"1","4"},logical = Logical.AND)
    26. @RequestMapping("/passRole")
    27. public String passRoles(HttpServletRequest request){
    28. System.out.println("角色验证通过");
    29. return "admin/addUser";
    30. }
    31. // @RequiresPermissions 当前方法只有具备指定的权限 才能够访问
    32. // 等价于Spring-shiro.xml中的user/teacher.jsp=perms配置
    33. // @RequiresPermissions(value = {"user:update","user:view"},logical = Logical.OR)
    34. @RequiresPermissions(value = {"2"},logical = Logical.AND)
    35. @RequestMapping("/passPer")
    36. public String passPer(HttpServletRequest request){
    37. System.out.println("权限通过验证");
    38. return "admin/addUser";
    39. }
    40. @RequestMapping("/unauthorized")
    41. public String unauthorized(){
    42. System.out.println("权限验证通过");
    43. return "admin/addUser";
    44. }
    45. }

     Spring-mvc.xml配置

    添加拦截器相关配置

    1. class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
    2. depends-on="lifecycleBeanPostProcessor">
    3. <property name="proxyTargetClass" value="true">property>
    4. <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    5. <property name="securityManager" ref="securityManager"/>
    6. bean>
    7. <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    8. <property name="exceptionMappings">
    9. <props>
    10. <prop key="org.apache.shiro.authz.UnauthorizedException">
    11. unauthorized
    12. prop>
    13. props>
    14. property>
    15. <property name="defaultErrorView" value="unauthorized"/>
    16. bean>

    jsp界面(测试)

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%@taglib prefix="r" uri="http://shiro.apache.org/tags" %>
    3. <html>
    4. <head>
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <h1>主界面<%=System.currentTimeMillis()%>,欢迎您:[${sessionScope.username}]h1>
    9. <ul>
    10. shiro注解
    11. <li>
    12. <a href="${pageContext.request.contextPath}/passUser">用户认证a>
    13. li>
    14. <li>
    15. <a href="${pageContext.request.contextPath}/passRole">角色a>
    16. li>
    17. <li>
    18. <a href="${pageContext.request.contextPath}/passPer">权限认证a>
    19. li>
    20. ul>
    21. body>
    22. html>

    效果图展示

    标记处:由于每个用户的权限不同,有的用户有该权限,有的用户没有 

     

     

     

  • 相关阅读:
    【2022集创赛】安谋科技杯三等奖:基于ARM处理器的无线SoC设计
    让页面滚动到指定位置
    DAMA-DMBOK2重点知识整理CDGA/CDGP——第16章 数据管理组织与角色期望
    学习中涌现的面试问题
    How To Improve Your LISTENING SKILLS( 如何提升你的听力技能) 学习
    JavaWeb(一)
    Eclipse 主网即将上线迎空投预期,Zepoch 节点或成受益者?
    【.net core】【sqlsugar】条件查询时使用Contains的注意问题
    wpf添加Halcon的窗口控件报错:下列控件已成功添加到工具箱中,但未在活动设计器中启用
    域渗透基础_域渗透实战下gpo策略利用
  • 原文地址:https://blog.csdn.net/weixin_66202611/article/details/126549371