• shiro授权


    目录

    一、shiro授权角色、权限

    二、注解式开发


    一、shiro授权角色、权限

                    UserMapper.xml

    1. <select id="queryByroleid" resultType="java.lang.String" parameterType="java.lang.String">
    2. select roleid FROM t_shiro_user us,t_shiro_user_role usr
    3. where us.userid = usr.userid and us.username = #{userName}
    4. select>
    5. <select id="queryByperid" resultType="java.lang.String" parameterType="java.lang.String">
    6. select rp.perid from t_shiro_user u,t_shiro_user_role ur,t_shiro_role_permission rp
    7. where rp.roleid = ur.roleid and u.userid = ur.userid and u.username = #{userName}
    8. select>

            UserMapper

    1. Set queryByroleid(@Param("userName") String userName);
    2. Set queryByperid(@Param("userName") String userName);

          重写  MyRealm 中的 AuthorizationInfo 方法

    1. @Override
    2. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    3. String userName = principals.getPrimaryPrincipal().toString();
    4. Set roleid = userBiz.queryByroleid(userName);
    5. Set perid = userBiz.queryByperid(userName);
    6. SimpleAuthorizationInfo info =new SimpleAuthorizationInfo();
    7. info.setStringPermissions(perid);
    8. info.setRoles(roleid);
    9. return info;
    10. }

    二、注解式开发

    常用注解介绍

     @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

    Springmvc.xml

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

    ShiroController

    1. package com.zsx.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. /**
    9. * @author zsx
    10. * @site 155954····
    11. * @company 交换余生
    12. * @create 2022--08--26 20:25
    13. */
    14. @RequestMapping("/shiro")
    15. @Controller
    16. public class ShiroController {
    17. //RequiresUser 代表当前方法只有登录后才能够访问
    18. //RequiresUser 等价于 Spring-shiro 中的/user/updatePwd.jsp=authc配置
    19. @RequiresUser
    20. @RequestMapping("/passUser")
    21. public String passUser(){
    22. System.out.println("身份认证通过");
    23. return "admin/addUser";
    24. }
    25. //RequiresRoles 当前方法只有具备指定的角色才能访问
    26. //RequiresRoles 等价于 Spring-shiro.xml 中的/admin/*.jsp=roles[4]配置
    27. @RequiresRoles(value = {"1","4"},logical = Logical.AND)
    28. @RequestMapping("/passRole")
    29. public String passRole(){
    30. System.out.println("角色认证通过");
    31. return "admin/addUser";
    32. }
    33. //RequiresPermissions 当前方法只有具备指定的权限才能访问
    34. //RequiresPermissions 等价于 Spring-shiro.xml 中的/user/teacher.jsp=perms[2]配置
    35. @RequiresPermissions(value = {"2"},logical = Logical.AND)
    36. @RequestMapping("/passPermission")
    37. public String passPermission(){
    38. System.out.println("权限认证通过");
    39. return "admin/addUser";
    40. }
    41. }

  • 相关阅读:
    java开发之个微机器人的二次开发
    Java中遍历HashSet集合有哪些方法呢?
    计算机毕设 SpringBoot+Vue校园网课管理系统 网上选课系统 疫情网课管理系统Java Vue MySQL数据库 远程调试 代码讲解
    《PyTorch深度学习实践》第十三讲RNN进阶
    Another app is currently holding the yum lock; waiting for it to exit...
    代码随想录算法训练营第五十五天 | 300.最长递增子序列、674. 最长连续递增序列、718. 最长重复子数组
    一文让你学会 Java 中的内部类
    Mysql InnoDB Buffer Pool
    贪心算法-均分纸牌-JAVA
    ElasticSearch7.3学习(二)----内部基于_version乐观锁控制机制
  • 原文地址:https://blog.csdn.net/weixin_61523879/article/details/126550647