Access token provider returned a null access token, which is illegal according to the contract

Refresh

December 2018

Views

1.1k time

2

I have created rest services with spring security OAuth implementation. To retrieve accessToken below url is used:

http://localhost:8080/alp-services-1.0/oauth/token?grant_type=password&client_id=restapp&client_secret=restapp&username=ad&password=passd

It works with postman perfectly. I created a java client to get access token so that i can access other url as well but I am getting below exception:

  StandardWrapperValve[appServlet]: Servlet.service() for servlet appServlet threw exception
java.lang.IllegalStateException: Access token provider returned a null access token, which is illegal according to the contract.
at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:223)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.getAccessToken(OAuth2RestTemplate.java:173)
at com.znpy.alp.rest.client.impl.AuthenticationServiceImpl.authenticate(AuthenticationServiceImpl.java:45)
at com.znpy.alp.spring.security.AlpAuthenticationProvider.authenticate(AlpAuthenticationProvider.java:29)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:177)
at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:211)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)

My java client code is:

@Service
@EnableOAuth2Client
public class AuthenticationServiceImpl implements AuthenticationService {

private static final String accessTokenUrl = "http://localhost:8080/alp-services-1.0/oauth/token";

@Override
public void authenticate(String username, String password) {
    ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
    resource.setAccessTokenUri(accessTokenUrl);
    resource.setClientId("restapp");
    resource.setClientSecret("restapp");
    resource.setGrantType("password");
    resource.setScope(Arrays.asList("read", "write"));

    resource.setUsername(username);
    resource.setPassword(password);

    AccessTokenRequest atr = new DefaultAccessTokenRequest();

    OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resource, new DefaultOAuth2ClientContext(atr));
    oAuth2RestTemplate.setMessageConverters(getMessageConverter());
    System.out.println("ssssssssssssssssssssssssssssssss");
    System.out.println("AccessToken =========================== " + oAuth2RestTemplate.getAccessToken());

    Object object = oAuth2RestTemplate.exchange("http://localhost:8080/alp/superAdmin/findAllInstitutes", HttpMethod.GET, null, Object.class);
    System.out.println(object);
}

private List<HttpMessageConverter<?>> getMessageConverter() {
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    messageConverters.add(new FormHttpMessageConverter());
    messageConverters.add(new StringHttpMessageConverter());
    messageConverters.add(new MappingJackson2HttpMessageConverter());

    return messageConverters;
}
}

I have applied debugger at rest service. User is validating user. And if i hit url with normal RestTemplate, it works. I could not find any reason why accessToken is null. Any help is appreciated.

0 answers