写点什么

SpringBoot:Shiro- 整合 -Redis,也不用担心用户投诉啦,java 技术经理面试题

作者:Java高工P7
  • 2021 年 11 月 10 日
  • 本文字数:1520 字

    阅读完需:约 5 分钟

this.jedisPool = new JedisPool(this.getJedisPoolConfig(), redis.getHost(), redis.getPort(),redis.getTimeout(), redis.getPassword(), redis.getDatabase());}}}


@Overrideprotected Jedis getJedis() {if (this.jedisPool == null) {this.init();}return this.jedisPool.getResource();}}


参数配置?RedisProperties


@Data@ConfigurationProperties(prefix = "redis")public class RedisProperties {


private String host;private int port;private int timeout;private String password;private int database;}


配置?ShiroConfig


/**


  • Shiro 权限配置

  • 一定要配置 @Configuration 和 @EnableConfigurationProperties 注解*/@Configuration@EnableConfigurationProperties({RedisProperties.class})public class ShiroConfig {


private RedisProperties redis;


public ShiroConfig(RedisProperties redis) {this.redis = redis;}


@Beanpublic UserRealm userRealm() {return new UserRealm();}


@Beanpublic ShiroFilterFactoryBean shiroFilterFactoryBean (SecurityManager securityManager) {ShiroFilterFactoryBean sh


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


iroFilterFactoryBean = new ShiroFilterFactoryBean();shiroFilterFactoryBean.setSecurityManager(securityManager);shiroFilterFactoryBean.setLoginUrl("/index.html");shiroFilterFactoryBean.setUnauthorizedUrl("/403");// 拦截器 Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();/**


  • 静态文件*/filterChainDefinitionMap.put("/file/","anon");/

  • 登录注册/filterChainDefinitionMap.put("/register.shtml","anon");filterChainDefinitionMap.put("/login.shtml","anon");/*

  • 管理后台*/filterChainDefinitionMap.put("/sys/", "roles[admin]");filterChainDefinitionMap.put("/", "authc");shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);return shiroFilterFactoryBean;}


@Beanpublic SessionsSecurityManager securityManager() {DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();securityManager.setRealm(userRealm());securityManager.setCacheManager(cacheManager());securityManager.setSessionManager(sessionManager());return securityManager;}@Beanpublic DefaultWebSessionManager sessionManager() {DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();sessionManager.setSessionIdUrlRewritingEnabled(false);sessionManager.setSessionDAO(redisSessionDAO());return sessionManager;}@Beanpublic ShiroDialect shiroDialect(){return new ShiroDialect();}


/**


  • cacheManager 缓存 redis 实现

  • @return*/public RedisCacheManager cacheManager() {RedisCacheManager redisCacheManager = new RedisCacheManager();redisCacheManager.setRedisManager(redisManager());return redisCacheManager;}


/**


  • 配置 shiro redisManager

  • @return*/public RedisManager redisManager() {RedisManager redisManager = new RedisManager(redis);return redisManager;}


/**


  • RedisSessionDAO shiro sessionDao 层的实现

  • 原理就是重写 AbstractSessionDAO

  • 有兴趣的小伙伴自行阅读源码*/@Beanpublic RedisSessionDAO redisSessionDAO() {RedisSessionDAO redisSessionDAO = new RedisSessionDAO();redisSessionDAO.setRedisManager(redisManager());return redisSessionDAO;}}

小结

是不是很爽,以后重启应用再也不用担心用户投诉了?

用户头像

Java高工P7

关注

还未添加个人签名 2021.11.08 加入

还未添加个人简介

评论

发布
暂无评论
SpringBoot:Shiro-整合-Redis,也不用担心用户投诉啦,java技术经理面试题