【发布时间】:2019-10-11 11:39:50
【问题描述】:
我正在添加一个 WebFilter 以在 SecurityWebFilterChain 内执行 JWT 身份验证。我们在 JWT 中编码了许多 API 端点所需的与身份验证无关的信息,因此我需要能够从 JWT 中提取信息并在我的 API 处理程序方法(例如 LoginController.爪哇)。实现这一目标的最佳模式是什么?
这是显示 WebFilter 身份验证的 SecurityWebFilterChain:
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
return http
.authorizeExchange()
.pathMatchers("/login", "/")
.authenticated()
.and()
.addFilterAt(basicAuthenticationFilter(), SecurityWebFiltersOrder.HTTP_BASIC)
.authorizeExchange()
.pathMatchers("/adm")
.authenticated()
.and()
.addFilterAt(basicAuthenticationFilter(), SecurityWebFiltersOrder.HTTP_BASIC)
.authorizeExchange()
.pathMatchers("/api/**")
.access(authorizationManager)
.and()
.addFilterAt(bearerAuthenticationFilter(), SecurityWebFiltersOrder.AUTHENTICATION)
.build();
}
这是我想在 LoginController.java 中访问声明的地方:
@RestController()
@RequestMapping(value = "/login")
public class LoginController {
private final UserMongoRepository repository;
@Autowired
public LoginController(UserMongoRepository repository) {
this.repository = repository;
}
@PostMapping("")
public Mono<User> login(@RequestBody User post,
@RequestParam String user_id,
@RequestParam String username) {
//Need to access information from JWT claims here
return this.repository.findById(user_id);
}
}
【问题讨论】:
标签: java spring spring-security spring-security-oauth2 spring-webflux