Transformation based on Spring Security's old project and Spring Session Redis

background

Because the existing old projects need to be transformed into a distributed system, the first step to go to state is to go to session. Therefore, it is necessary to modify the existing Spring Security-based programs to change the information stored in the session from the cache to redis, thereby de-stating.

step

1. Choose the appropriate dependency according to the current existing framework

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
    <version>1.3.5.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework.session</groupId>
	<artifactId>spring-session</artifactId>
	<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.8.23.RELEASE</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

2. Configure redis in the application file

<bean id="connectionFactory"
	class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
	<property name="hostName" value="x.x.x.x" />
	<property name="port" value="6379" />
	<property name="password" value="1234567"/>
	<property name="poolConfig" ref="poolConfig" />
</bean>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
	<property name="maxIdle" value="50" />
	<property name="maxTotal" value="100" />
	<property name="maxWaitMillis" value="20000" />
</bean>

3. Turn on spring annotations and introduce the use of classes

<context:annotation-config/>
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>

4. Find the application configuration file that used to configure spring security, add sessionRegistry configuration to combine spring-session with spring security

<beans:bean id="sessionRegistry" class="org.springframework.session.security.SpringSessionBackedSessionRegistry">
	<beans:constructor-arg name="sessionRepository" ref="sessionRepository"></beans:constructor-arg>
</beans:bean>

Progress and shortcomings

The transformation of old projects allowed me to switch from traditional CRUD work to project construction, and to understand the use of distributed and middleware, which greatly broadened my horizons. At the same time, my use of maven and conflict resolution are getting more and more handy, hoping to make a breakthrough soon.

Guess you like

Origin blog.csdn.net/weixin_44159662/article/details/109072921