• thymeleaf-extras-shiro(根据当前用户的权限显示对应的标签) 与 shiro的加盐加密


    1.thymeleaf-extras-shiro

    根据当前用户的权限显示对应的标签

    1.导入依赖

    
    
    
    com.github.theborakompanioni
    thymeleaf-extras-shiro
    2.0.0
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.在ShiroConfiguration 配置

    /* 配置shiro thymeleaf 标签
    * */
    @Bean
    public ShiroDialect shiroDialect() {
    return new ShiroDialect();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3. 导入shiro标签

    
    
    
    
    Title
    
    
    

    index

    • 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

    2.shiro 加密加盐

    1.对密码进行加密,加盐

    public static void main(String[] args) {
    int hashIterations = 1000;//加密的次数
    Object salt = "root";//盐值
    Object credentials = "123456";//密码
    String hashAlgorithmName = "MD5";//加密方式
    Object simpleHash = new SimpleHash(hashAlgorithmName, credentials,
    salt, hashIterations);
    System.out.println("加密后的值----->" + simpleHash);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.配置UserRealm 加密

    @Bean
    public UserRealm getUserRealm(){
    UserRealm userRealm = new UserRealm();
    // 配置md5 加密
    userRealm.setCredentialsMatcher(getHashedCredentialsMatcher());
    return userRealm;
    }
    // MD5 加密
    @Bean
    public HashedCredentialsMatcher getHashedCredentialsMatcher(){
    HashedCredentialsMatcher hashedCredentialsMatcher = new
    HashedCredentialsMatcher();
    hashedCredentialsMatcher.setHashAlgorithmName("MD5");
    hashedCredentialsMatcher.setHashIterations(1000);
    return hashedCredentialsMatcher;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3.在自定义认证 realm中配置加盐

    // 认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken
    token) throws AuthenticationException {
    System.out.println("RealmForDouble认证中---->用
    户:"+token.getPrincipal());
    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    String password="123456";// 假设这是从数据库中查询到的用户密码
    // 创建一个SimpleAuthenticationInfo,第一个参数是用户名,第二个是验证密码,第三个
    是当前realm的className
    // 验证密码会与用户提交的密码进行比对
    // SimpleAuthenticationInfo info = new
    SimpleAuthenticationInfo(upToken.getUsername(),password,this.getName());
    // 加入盐
    ByteSource byteSource = ByteSource.Util.bytes(upToken.getUsername());
    password = "72708d3c290ccf2aa362ba305633eaba"; // "12345 经过 1000 次 MD5
    并加盐// "
    SimpleAuthenticationInfo info = new
    SimpleAuthenticationInfo(upToken.getUsername(),password,byteSource,this.getName(
    ));
    return info;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3 在shiro中增加 eache缓存

    1.配置依赖

    
    
    
    org.springframework.boot
    spring-boot-starter-cache
    
    
    
    net.sf.ehcache
    ehcache
    
    
    
    org.apache.shiro
    shiro-ehcache
    1.4.0
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2.在resouces下添加 ehcache.xml

    
    
    
    
    
    
    
    
    
    
    
    • 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

    3.在shiro配置文件配置缓存

    /**
    * 添加 缓存管理器
    * @return
    */
    @Bean
    public EhCacheManager ehCacheManager(){
    EhCacheManager cacheManager = new EhCacheManager();
    cacheManager.setCacheManagerConfigFile("classpath:ehcache.xml");
    return cacheManager;
    }
    @Bean
    public UserRealm getUserRealm(){
    UserRealm userRealm = new UserRealm();
    userRealm.setCredentialsMatcher(getHashedCredentialsMatcher());
    // 获取each
    userRealm.setCacheManager(ehCacheManager());
    userRealm.setCachingEnabled(true);
    //启用身份验证缓存,即缓存AuthenticationInfo信息,默认false
    userRealm.setAuthenticationCachingEnabled(true);
    //缓存AuthenticationInfo信息的缓存名称 在ehcache-shiro.xml中有对应缓存的配置
    userRealm.setAuthenticationCacheName("authenticationCache");
    //启用授权缓存,即缓存AuthorizationInfo信息,默认false
    userRealm.setAuthorizationCachingEnabled(true);
    //缓存AuthorizationInfo信息的缓存名称 在ehcache-shiro.xml中有对应缓存的配置
    userRealm.setAuthorizationCacheName("authorizationCache");
    return userRealm;
    }
    @Bean
    public SecurityManager getSecurityManager(UserRealm userRealm){
    //SecurityManager 为接口 创建一个实现类
    DefaultWebSecurityManager securityManager = new
    DefaultWebSecurityManager();
    //设置 自定义的Realm
    securityManager.setRealm(userRealm);
    // 配置ehcache缓存 userRealm.setCacheManager(ehCacheManager()); 配置这里
    就不需要了
    //securityManager.setCacheManager(ehCacheManager());
    return securityManager;
    }
    
    • 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
  • 相关阅读:
    WinUI(WASDK)使用ChatGPT和摄像头手势识别结合TTS让机器人更智能
    i7 12800hx和r9 5900hx 选哪个好
    java毕业设计在线家教预约系统Mybatis+系统+数据库+调试部署
    期末前端web大作业——动漫客栈响应式bootstarp(7页) 排版整洁,内容丰富,主题鲜明
    06.特殊CSS伪选择器
    第21章 自旋锁实验(iTOP-RK3568开发板驱动开发指南 )
    麒麟系统开发笔记(八):在国产麒麟系统上使用linuxdeployqt发布qt程序
    离子液体[C2MIm]SbF6/cas:305370-81-4/1-乙基-3-甲基咪唑六氟锑酸盐的密度值
    CH58X/CH57X/V208 Observer(观察者)例程讨论讲解
    详解Java代理
  • 原文地址:https://blog.csdn.net/m0_67265464/article/details/126516715