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

1.thymeleaf-extras-shiro

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

1.导入依赖

<!--thymeleaf shiro 根据权限标签显示-->
<!--shiro与themeleaf集成-->
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>

2.在ShiroConfiguration 配置

/* 配置shiro thymeleaf 标签
* */
@Bean
public ShiroDialect shiroDialect() {
return new ShiroDialect();
}

3. 导入shiro标签

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>index</h3>
<div>
<a href="/user/add"> 增加用户</a>
<a href="/user/logout"> 注销</a>
<br>
<a href="/goods/add">增加商品</a>
<a href="/goods/delete">删除商品</a>
<a href="/goods/update">更新商品</a>
<a href="/goods/get">获取单个商品</a>
<a href="/goods/getAllGoods"> 获取所有商品</a>
</div>
<div>
<a shiro:hasRole="root" href="/user/add"> 增加用户</a>
<a href="/user/logout"> 注销</a>
<br>
<a shiro:hasPermission="goods:add" href="/goods/add">增加商品</a>
<a shiro:hasPermission="goods:delete" href="/goods/delete">删除商品</a>
<a shiro:hasPermission="goods:update" href="/goods/update">更新商品</a>
<!-- shiro 标签的权限 与 url 权限对应-->
<a shiro:hasRole="root" href="/goods/get">获取单个商品</a>
<a shiro:guest="" href="/goods/getAllGoods"> 获取所有商品</a>
</div>
</body>
</html>

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);
}

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;
}

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;
}

3 在shiro中增加 eache缓存

1.配置依赖

<!-- 配置eache 缓存 -->
<!--开启 cache 缓存-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- ehcache 缓存 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<!-- shiro-ehcache-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.4.0</version>
</dependency>

2.在resouces下添加 ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<defaultCache
eternal="false"
maxElementsInMemory="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU" />
<!-- 授权缓存 -->
<cache name="authorizationCache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">
</cache>
<!-- 认证缓存 -->
<cache name="authenticationCache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">
</cache>
</ehcache>

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值