最近自己搭建了个springboot2.x整合shiro框架使用Redis缓存的环境,以后可能会用得上,特意保存记录下来。
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com
zjcloud
0.0.1-SNAPSHOT
war
zjcloud
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.7
com.alibaba
druid-spring-boot-starter
1.1.13
com.alibaba
fastjson
1.2.57
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-tomcat
provided
org.springframework.boot
spring-boot-starter-data-redis
org.crazycake
shiro-redis-spring-boot-starter
3.2.1
org.apache.shiro
shiro-spring-boot-web-starter
1.4.0-RC2
org.springframework.boot
spring-boot-starter-test
test
com.alibaba
fastjson
1.2.47
org.springframework.boot
spring-boot-maven-plugin
然后是ShiroConfig配置类,里面的配置很重要。
package com.zjcloud.util.shiro;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SessionsSecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.crazycake.shiro.RedisCacheManager;
import org.crazycake.shiro.RedisManager;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.LinkedHashMap;
import java.util.Map;
@Configuration
public class ShiroConfig {
//不需要在此处配置权限页面,因为上面的ShiroFilterFactoryBean已经配置过,但是此处必须存在,因为shiro-spring-boot-web-starter或查找此Bean,没有会报错
@Bean
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
return new DefaultShiroFilterChainDefinition();
}
/**
* 配置shiroFilter过滤器
*
* @return
*/
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean() {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//设置安全管理器
shiroFilterFactoryBean.setSecurityManager(securityManager());
//权限配置
Map filterChainDefinitionMap = new LinkedHashMap<>();
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
/**
* 配置securityManager 安全管理器
*
* @return
*/
@Bean
public SessionsSecurityManager securityManager() {
DefaultWebSecurityManager webSecurityManager = new DefaultWebSecurityManager();
//配置认证器
webSecurityManager.setRealm(userRealm());
webSecurityManager.setSessionManager(sessionManager());
webSecurityManager.setCacheManager(redisCacheManager());
return webSecurityManager;
}
/**
* 配置自定义认证器
*
* @return
*/
@Bean
public UserRealm userRealm() {
UserRealm userRealm = new UserRealm();
userRealm.setCredentialsMatcher(hashedCredentialsMatcher());
return userRealm;
}
/**
* 配置加密方式
*
* @return
*/
@Bean
public HashedCredentialsMatcher hashedCredentialsMatcher() {
//盐
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
//配置散列算法,,使用MD5加密算法
hashedCredentialsMatcher.setHashAlgorithmName("md5");
//设置散列次数
hashedCredentialsMatcher.setHashIterations(1024);
return hashedCredentialsMatcher;
}
/**
* Session Manager
* 使用的是shiro-redis开源插件
*/
@Bean
public SessionManager sessionManager() {
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
sessionManager.setSessionDAO(redisSessionDAO());
return sessionManager;
}
/**
* cacheManager 缓存 redis实现
* 使用的是shiro-redis开源插件
*
* @return
*/
@Bean
public RedisCacheManager redisCacheManager() {
RedisCacheManager redisCacheManager = new RedisCacheManager();
redisCacheManager.setRedisManager(redisManager());
redisCacheManager.setExpire(5000);
//指定存入Redis的主键
redisCacheManager.setPrincipalIdFieldName("id");
return redisCacheManager;
}
/**
* 配置shiro redisManager
* 使用的是shiro-redis开源插件
*
*/
@Bean
public RedisManager redisManager() {
RedisManager redisManager = new RedisManager();
return redisManager;
}
/**
* RedisSessionDAO shiro sessionDao层的实现 通过redis
* 使用的是shiro-redis开源插件
*/
@Bean
public RedisSessionDAO redisSessionDAO() {
RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
redisSessionDAO.setRedisManager(redisManager());
redisSessionDAO.setExpire(2000);
return redisSessionDAO;
}
}
自定义Realm,这个不通用啊,别复制。
package com.zjcloud.util.shiro;
import com.zjcloud.custsystem.mapper.ZjEmpAccountMapper;
import com.zjcloud.custsystem.mapper.ZjEmpMenubarMapper;
import com.zjcloud.custsystem.mapper.ZjEmpRoleMapper;
import com.zjcloud.custsystem.pojo.ZjEmpAccount;
import com.zjcloud.custsystem.pojo.ZjEmpAccountExample;
import com.zjcloud.custsystem.pojo.ZjEmpMenubar;
import com.zjcloud.custsystem.pojo.ZjEmpRole;
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.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class UserRealm extends AuthorizingRealm {
/**
* 授权
*
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection){
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
principalCollection.getPrimaryPrincipal();
//添加角色
//添加权限
}
return info;
}
/**
* 认证
*
* @param authenticationToken
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
//获取用户名
String username = token.getUsername();
//查询实体类
//获取密码
//加盐
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(实体类,密码,盐,this.getName());
return info;
}
}
只是当做保存,以后方便查看,所以就只是把一些核心代码贴出来就行了。
还有个加密密码的,也贴出来
String hex = new SimpleHash("MD5", "123", "abc", 1024).toHex();