• shiro授权-SSM


    目录

    一、授权

    1.1 配置SQL语句 UserMapper.xml

    1.2

    UserMapper 

     UserBiz 

     UserBizImpl

     MyRealm

     applicationContext-shiro.xml

     二、shiro注解式开发

     2.1 Springmvc.xml

    2.2 ShiroController 


    一、授权

    1.1 配置SQL语句 UserMapper.xml

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

    1.2

    UserMapper 

    1. package com.cdl.ssm.mapper;
    2. import com.cdl.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 queryUserByUserName(@Param("userName") String userName);
    14. //通过用户名拿到角色ID
    15. Set selectRoleIdsByUserName(@Param("userName") String userName);
    16. //通过用户名拿到权限ID
    17. Set selectPerIdsByUserName(@Param("userName") String userName);
    18. int updateByPrimaryKeySelective(User record);
    19. int updateByPrimaryKey(User record);
    20. }

     UserBiz 

    1. package com.cdl.ssm.biz;
    2. import com.cdl.ssm.model.User;
    3. import java.util.Set;
    4. /**
    5. * @author cdl
    6. * @site www.cdl.com
    7. * @create 2022-08-25 18:48
    8. */
    9. public interface UserBiz {
    10. int deleteByPrimaryKey(Integer userid);
    11. int insert(User record);
    12. int insertSelective(User record);
    13. User selectByPrimaryKey(Integer userid);
    14. /*通过账号插询*/
    15. User queryUserByUserName( String userName);
    16. //通过用户名拿到角色ID
    17. Set selectRoleIdsByUserName(String userName);
    18. //通过用户名拿到权限ID
    19. Set selectPerIdsByUserName(String userName);
    20. int updateByPrimaryKeySelective(User record);
    21. int updateByPrimaryKey(User record);
    22. }

     UserBizImpl

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

     MyRealm

    1. package com.cdl.ssm.shiro;
    2. import com.cdl.ssm.biz.UserBiz;
    3. import com.cdl.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 cdl
    16. * @site www.cdl.com
    17. * @create 2022-08-25 19:08
    18. */
    19. public class MyRealm extends AuthorizingRealm {
    20. public UserBiz userBiz;
    21. public UserBiz getUserBiz() {
    22. return userBiz;
    23. }
    24. public void setUserBiz(UserBiz userBiz) {
    25. this.userBiz = userBiz;
    26. }
    27. @Override
    28. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    29. System.out.println("用户授权...");
    30. String userName = principals.getPrimaryPrincipal().toString();//获取账户名
    31. Set roleIds = userBiz.selectRoleIdsByUserName(userName);
    32. Set perIds = userBiz.selectPerIdsByUserName(userName);
    33. SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    34. //将当前登录的权限 交给shiro的授权器
    35. info.setStringPermissions(perIds);
    36. //将当前角色的权限 交给shiro的授权器
    37. info.setRoles(roleIds);
    38. return info;
    39. }
    40. @Override
    41. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    42. String userName = token.getPrincipal().toString();
    43. User user = userBiz.queryUserByUserName(userName);
    44. AuthenticationInfo info = new SimpleAuthenticationInfo(
    45. user.getUsername(),
    46. user.getPassword(),
    47. ByteSource.Util.bytes(user.getSalt()),
    48. this.getName()//realm的名字
    49. );
    50. return info;
    51. }
    52. }

     applicationContext-shiro.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    5. <bean id="shiroRealm" class="com.cdl.ssm.shiro.MyRealm">
    6. <property name="userBiz" ref="userBiz" />
    7. <property name="credentialsMatcher">
    8. <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
    9. <property name="hashAlgorithmName" value="md5"/>
    10. <property name="hashIterations" value="1024"/>
    11. <property name="storedCredentialsHexEncoded" value="true"/>
    12. bean>
    13. property>
    14. bean>
    15. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    16. <property name="realm" ref="shiroRealm" />
    17. bean>
    18. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    19. <property name="securityManager" ref="securityManager" />
    20. <property name="loginUrl" value="/login"/>
    21. <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
    22. <property name="filterChainDefinitions">
    23. <value>
    24. /user/login=anon
    25. /user/updatePwd.jsp=authc
    26. /admin/*.jsp=roles[4]
    27. /user/teacher.jsp=perms[2]
    28. value>
    29. property>
    30. bean>
    31. <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
    32. beans>

    运行登录界面 输入zs的正确密码和账号之后

     

     输入zdm的 有用户新增的权限

     二、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

     2.1 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>

    2.2 ShiroController 

    1. package com.cdl.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 cdl
    11. * @site www.cdl.com
    12. * @create 2022-08-26 19:54
    13. */
    14. @RequestMapping("/shiro")
    15. @Controller
    16. public class ShiroController {
    17. @RequiresUser//代表只有通过登录后才能通过
    18. @RequestMapping("/passUser")
    19. public String passUser(HttpServletRequest request){
    20. System.out.println("身份认证通过、、、");
    21. return "admin/addUser";
    22. }
    23. @RequiresRoles(value = {"1","4"},logical = Logical.AND)
    24. @RequestMapping("/passRole")
    25. public String passRole(HttpServletRequest request){
    26. System.out.println("角色认证通过、、、");
    27. return "admin/listUser";
    28. }
    29. @RequiresPermissions(value = {"user:update","user:view"},logical = Logical.OR)
    30. @RequestMapping("/passPer")
    31. public String passPer(HttpServletRequest request){
    32. return "admin/resetPwd";
    33. }
    34. @RequestMapping("/unauthorized")
    35. public String unauthorized(){
    36. return "unauthorized";
    37. }
    38. }

    输入zs的账号和密码

     

     

  • 相关阅读:
    跨链桥真的不能碰?一文详解跨链桥的分类以及过去、现在与未来
    Neo4j 基本语法
    k8s笔记21--prometheus 监控 nginx ingress
    【python基础】文件和异常详解:使用、读取、写入、追加、保存用户的信息,以及优雅的处理异常
    夏天快乐的源泉
    C#对象二进制序列化优化:位域技术实现极限压缩
    网络工程师进阶课:华为HCIP认证课程介绍
    【JUC系列】Fork/Join框架之概览
    leetcode 216. 组合总和 III
    npm更新包时This operation requires a one-time password.
  • 原文地址:https://blog.csdn.net/weixin_62735525/article/details/126548276