com.github.theborakompanioni
thymeleaf-extras-shiro
2.0.0
/* 配置shiro thymeleaf 标签
* */
@Bean
public ShiroDialect shiroDialect() {
return new ShiroDialect();
}
Title
index
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);
}
@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;
}
// 认证
@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;
}
org.springframework.boot
spring-boot-starter-cache
net.sf.ehcache
ehcache
org.apache.shiro
shiro-ehcache
1.4.0
/**
* 添加 缓存管理器
* @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;
}