@FrameworkEndpointpublicclassTokenEndpointextendsAbstractEndpoint{@RequestMapping(value ="/oauth/token", method=RequestMethod.GET)publicResponseEntity<OAuth2AccessToken>getAccessToken(Principal principal,@RequestParamMap<String,String> parameters)throwsHttpRequestMethodNotSupportedException{if(!allowedRequestMethods.contains(HttpMethod.GET)){thrownewHttpRequestMethodNotSupportedException("GET");}returnpostAccessToken(principal, parameters);}@RequestMapping(value ="/oauth/token", method=RequestMethod.POST)publicResponseEntity<OAuth2AccessToken>postAccessToken(Principal principal,@RequestParamMap<String,String> parameters)throwsHttpRequestMethodNotSupportedException{if(!(principal instanceofAuthentication)){thrownewInsufficientAuthenticationException("There is no client authentication. Try adding an appropriate authentication filter.");}String clientId =getClientId(principal);ClientDetails authenticatedClient =getClientDetailsService().loadClientByClientId(clientId);TokenRequest tokenRequest =getOAuth2RequestFactory().createTokenRequest(parameters, authenticatedClient);if(clientId !=null&&!clientId.equals("")){// Only validate the client details if a client authenticated during this// request.if(!clientId.equals(tokenRequest.getClientId())){// double check to make sure that the client ID in the token request is the same as that in the// authenticated clientthrownewInvalidClientException("Given client ID does not match authenticated client");}}if(authenticatedClient !=null){
oAuth2RequestValidator.validateScope(tokenRequest, authenticatedClient);}if(!StringUtils.hasText(tokenRequest.getGrantType())){thrownewInvalidRequestException("Missing grant type");}if(tokenRequest.getGrantType().equals("implicit")){thrownewInvalidGrantException("Implicit grant type not supported from token endpoint");}if(isAuthCodeRequest(parameters)){// The scope was requested or determined during the authorization stepif(!tokenRequest.getScope().isEmpty()){
logger.debug("Clearing scope of incoming token request");
tokenRequest.setScope(Collections.<String>emptySet());}}if(isRefreshTokenRequest(parameters)){// A refresh token has its own default scopes, so we should ignore any added by the factory here.
tokenRequest.setScope(OAuth2Utils.parseParameterList(parameters.get(OAuth2Utils.SCOPE)));}OAuth2AccessToken token =getTokenGranter().grant(tokenRequest.getGrantType(), tokenRequest);if(token ==null){thrownewUnsupportedGrantTypeException("Unsupported grant type: "+ tokenRequest.getGrantType());}returngetResponse(token);}}
/* * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http:/