Spring Security OAuth2 User Credentials Not Correct

384 views Asked by At

Hi i have a spring configuration xml file for OAuth2. I have configured it in such a way as to require the parameters and effect called curl to request the token.It makes requests such as:

curl -X POST -v http://localhost:8080/oauth/token -d "grant_type=password&client_id=test&client_secret=test&username=user1&password=password1"

I wish if prompted the token with username and password wrong, it was generated a json custom error. How can I do?

Thanks

1

There are 1 answers

2
Yashar On

If you want to add additional information impliment a custom "OAuthTokenEnhancer" class from "TokenEnhancer". and in the enhance method you can add your additional information.

 @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        DefaultOAuth2AccessToken result = (DefaultOAuth2AccessToken) accessToken;
        final Map<String, Object> additionalInformation = new HashMap<>();
        additionalInformation.put("prop name", "value");
        result.setAdditionalInformation(additionalInformation);

        return result;
    }

If you want to return a custom error, You may use custom exception in your custom "AuthenticationProvider" ("authenticate" method), like:

if(!checkpassword()){
     authentication.setAuthenticated(false);
     throw new BadCredentialsException(ERROR_MESSAGE);
}