我正在尝试通过编写一个简单的 REST 应用程序来学习 Spring Boot,该应用程序将用户登录 ( POST /login) 并显示有关当前用户 ( GET /) 的信息。我正在使用 Redis 进行会话。
POST /login 按预期工作:它返回主体并在浏览器和 Redis 中设置会话 cookie。
GET /然而,随后的请求返回anonymousUser。我错过了什么?
pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
application.properties:
spring.session.store-type=redis
server.servlet.session.timeout=3600s
spring.session.redis.flush-mode=on-save
spring.session.redis.namespace=spring:session
spring.redis.host=localhost
spring.redis.port=6379
Run Code Online (Sandbox Code Playgroud)
IndexController.java:
@Controller
public class IndexController {
private AuthenticationManager authenticationManager;
IndexController(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
@GetMapping
ResponseEntity …Run Code Online (Sandbox Code Playgroud)