title: Spring Oauth2 - custom exception handler
body: <p>In Spring Security Oauth2 based authentication when the client sends an access token which needs to be refreshed, the DefaultTokenServices class throws an InvalidTokenException (see at line 235):</p>
  
  <p><a href="https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/token/DefaultTokenServices.java" rel="noreferrer">https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/token/DefaultTokenServices.java</a> </p>
  
  <p>the output when this happens is something like:</p>
  
  <pre><code>{"error":"invalid_token","error_description":"Invalid access token: a0cb5ab9-7281-46bd-a9a2-796a04a906c9"
  }
  </code></pre>
  
  <p>I'd like to change this output but I got lost. Some other answer suggested setting up a custom <strong>exceptionRenderer</strong> but this didn't work either, my custom exception renderer never gets called in these cases. </p>
  
  <p>Also there's something called an <strong>exception translator</strong> but they werent called either in any case.</p>
  
  <p>Part of my spring config:</p>
  
  <pre><code>&lt;bean id="clientAuthenticationEntryPoint"
        class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"&gt;
      &lt;property name="typeName" value="Basic"/&gt;
      &lt;property name="exceptionRenderer" ref="myExceptionRenderer" /&gt;
  &lt;/bean&gt;
  
  
  &lt;bean id="oauthAuthenticationEntryPoint"
        class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"&gt;
        &lt;property name="exceptionRenderer" ref="myExceptionRenderer" /&gt;
        &lt;property name="exceptionTranslator" ref="listyOauthExceptionTranslator" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="oauthAccessDeniedHandler" class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" &gt;
   &lt;property name="exceptionRenderer" ref="myExceptionRenderer" /&gt;
   &lt;property name="exceptionTranslator" ref="myExceptionTranslator" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="myExceptionRenderer" class="com.example.exceptions.MyOauth2ExceptionRenderer" /&gt;
  
  &lt;bean id="myExceptionTranslator" class="com.example.exceptions.MyOauth2ExceptionTranslator" /&gt;
  </code></pre>
  
  <p>The exception renderer:</p>
  
  <pre><code>public class MyExceptionRenderer implements OAuth2ExceptionRenderer {
  
  @Override
  public void handleHttpEntityResponse(HttpEntity&lt;?&gt; responseEntity, ServletWebRequest webRequest) throws Exception {
      System.out.println("Thrown exception");
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I also added a custom Exception Mapper which should get ALL the exceptions, but since I assume its another servlet this doesnt really work in this case?</p>
  
  <pre><code>@Provider
  public class GenericExceptionMapper implements ExceptionMapper&lt;Throwable&gt; {
  
  @Override 
  public Response toResponse(Throwable ex) {
      System.out.println("MAPPING EXCEPTION");
      return Response.status(200).entity().build();
  }
  }
  </code></pre>
  
  <p>I could catch cases of <strong>AuthenticationException</strong>, but not any of the <strong>InvalidTokenExceptions</strong>.</p>
  
  <p>Any help regarding this? Where does Spring actually catch this InvalidTokenException and how can I set it up so I can provide a custom output?</p>