springboot 集成 Redis 缓存

上期文章:springboot 集成logback 日志

本文内容:Redis 的安装及设置,springboot 对 Redis 集成的实现。

1

Redis 安装

从 github  下载:

https://github.com/MSOpenTech/redis/releases

解压缩得到以下文件:

2

Redis 设置

设置密码:

Redis 的默认配置是没有密码。 在刚刚解压的目录下,我们能够找到文件名为

redis.windows-service.conf 的配置文件,打开并搜索 requirepass 

################################## SECURITY ###################################
# Require clients to issue AUTH <PASSWORD> before processing any other# commands.  This might be useful in environments in which you do not trust# others with access to the host running redis-server.## This should stay commented out for backward compatibility and because most# people do not need auth (e.g. they run their own servers).## Warning: since Redis is pretty fast an outside user can try up to# 150k passwords per second against a good box. This means that you should# use a very strong password otherwise it will be very easy to break.#requirepass qwertyuiopasdfghjkl

比如我把密码设置为:qwertyuiopasdfghjkl 。那么就修改成这样:

requirepass qwertyuiopasdfghjkl

IP 绑定:

Ip绑定还是在 redis.windows-service.conf 配置文件里。Ctrl + F 搜索127.0.0.1 能找到如下所示:

# By default Redis listens for connections from all the network interfaces# available on the server. It is possible to listen to just one or multiple# interfaces using the "bind" configuration directive, followed by one or# more IP addresses.## Examples:## bind 192.168.1.100 10.0.0.1bind 127.0.0.1

支持这2种 IP 绑定,我们一般就绑定内网,默认是内网外网都可以访问。所以我们从安全的角度考虑,如果是本机使用就绑定 127.0.0.1 ,如果是局域网内使用请绑定本地 IP ,如:192.168.0.8等。

3

代码实现

pom.xml 文件中引入shiro-redis依赖

<!-- shiro+redis缓存插件 -->        <dependency>            <groupId>org.crazycake</groupId>            <artifactId>shiro-redis</artifactId>            <version>2.4.2.1-RELEASE</version>        </dependency>

application.yml 中配置redis的相关参数

###redisspring:    redis:        host: localhost        port: 6379        jedis:            pool:                max-idle: 8                min-idle: 0                max-active: 8                max-wait: -1        timeout: 0

ShiroConfig.java 中添加相应的配置

/**     * redisManager     *     * @return     */    public RedisManager redisManager() {        RedisManager redisManager = new RedisManager();        redisManager.setHost("localhost");        redisManager.setPort(6379);        // 配置过期时间        redisManager.setExpire(1800);        return redisManager;    }
    /**     * cacheManager      *     * @return     */    public RedisCacheManager cacheManager() {        RedisCacheManager redisCacheManager = new RedisCacheManager();        redisCacheManager.setRedisManager(redisManager());        return redisCacheManager;    }
    /**     * redisSessionDAO     */    public RedisSessionDAO redisSessionDAO() {        RedisSessionDAO redisSessionDAO = new RedisSessionDAO();        redisSessionDAO.setRedisManager(redisManager());        return redisSessionDAO;    }
    /**     * sessionManager     */    public DefaultWebSessionManager SessionManager() {        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();        sessionManager.setSessionDAO(redisSessionDAO());        return sessionManager;    }

在ShiroConfig.java中将session管理器和cache管理器注入到SecurityManager中

/**   * 创建DefaultWebSecurityManager   */  @Bean(name="securityManager")  public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm")UserRealm userRealm){    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();    //关联realm    securityManager.setRealm(userRealm);    //使用记住我    securityManager.setRememberMeManager(rememberMeManager());    // 自定义缓存实现 使用redis        securityManager.setCacheManager(cacheManager());        // 自定义session管理 使用redis        securityManager.setSessionManager(SessionManager());    return securityManager;  }

4

测试代码

为了方便测试,在loginController.java 中添加了一个普通的控制器方法

@RequestMapping("/test")    @ResponseBody    public void syso() {      System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaa");    }

home.html 修改页面

<body>  <div th:text="${member.userName}" style="color: green"></div>  <a href="logout">退出登录</a>  <div shiro:hasPermission="AuthGroup:editRuleGroup">    <a href="listGroups">权限修改</a><br/>  </div>  <a href="test">test</a><br/></body>

5

测试比较

依次点击 test->权限修改->test

没有集成 Redis 缓存时:在地址栏输入http://localhost:8080/hospital/login ,登陆成功后查看控制台输出

可以发现,在没有配置缓存的时候,会存在这样的问题:每发起一个请求,就会调用一次授权方法。用户基数大请求多的时候,会对数据库造成很大的压力。所以我们需要配置缓存,将用户信息放在缓存里,从而减小数据库压力。

在cmd 中启动redis:依次输入

在地址栏输入http://localhost:8080/hospital/login ,登陆成功后查看控制台输出:

可以看出配置缓存之后,无论多少次请求,我们的授权方法只调用了一次。

至此,本文结束。欢迎各位关注我的公众号:暗星涌动。

往期文章:

1、Springboot 整合 Shiro

2、Shiro 实现 RememberMe 功能

3、springboot 集成Kaptcha验证码

4、springboot 集成logback 日志

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暗星涌动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值