In Spring, Oauth2-based security authentication, when a client sends an access token that needs to be updated, the DefaultTokenServices class raises an InvalidTokenException (see line 235):
https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/token/DefaultTokenServices.java
the output when this happens looks something like this:
{"error":"invalid_token","error_description":"Invalid access token: a0cb5ab9-7281-46bd-a9a2-796a04a906c9" }
I want to change this conclusion, but I'm lost. Another answer suggested creating a custom exceptionRenderer , but that didn't work either, my custom exception handler is never called in these cases.
There is also something called the exception translator, but in any case they are called.
Part of my Spring configuration:
<bean id="clientAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"> <property name="typeName" value="Basic"/> <property name="exceptionRenderer" ref="myExceptionRenderer" /> </bean> <bean id="oauthAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"> <property name="exceptionRenderer" ref="myExceptionRenderer" /> <property name="exceptionTranslator" ref="listyOauthExceptionTranslator" /> </bean> <bean id="oauthAccessDeniedHandler" class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" > <property name="exceptionRenderer" ref="myExceptionRenderer" /> <property name="exceptionTranslator" ref="myExceptionTranslator" /> </bean> <bean id="myExceptionRenderer" class="com.example.exceptions.MyOauth2ExceptionRenderer" /> <bean id="myExceptionTranslator" class="com.example.exceptions.MyOauth2ExceptionTranslator" />
Exception Visualizer:
public class MyExceptionRenderer implements OAuth2ExceptionRenderer { @Override public void handleHttpEntityResponse(HttpEntity<?> responseEntity, ServletWebRequest webRequest) throws Exception { System.out.println("Thrown exception"); }
}
I also added a custom Exception Mapper that should get ALL exceptions, but since I assume this is another servlet, does this really not work in this case?
@Provider public class GenericExceptionMapper implements ExceptionMapper<Throwable> { @Override public Response toResponse(Throwable ex) { System.out.println("MAPPING EXCEPTION"); return Response.status(200).entity().build(); } }
I could detect AuthenticationException cases, but not any of InvalidTokenExceptions .
Any help on this? Where does Spring really catch this InvalidTokenException and how to set it up so that I can provide custom output?