Configuration
When I haven't used spring session yet, I have never understood how to make the backend actively store the session in the persistence layer (such as redis). After reading the spring session documentation and blog these days, I understand it. Simply put, it is the following A passage:
Spring Session creates a Spring bean with the name of springSessionRepositoryFilter that implements Filter. The filter is in charge of replacing the HttpSession implementation to be backed by Spring Session.
However , the configuration tutorial on the spring.io official website is really not good. It is impossible to insert data into redis when it is configured , and the demo given cannot be run on this machine.
Now give my configuration scheme
rely
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
application.properties
# 必须要加,否则你加依赖不加这个连启动都报错
spring.session.store-type=redis
# 默认为localhost:6379,可以通过配置更改
spring.redis.host=localhost
# Redis server password
# 如果密码为空,就不要写了,否则也报错
# spring.redis.password=
# Redis server port.
spring.redis.port=6379
# 指定储存的位置
spring.session.redis.namespace=redis:session
spring.session.redis.flush-mode=immediate
spring.session.timeout=5m
# 超时断连,不指定可能会报unable to connect to redis
spring.redis.timeout=3000
View redis
Send any request, and then check the keys * on redis, you can see the saved session
Configure spring security
As a non-login side, the security configuration does not need to be complicated, only a simple securityConfig class is needed
Introduce dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
SecurityConfig class
package com.ilife.weiboservice.config;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.csrf().disable();
}
}
controller
For the interface to be protected, @PreAuthorize needs to be annotated as follows
@ApiOperation(notes = "Get all Weibos from database of one user specified by userID", value = "get one user's Weibos", httpMethod = "GET")
@RequestMapping(path = "/weibo/getWeibos")
@PreAuthorize("hasRole('ROLE_USER')")
public List<Weibo> getWeibos(@ApiParam(name = "userId", value = "The user ID of a WeiBo user,should be a Long Integer") @RequestParam("userId") Long uid) {
System.out.println("********** getWeibos **********");
return weiboService.findAllByUid(uid);
}