我正在添加一个 WebFilter 以在 SecurityWebFilterChain 内部执行 JWT 身份验证。我们在 JWT 中编码了许多 API 端点所需的非身份验证相关信息,因此我需要能够从 JWT 中提取信息并在我的 API 处理程序方法(例如,LoginController.爪哇)。实现这一目标的最佳模式是什么?
这是我的 SecurityWebFilterChain 显示 WebFilter 身份验证:
@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);
}
}
慕容708150
相关分类