0
When an authentication failure occurs in Spring Security, it returns an error in the response header. I would like to customize the error and return a message in the reply Body.
Below is the code used to validate Oauth2 with Spring Security Webflux with Opaque Token
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class SecurityConfig {
@Value("${spring.security.oauth2.resourceserver.opaque-token.introspection-uri}")
private String introspectUri;
@Value("${spring.security.oauth2.resourceserver.opaque-token.client-id}")
private String clientId;
@Value("${spring.security.oauth2.resourceserver.opaque-token.client-secret}")
private String clientSecret;
@Bean
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http.csrf().disable()
.formLogin().disable()
.httpBasic().disable()
.authorizeExchange()
.pathMatchers(HttpMethod.OPTIONS).permitAll()
.pathMatchers("/**").hasRole("read")
.anyExchange().denyAll()
.and()
.oauth2ResourceServer(ServerHttpSecurity.OAuth2ResourceServerSpec::opaqueToken);
return http.build();
}
@Bean
public ReactiveOpaqueTokenIntrospector introspector() {
return new JwtOpaqueTokenIntrospector(this.introspectUri, this.clientId, this.clientSecret);
}
}
Current response:
Response I wish to return
{
"developerMessage": "Unauthorized - make sure the header parameter Authorization is valid",
"userMessage": "You are not authorized to perform this operation",
"errorCode": 30001,
"moreInfo": ""
}
Welcome to the Stackoverflow in Portuguese. As the name suggests, the official language used here is Portuguese. So, could you please translate your question? If you prefer, you can also ask the same question on Stackoverflow website in English.
– Denis Rudnei de Souza
Welcome to Stackoverflow in Portuguese. As the name implies, the Official language used here is English. So, can you Please Translate your Question? If you prefer, you may also Ask this same Question in the English Stackoverflow site.
– Denis Rudnei de Souza