• SpringBoot-34-shiro整合thymeleaf


    9.10 shiro整合thymeleaf
    • 导入依赖
    
    
        com.github.theborakompanioni
        thymeleaf-extras-shiro
        2.1.0
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 项目运行中其他依赖
    
    
        org.apache.shiro
        shiro-spring-boot-web-starter
        1.9.1
    
    
    
        org.springframework.boot
        spring-boot-starter-thymeleaf
    
    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
    
        org.springframework.boot
        spring-boot-devtools
        runtime
        true
    
    
        org.projectlombok
        lombok
        true
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    
        org.apache.logging.log4j
        log4j-core
        2.17.2
    
    
    
        org.springframework.boot
        spring-boot-starter-data-jdbc
        2.7.2
    
    
        org.springframework.boot
        spring-boot-starter-jdbc
        2.7.2
    
    
    
        mysql
        mysql-connector-java
        8.0.29
    
    
    
        com.alibaba
        druid
        1.2.11
    
    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.2.2
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 接下来需要在ShiroConfiguration配置类中整合thymeleaf
    @Configuration
    public class ShiroConfiguration {
        //ShiroDialect   thymeleaf-extras-shiro包下的Shiro方言,导入整合包后还需要进行注册
        @Bean
        public ShiroDialect getShiroDialect(){
            return new ShiroDialect();
        }
        
        //第三步:ShiroFilterFactoryBean
        @Bean
        public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager")DefaultWebSecurityManager securityManager){
            ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
            //设置安全管理器
            shiroFilterFactoryBean.setSecurityManager(securityManager);
    
            /*添shiro的内置过滤器,类型
                anon:无需认证就可以访河
                authc: 必须认证了才能让河
                user: 必颈拥有记住我功能才能用
                perms: 拥有对某个资源的权限才能访问:
                role: 拥有某个角色权限才能访河
            */
            Map filterChainDefinitionMap = new LinkedHashMap<>();
            /*也可以用通配符
            * filterChainDefinitionMap.put("/user/*","authc");*/
            //拦截简单请求
            filterChainDefinitionMap.put("/user/addUser","authc");
            filterChainDefinitionMap.put("/user/updUser","authc");
            //设置请求的用户主体的访问权限,然后在realm给用户授予访问权限
            filterChainDefinitionMap.put("/user/addUser","perms[user:add]");
            filterChainDefinitionMap.put("/user/updUser","perms[user:upd]");
            shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
            //无认证权限时,设置登陆请求
            shiroFilterFactoryBean.setLoginUrl("/user/toLogin");
            //未授权时返回页面信息
            shiroFilterFactoryBean.setUnauthorizedUrl("/user/unauthorized");
            return shiroFilterFactoryBean;
        }
    
        //第二步:DefaultWebSecurityManager
        @Bean(name = "defaultWebSecurityManager")
        public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm")UserRealm userRealm){
            DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
            //关联UserRealm
            securityManager.setRealm(userRealm);
            return securityManager;
        }
    
        //第一步:创建Realm对象,需要自定义类
        @Bean
        public UserRealm userRealm(){
            UserRealm userRealm = new UserRealm();
            userRealm.setCredentialsMatcher(new Md5CredentialsMatcher());
            return userRealm;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 自定义UserRealm,进行授权认证
    package com.example.config;
    
    import com.example.mapper.UserMapper;
    import com.example.pojo.User;
    import org.apache.shiro.SecurityUtils;
    import org.apache.shiro.authc.*;
    import org.apache.shiro.authz.AuthorizationInfo;
    import org.apache.shiro.authz.SimpleAuthorizationInfo;
    import org.apache.shiro.realm.AuthorizingRealm;
    import org.apache.shiro.subject.PrincipalCollection;
    import org.apache.shiro.subject.Subject;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.util.StringUtils;
    
    import javax.jws.soap.SOAPBinding;
    import java.util.HashMap;
    import java.util.Map;
    
    //自定义UserRealm extends AuthorizingRealm
    public class UserRealm extends AuthorizingRealm {
        @Autowired
        UserMapper userMapper;  //查询用户权限
        //访问页面属于授权
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
            System.out.println("执行了授权doGetAuthorizationInfo方法!");
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            //硬编码,给所有登录用户授予"user:add"权限
            //info.addStringPermission("user:add");
            //拿到当前用户
            Subject subject = SecurityUtils.getSubject();
            //1.从认证这块传递的用户信息,在授权这个用这个方法得到用户,通过当前用户Subject的属性传值
            //2.也可以通过session传值
            User currentUser= (User)subject.getPrincipal();
            //设置用户访问授权
            info.addStringPermission(currentUser.getPerm());
            return info;
        }
    
        //用户登录属于认证
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
            System.out.println("执行了认证doGetAuthenticationInfo方法!");
            //连接真实数据库
            UsernamePasswordToken userToken = (UsernamePasswordToken) token;
            Map map = new HashMap(15);
    
            /*(String)userToken.getPrincipal() == userToken.getUsername()*/
            map.put("name",userToken.getUsername());
            User user = userMapper.getUser(map);
    
            if (user==null) {
                return null; //抛出UnknownAccountException异常
            }
            /*可以加密:
            MD5加密:e10adc3949ba59abbe56e057f20f883e
            MD5盐值加密(密码混合账户): e10adc3949ba59abbe56e057f20f883eusername*/
            //密码认证,shiro做,加密了,默认是简单验证new SimpleCredentialsMatcher()明文验证
            return new SimpleAuthenticationInfo(user,user.getPassword(),"");
            //传值方式
            //1.从认证这块传递的用户信息,在授权这个用这个方法得到用户,通过当前用户Subject的属性传值
            //2.也可以通过session传值
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 修改首页,拥有某权限的用户显示对应的菜单

    用shiro:hasPermission="user:add"标签来判断

    • 注意命名空间的导入

    xmlns:shiro=“http://www.pollix.at/thymeleaf/shiro”

    
    
    
        
        Title
    
    
    

    首页

    信息显示

    登陆 添加|修改|退出
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 这时候可以在首页添加一个登陆按钮,未登录或未认证的用户显示,登陆后或认证的用户不显示

    • 方式有很多,一种是在登陆时用当前用户的session中放值。

    th:if=“${session.user == null}”

    @RequestMapping("/login")
    public String login(String username,String password,Model model){
        Subject subject = SecurityUtils.getSubject();
        //全局token
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            token.setRememberMe(true);
            subject.login(token);
            Session session = subject.getSession();
            session.setAttribute("user",token.getPrincipal());
            return "index";
        } catch (UnknownAccountException uae) {
            model.addAttribute("msg",token.getPrincipal()+" is not correct");
            return "login";
        } catch (IncorrectCredentialsException ice) {
            model.addAttribute("msg","Password for account " + token.getPrincipal() + " was incorrect!");
            return "login";
        } catch (LockedAccountException lae) {
            model.addAttribute("msg","The account for username " + token.getPrincipal() + " is locked.  " +
                    "Please contact your administrator to unlock it.");
            return "login";
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 此时便可以访问http://127.0.0.1:8080/user/进行测试了,会发现不同用户登录后显示的菜单不同,游客身份访问首页有登陆按钮。

    在这里插入图片描述

    • 访问http://127.0.0.1:8080/user/toLogin,用admin/123456登陆显示user:add权限的页面链接

    在这里插入图片描述

    • 用Tom/123456登陆显示user:upd权限的页面链接

    在这里插入图片描述

    • 另一种是用shiro的标签元素

    shiro:guest shiro:notAuthenticated

    • 补充:常见的Shiro 权限标签

    guest标签
      shiro:guest
      用户没有身份验证时显示相应信息,即游客访问信息。

    user标签
      shiro:user  
      
      用户已经身份验证/记住我登录后显示相应的信息。

    authenticated标签
      shiro:authenticated  
      
      用户已经身份验证通过,即Subject.login登录成功,不是记住我登录的。

    notAuthenticated标签
      shiro:notAuthenticated
      
      
      用户已经身份验证未通过,即没有调用Subject.login进行登录,包括记住我自动登录的也属于未进行身份验证。

    principal标签
      
      
      
      相当于((User)Subject.getPrincipals()).getUsername()。

    lacksPermission标签
      
     
      

      如果当前Subject没有权限将显示body体内容。

    hasRole标签
        
      

      如果当前Subject有角色将显示body体内容。

    hasAnyRoles标签
      
       
      

      如果当前Subject有任意一个角色(或的关系)将显示body体内容。

    lacksRole标签
        
      

      如果当前Subject没有角色将显示body体内容。

    hasPermission标签
        
      

      如果当前Subject有权限将显示body体内容

    下一篇:SpringBoot-35-任务
  • 相关阅读:
    【小沐学QT】QT学习之Web控件的使用
    DiffuSEEG:一种基于stable diffusion 的SEEG数据补全方法
    【中国知名企业高管团队】系列60:长虹电器
    es查询初学
    日志巡检内容
    一千多的投影仪能用吗?哈趣、小明性价比超高的卧室投影仪
    【JAVA高级】——Druid连接池和Apache的DBUtils使用
    Spark Core
    java使用拦截器进行接口签名验证
    C# 结构体介绍
  • 原文地址:https://blog.csdn.net/weixin_42045639/article/details/126688460