• shiro授权


    目录

    1.shiro授权角色、权限

    2.Shiro的注解式开发


    1.shiro授权角色、权限

     在ShiroUserMapper.xml中新增内容

    1. <select id="selectRoleIdsByUserName" resultType="java.lang.String" parameterType="java.lang.Integer">
    2. select r.roleid from t_shiro_user u,t_shiro_user_role ur,t_shiro_role r
    3. where u.userid = ur.userid and ur.roleid = r.roleid
    4. and u.userid = #{userid}
    5. select>
    6. <select id="selectPerIdsByUserName" resultType="java.lang.String" parameterType="java.lang.Integer">
    7. select p.permission from t_shiro_user u,t_shiro_user_role ur,t_shiro_role_permission rp,t_shiro_permission p
    8. where u.userid = ur.userid and ur.roleid = rp.roleid and rp.perid = p.perid
    9. and u.userid = #{userid}
    10. select>

    userMapper.java

    1. public Set selectRoleIdsByUserName(Integer userId);
    2. public Set selectPerIdsByUserName(Integer userId);

    userbizimpl.java

    1. package com.hmj.ssm.Biz.impl;
    2. import com.hmj.ssm.Biz.UserBiz;
    3. import com.hmj.ssm.mapper.UserMapper;
    4. import com.hmj.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 小何吖
    10. * @create 2022-08-25 18:30
    11. */
    12. @Service("userBiz")
    13. public class UserBizImpl implements UserBiz {
    14. @Autowired
    15. private UserMapper userMapper;
    16. @Override
    17. public Set selectRoleIdsByUserName(String userName) {
    18. return userMapper.selectRoleIdsByUserName(userName);
    19. }
    20. @Override
    21. public Set selectPerIdsByUserName(String userName) {
    22. return userMapper.selectPerIdsByUserName(userName);
    23. }
    24. }

    MyRealm.java

    1. package com.hmj.ssm.shiro;
    2. import com.hmj.ssm.Biz.UserBiz;
    3. import com.hmj.ssm.model.User;
    4. import org.apache.shiro.authc.AuthenticationException;
    5. import org.apache.shiro.authc.AuthenticationInfo;
    6. import org.apache.shiro.authc.AuthenticationToken;
    7. import org.apache.shiro.authc.SimpleAuthenticationInfo;
    8. import org.apache.shiro.authz.AuthorizationInfo;
    9. import org.apache.shiro.authz.SimpleAuthorizationInfo;
    10. import org.apache.shiro.realm.AuthorizingRealm;
    11. import org.apache.shiro.subject.PrincipalCollection;
    12. import org.apache.shiro.util.ByteSource;
    13. import java.util.Set;
    14. /**
    15. * @author 小何吖
    16. * @create 2022-08-25 18:33
    17. */
    18. public class MyRealm extends AuthorizingRealm {
    19. public UserBiz userBiz;
    20. public UserBiz getUserBiz() {
    21. return userBiz;
    22. }
    23. public void setUserBiz(UserBiz userBiz) {
    24. this.userBiz = userBiz;
    25. }
    26. /**
    27. * 授权
    28. * @param principalCollection
    29. * @return
    30. * shiro-web.ini
    31. */
    32. @Override
    33. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    34. System.out.println("用户授权...");
    35. String username = principals.getPrimaryPrincipal().toString();
    36. ShiroUser user = shiroUserService.queryByName(username);
    37. Set roles = shiroUserService.getRolesByUserId(user.getUserid());
    38. Set pers = shiroUserService.getPersByUserId(user.getUserid());
    39. // SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    40. // info.addRoles(roles);
    41. // info.addStringPermissions(pers);
    42. SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
    43. info.setRoles(roles);
    44. info.setStringPermissions(pers);
    45. return info;
    46. }
    47. }

    2.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层

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

    Springmvc.xml

    1. "org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
    2. depends-on="lifecycleBeanPostProcessor">
    3. "proxyTargetClass" value="true">
    4. "org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    5. "securityManager" ref="securityManager"/>
    6. "exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    7. "exceptionMappings">
    8. "org.apache.shiro.authz.UnauthorizedException">
    9. unauthorized
    10. "defaultErrorView" value="unauthorized"/>

  • 相关阅读:
    Java项目:ssm实验室预约维修管理系统
    高并发场景下的分布式锁优化
    RV1126 DSI 调试
    elasticsearch索引的数据类型以及别名的使用
    3分钟裁员1000+人!IBM中国研发部确认关闭,提供N+3赔偿
    2023_Spark_实验六:Scala面向对象部分演示(二)(IDEA开发)
    统计数(C++)
    vivo 帐号服务稳定性建设之路-平台产品系列06
    为dev c++配置图形开发环境easyx之mingw32
    记录--一道字节面试题引出的this指向问题
  • 原文地址:https://blog.csdn.net/hmjcxy/article/details/126579999