• shiro授权


    目录

    一、shiro授权角色、权限

    1、Mapper层、servlet层

    二、shiro的 授权的方法 

    运行:

     二、Shiro的注解式开发

    1、常用注解介绍

    2、测试

    controller层


    一、shiro授权角色、权限

    1、Mapper层、servlet层

    UserMapper.xml中新增方法

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

    UserMapper.java

    1. package com.zking.ssm.mapper;
    2. import com.zking.ssm.model.User;
    3. import org.apache.ibatis.annotations.Param;
    4. import org.springframework.stereotype.Repository;
    5. import java.util.Set;
    6. @Repository
    7. public interface UserMapper {
    8. int deleteByPrimaryKey(Integer userid);
    9. int insert(User record);
    10. int insertSelective(User record);
    11. User selectByPrimaryKey(Integer userid);
    12. //通过用户名查询用户信息
    13. User queryByName(@Param("userName") String userName);
    14. //通过用户名查询对应的角色
    15. Set selectRoleIdsByUserName(@Param("userName") String userName);
    16. //通过用户名查询对应的权限
    17. Set selectPerIdsByUserName(@Param("userName") String userName);
    18. int updateByPrimaryKeySelective(User record);
    19. int updateByPrimaryKey(User record);
    20. }

    UserBiz.java

    1. package com.zking.ssm.biz;
    2. import com.zking.ssm.model.User;
    3. import org.apache.ibatis.annotations.Param;
    4. import java.util.Set;
    5. public interface UserBiz {
    6. int deleteByPrimaryKey(Integer userid);
    7. int insert(User record);
    8. int insertSelective(User record);
    9. User selectByPrimaryKey(Integer userid);
    10. User queryByName(String userName);
    11. int updateByPrimaryKeySelective(User record);
    12. int updateByPrimaryKey(User record);
    13. //通过用户名查询对应的角色
    14. Set selectRoleIdsByUserName(String userName);
    15. //通过用户名查询对应的权限
    16. Set selectPerIdsByUserName(String userName);
    17. }

    UserBizImpl.java

    1. package com.zking.ssm.impl;
    2. import com.zking.ssm.biz.UserBiz;
    3. import com.zking.ssm.mapper.UserMapper;
    4. import com.zking.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. * @site 3185579318
    11. * @company xxx公司
    12. * @create  2022-08-26 12:54
    13. */
    14. @Service("userBiz")
    15. public class UserBizImpl implements UserBiz {
    16. @Autowired
    17. private UserMapper userMapper;
    18. @Override
    19. public int deleteByPrimaryKey(Integer userid) {
    20. return userMapper.deleteByPrimaryKey(userid);
    21. }
    22. @Override
    23. public int insert(User record) {
    24. return userMapper.insert(record);
    25. }
    26. @Override
    27. public int insertSelective(User record) {
    28. return userMapper.insertSelective(record);
    29. }
    30. @Override
    31. public User selectByPrimaryKey(Integer userid) {
    32. return userMapper.selectByPrimaryKey(userid);
    33. }
    34. @Override
    35. public User queryByName(String userName) {
    36. return userMapper.queryByName(userName);
    37. }
    38. @Override
    39. public int updateByPrimaryKeySelective(User record) {
    40. return userMapper.updateByPrimaryKeySelective(record);
    41. }
    42. @Override
    43. public int updateByPrimaryKey(User record) {
    44. return userMapper.updateByPrimaryKey(record);
    45. }
    46. @Override
    47. public Set selectRoleIdsByUserName(String userName) {
    48. return userMapper.selectRoleIdsByUserName(userName);
    49. }
    50. @Override
    51. public Set selectPerIdsByUserName(String userName) {
    52. return userMapper.selectPerIdsByUserName(userName);
    53. }
    54. }

    二、shiro的 授权的方法 

    1. package com.zking.ssm.shiro;
    2. import com.zking.ssm.biz.UserBiz;
    3. import com.zking.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.AuthenticatingRealm;
    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 白未
    17. * @site 3185579318
    18. * @company xxx公司
    19. * @create  2022-08-26 13:00
    20. */
    21. public class MyRealm extends AuthorizingRealm {
    22. public 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 principalCollection
    32. * @return
    33. */
    34. @Override
    35. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    36. String userName = principalCollection.getPrimaryPrincipal().toString();//获取当前用户名
    37. Set roleIds = userBiz.selectRoleIdsByUserName(userName);
    38. Set perIds = userBiz.selectPerIdsByUserName(userName);
    39. SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
    40. // 将当前登录的 权限 交给 shiro的授权器
    41. info.setStringPermissions(perIds);
    42. // 将当前登录的 角色 交给 shiro的授权器
    43. info.setRoles(roleIds);
    44. return info;
    45. }
    46. /**
    47. * 认证
    48. * @param authenticationToken
    49. * @return
    50. * @throws AuthenticationException
    51. */
    52. @Override
    53. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
    54. String userName = authenticationToken.getPrincipal().toString();
    55. User user = userBiz.queryByName(userName);
    56. AuthenticationInfo info = new SimpleAuthenticationInfo(
    57. user.getUsername(),
    58. user.getPassword(),
    59. ByteSource.Util.bytes(user.getSalt()),
    60. this.getName()//realm的名字
    61. );
    62. return info;
    63. }
    64. }

    角色与权限的结果要与applicationContext-shiro.xml的配置保持一致 

    角色:4
    权限:2

    运行:

    登录zs

     点击用户新增,没有权限

     zdm登录点击用户新增

     zs登录点击老师简介

    ls登录点击老师简介

     

     二、Shiro的注解式开发

    1、常用注解介绍

     @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

    2、测试

     在springmvc-servlet.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>

    controller层

    Shirocontroller.java

    1. package com.zking.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. * @site 3185579318
    11. * @company xxx公司
    12. * @create  2022-08-29 10:11
    13. */
    14. @RequestMapping("/shiro")
    15. @Controller
    16. public class ShiroController {
    17. //RequiresUser代表当前方法只有登录后才能访问
    18. //RequiresUser等价于Spring-shiro.xml中的/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("/passPermissions")
    37. public String passPermissions(){
    38. System.out.println("权限认认证通过");
    39. return "admin/addUser";
    40. }
    41. }

     OR代表只要满足value中的一个条件即可

  • 相关阅读:
    儿童智能口算数学宝/数学计算机开发方案:VK0256B LQFP64 LCD液晶段码屏驱动芯片工程技术支持
    【收藏系列】多线程的代码案例(单例模式 + 阻塞队列 + 定时器 + 线程池)
    让.NET 8 支持 Windows Vista RTM
    文字处理控件TX Text Control迎来2022年第一版重大更新x30版本,一起来看看都有哪些特色功能吧
    Java:Java 反射API简介
    css:详解BFC块级格式化上下文
    2022-9-20-C++11新特性
    新手学习:ArcGIS对shp文件裁剪
    Quartz核心原理之架构及基本元素介绍
    利用python连接linux虚机并执行命令
  • 原文地址:https://blog.csdn.net/yzq102873/article/details/126578159