Override rest api invalid_token response

Hi,
I am trying to override rest api response when the token is invalid.

The original response looks like this:

{
    "error": "invalid_token",
    "error_description": "Invalid access token: bdbf7a85-1468-4eae-a437-5597f65a2538"
}

What I need is something like this:

{
    "errorCode": "401",
    "errorMessage": "Invalid access token"
}

I try to make this happen with custom AuthenticationEntryPoint, so I create a class which extends AuthenticationEntryPoint, and override commence method. Then I make some changes in rest-dispatcher-spring.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:security="http://www.springframework.org/schema/security"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <context:component-scan base-package="com.test.restfulapi.rest"/>

    <bean id="myAuthenticationEntryPoint"
          class="com.test.restfulapi.rest.MyAuthenticationEntryPoint"/>

    <security:http pattern="/rest/resource/**"
                   create-session="stateless"
                   entry-point-ref="myAuthenticationEntryPoint"
                   xmlns="http://www.springframework.org/schema/security">
        <!-- Specify one or more protected URL patterns-->
        <intercept-url pattern="/rest/resource/**" access="isAuthenticated()"/>
        <anonymous enabled="false"/>
        <csrf disabled="true"/>
        <cors configuration-source-ref="cuba_RestCorsSource"/>
        <custom-filter ref="resourceFilter" before="PRE_AUTH_FILTER"/>
        <custom-filter ref="cuba_AnonymousAuthenticationFilter" after="PRE_AUTH_FILTER"/>
    </security:http>

</beans>

But everything remains the same, is there anything I did wrong?

Thanks in advance.

1 Like

Hello @dpyfjh63514
I have the same problem. Do you found a solution?
Thanks!