We have an application that uses spring-security-oauth2:1.0 . I tried to change it to a newer version, spring-security-oauth2:2.0.7.RELEASE . Some classes were deleted, some package structure changed, I was able to figure out all these things, and I was able to start the server without any problems. But here I come across a strange problem.
In OAuth2 - 1.0 version , when a user logs in, we used a GET request to /oauth/token , for example:
HTTP: // local: 8080 / echo / OAuth / token grant_type = password and client_id = WS & client_secret = secret and scope = read, write and username = john @ abc.com and password = password123
And before, it worked just fine.
When I try to do the same, firstly, I cannot make a GET request due to the logic in TokenEndPoint.java
private Set<HttpMethod> allowedRequestMethods = new HashSet<HttpMethod>(Arrays.asList(HttpMethod.POST)); @RequestMapping(value = "/oauth/token", method=RequestMethod.GET) public ResponseEntity<OAuth2AccessToken> getAccessToken(Principal principal, @RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException { if (!allowedRequestMethods.contains(HttpMethod.GET)) { throw new HttpRequestMethodNotSupportedException("GET"); } return postAccessToken(principal, parameters); }
I tried to make a POST request the same as the URL above, but I get an InsufficientAuthenticationException with an error message
No client authentication. Try adding the appropriate authentication filter.
This is because of the following POST request controller in TokenEndpoint.java . When I debug, I see that principal is null.
@RequestMapping(value = "/oauth/token", method=RequestMethod.POST) public ResponseEntity<OAuth2AccessToken> postAccessToken(Principal principal, @RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException { //principal is null here if (!(principal instanceof Authentication)) { throw new InsufficientAuthenticationException( "There is no client authentication. Try adding an appropriate authentication filter."); } ............. }
I have an authentication filter and it worked well when I used version 1.0 . These are the corresponding ancestors of my config:
<authentication-manager xmlns="http://www.springframework.org/schema/security"> <authentication-provider user-service-ref="userDetailsService"/> </authentication-manager> <bean id="userDetailsService" class="com.hcl.nc.service.UserDetailsService"> <constructor-arg><ref bean="sessionFactory" /></constructor-arg> </bean>
I always thought that the request will be authenticated by authentication-provider and sent to token-endpoint but this does not seem to be the correct thread. After debugging the application with version 2.0.7 now I really doubt my understanding of the flow.
Can someone explain why it worked in the previous version and why it does not work now?
Do I have to do something else to get an OAuth token?
NOTE : I have already checked these questions: here , here , here . But I could not find the right solution.