I'm trying to verify the claim inside the JWT token using JwtClaimsSetVerifier
given by Spring Boot 2.1. The problem is that Spring always throws an exception with the default exception message:
{
"error": "invalid_token",
"error_description": "Cannot convert access token to JSON"
}
Even if I create a custom exception which extends the ClientAuthenticationException
, I get the same exception message.
When the JWT claim verification fails, I want to modify the exception message. Here is my configuration class:
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceserverConfig extends ResourceServerConfigurerAdapter{
@Autowired
private DataSource dataSource;
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll().and()
.exceptionHandling().accessDeniedHandler(new CustomAccessDeniedHandler());
}
@Bean
public DataSource getDataSource() {
return dataSource;
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey("qwerty123");
converter.setJwtClaimsSetVerifier(jwtClaimsSetVerifier());
return converter;
}
@Bean
public AuthenticationFailureHandler authenticationFailureHandler()
{
return new RestAuthenticationFailureHandler();
}
@Bean
public JwtClaimsSetVerifier jwtClaimsSetVerifier() {
return new DelegatingJwtClaimsSetVerifier(Arrays.asList(customJwtClaimVerifier()));
}
@Bean
public JwtClaimsSetVerifier customJwtClaimVerifier() {
return new CustomClaimVerifier();
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
TokenStore tokenStoreRes = new JdbcTokenStore(dataSource);
resources.resourceId("RESOURCE").tokenStore(tokenStoreRes);
}
@Bean
@Primary
public DefaultTokenServices tokenJWTServices() {
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
TokenStore tokenStoreRes = new JwtTokenStore(accessTokenConverter());
defaultTokenServices.setTokenStore(tokenStoreRes);
defaultTokenServices.setSupportRefreshToken(true);
return defaultTokenServices;
}
}
Here is my JWTClaimVerifier
class:
public class CustomClaimVerifier implements JwtClaimsSetVerifier{
@Autowired
HttpServletRequest request;
@Override
public void verify(Map<String, Object> claims) throws InvalidTokenException {
try {
JsonParser parser = new JsonParser();
String json = new Gson().toJson(claims.get("userdetails"));
JsonElement menu = parser.parse(json);
String menuList = menu.getAsJsonObject().get("menu").getAsString();
boolean isMenuAccessible = validateAccessForMenu(request.getHeader("menuClicked"), menuList);
if(!isMenuAccessible) {
throw new InvalidTokenException("Invalid Permissions");
}
} catch (Exception e) {
throw new InvalidTokenException(e.getMessage());
}
}
}
I want an exception with my custom exception message when JWT claim verification fails, but all I get is the standard exception message thrown by Spring Security.