I'm using spring security to authenticate users using jwt tokens.
The authentication works fine, when the token is malformed or expired i get a 403 Http status, as shown in the below configuration:
@Bean
public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) {
return http
.exceptionHandling()
.authenticationEntryPoint((swe, e) -> {
return Mono.fromRunnable(() -> {
swe.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
});
}).accessDeniedHandler((swe, e) -> {
return Mono.fromRunnable(() -> {
swe.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
});
}).and()
.csrf().disable()
.formLogin().disable()
.httpBasic().disable()
.authenticationManager(authenticationManager)
.securityContextRepository(securityContextRepository)
.authorizeExchange()
.pathMatchers(HttpMethod.OPTIONS).permitAll()
.pathMatchers("/**").permitAll()
.anyExchange().authenticated()
.and().build();
}
However when i use Principal or Authentication from spring security context to get user information from the jwt token, like in the below code:
@ResponseStatus(HttpStatus.OK)
@GetMapping("/me")
public Mono<ClientDTO> find(final Principal principal) {
return this.clientService.findByMail(principal.getName());
}
If the token is malformed or expired, i get a null pointer exception from the Principal object and an 500 https status.