Announcement Announcement Module
No announcement yet.
Client Side caching of OAuth2RestTemplate and OAuth2AccessToken possible? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Client Side caching of OAuth2RestTemplate and OAuth2AccessToken possible?


    My client (website) obtains a access token from a web service. The client is attempting to cache the OAuth2RestTemplate and OAuth2AccessToken in a singleton so that subsequent requests to the web service can be signed with the oauth token.

    However I have noticed that even though the client is storing the token and template in a singleton, when a subsequent request is made both the token and restemplate are null??? All other member vars of the single are as they were but those 2 are null every time.

    Is this expected? For info: the token is provided for a few mins so there is no reason for it to be numm within seconds.


  • #2
    I don't think there's enough information here to understand what you did precisely. Maybe you can provide some code sample and config snippets? Please use [CODE][/CODE] tags.


    • #3
      Hi Dave

      This is my code:-
      public class APIService {
      	private String apiUrl;
      	private final Logger LOGGER = LoggerFactory.getLogger(APIService.class);
      	private static final String API_TOKEN_ENDPOINT = "/oauth/token";
      	private static final String RESOURCE_ENDPOINT = "/photo";
      	private OAuth2RestTemplate restTemplate;
      	private OAuth2AccessToken userAccessToken;
      	 * Authenticates the user against the API and receives an access token back. 
      	 * @param uid
      	 * @param pwd
      	public void authenticateUser(String uid, String pwd){
      		ResourceOwnerPasswordResourceDetails resource = getResourceDetails(uid, pwd);
      	        restTemplate = new OAuth2RestTemplate(resource);
      	        userAccessToken = restTemplate.getAccessToken();
      	        LOGGER.debug("Access Token value ->"+userAccessToken.getValue());
      	        LOGGER.debug("Token type ->"+userAccessToken.getTokenType());
      	public String getPhotoDetails(String id){
      		return restTemplate.getForObject(apiUrl + RESOURCE_ENDPOINT + "/" + id, String.class);
      	private ResourceOwnerPasswordResourceDetails getResourceDetails(String uid, String pwd){
      		ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
      		resource.setAccessTokenUri(apiUrl + API_TOKEN_ENDPOINT);
      		return resource;
      	public void setApiUrl(String apiUrl) {
      		this.apiUrl = apiUrl;
      In authenticateUser() I create and initialise the restTemplate. This works just fine and retrieves the access token. In a subsequent request getPhotoDetails() attempts to get a resource from the web service using the same restTemplate. But for some reason the restTemplate is null and so is userAccessToken. How could that be? These are member variables of the APIService instance. I notice the other member variable apiUrl is just fine.


      • #4
        Fixed by making APIService instance session scoped. Still strange as to why it was getting set to null though - I'm guessing the template by default has request scope?


        • #5
          Hard to say still. I would suspect that if this ApiService is a Spring bean and you have declared it as a normal singleton, then you must be reloading your application context more often than necessary. If I were you I would try and nail this one because it might be a sign of some other problems with your app.