springboot Integrated Redis Cache

Last article: springboot integrated logback log

Contents of this article: Redis installation and setup, springboot implementation of Redis integration.

1

Redis Installation

Download from github:

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

Unzip to get the following files:

2

Redis Settings

Set password:

The default configuration for Redis is no password.In the directory just unzipped, we can find the file name

Redis.windows-Service.confConfiguration file, open and search 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

For example, I set the password to: qwertyuiopasdfghjkl.Then change to this:

requirepass qwertyuiopasdfghjkl

IP Binding:

Is Ip bound or inRedis.windows-Service.confConfiguration file.Ctrl + F Search 127.0.0.1 finds the following:

# 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

Supporting these two IP bindings, we usually bind to the intranet, the default is that both the intranet and the extranet can be accessed.Therefore, from a security point of view, if it is used locally, then bind 127.0.0.1. If it is used locally, then bind local IP, such as 192.168.0.8.

3

code implementation

Pom.xmlIntroducing shiro-redis dependencies into the file

<!-- shiro+redis Cache Plugin -->        <dependency>            <groupId>org.crazycake</groupId>            <artifactId>shiro-redis</artifactId>            <version>2.4.2.1-RELEASE</version>        </dependency>

Application.ymlConfigure redis parameters in

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

ShiroConfig.java Add the appropriate configuration in

/**     * redisManager     *     * @return     */    public RedisManager redisManager() {        RedisManager redisManager = new RedisManager();        redisManager.setHost("localhost");        redisManager.setPort(6379);        //Configure Expiration TimeRedisManager.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;    }

stay ShiroConfig.java Inject session manager and cache manager into SecurityManager in

/**   * Create DefaultWebSecurityManager*/  @Bean(name="securityManager")  public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm")UserRealm userRealm){    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();    //Associate realm Security Manager.setRealm(userRealm); //Use Remember Me)Security Manager.setRememberMeManager(rememberMeManager ()); // Custom cache implementation using redisSecurity Manager.setCacheManager(cacheManager());// Customize session management using redisSecurity Manager.setSessionManager(SessionManager ();) return securityManager;}

4

Test Code

For testing convenience, theLoginController.javaA common controller method has been added to the

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

Home.htmlModify Page

<body>  <div th:text="${member.userName}" style="color: green"></div>  <a href="logout">Log out</a>  <div shiro:hasPermission="AuthGroup:editRuleGroup">    <a href="listGroups">Permission modification</a><br/>  </div>  <a href="test">test</a><br/></body>

5

Test comparison

Click test->permission modification->test in turn

When there is no integrated Redis cache: enter it in the address barHttp://localhost8080/hospital/login, view console output after successful login

You can find that when you do not configure your cache, you have the problem of invoking the authorization method once for every request you make.Large user cardinality can put a lot of pressure on the database.So we need to configure the cache to put user information in the cache to reduce database pressure.

Launch redis in cmd: enter in turn

Enter in Address BarHttp://localhost8080/hospital/login, check console output after successful login:

You can see that after configuring the cache, our authorization method is invoked only once, no matter how many requests are made.

This concludes the article.Welcome to my public number: The dark stars surge.

Previous articles:

1,Springboot Integration Shiro

2,Shiro implements RememberMe functionality

3,springboot Integrated Kaptcha Authentication Code

4,springboot integrated logback log


Tags: Java Redis Shiro SpringBoot github

Posted on Thu, 04 Jun 2020 18:19:52 -0400 by Eminem