@Controller
public class Controller1 {
@Autowired
private OAuth2ClientContext context;
@Bean
@Scope("session")
public OAuth2ClientContext createContext(){
OAuth2ClientContext context = new DefaultOAuth2ClientContext();
return context;
}
@ResponseBody
@RequestMapping("/weixin/authorize")
public Object getUserInfo(HttpServletRequest request){
class WeixinAuthorizationCodeAccessTokenProvider extends AuthorizationCodeAccessTokenProvider{
public WeixinAuthorizationCodeAccessTokenProvider(List<HttpMessageConverter<?>> messageConverters){
this.setMessageConverters(messageConverters);
this.setTokenRequestEnhancer(new RequestEnhancer() {
@Override
public void enhance(AccessTokenRequest request, OAuth2ProtectedResourceDetails resource,
MultiValueMap<String, String> form, HttpHeaders headers) {
String clientId = form.getFirst("client_id");
String clientSecret = form.getFirst("client_secret");
form.set("appid", clientId);
form.set("secret", clientSecret);
form.remove("client_id");
form.remove("client_secret");
}
});
}
@Override
public OAuth2AccessToken obtainAccessToken(OAuth2ProtectedResourceDetails details,
AccessTokenRequest request) throws UserRedirectRequiredException, UserApprovalRequiredException,
AccessDeniedException, OAuth2AccessDeniedException {
try {
return super.obtainAccessToken(details, request);
} catch (UserRedirectRequiredException e) {
Map<String, String> params = e.getRequestParams();
String clientId = params.get("client_id");
params.put("appid", clientId);
params.remove("client_id");
throw e;
}
}
};
class WeixinOAuth2RestTemplate extends OAuth2RestTemplate{
public WeixinOAuth2RestTemplate(AuthorizationCodeResourceDetails resource , OAuth2ClientContext context){
super(resource , context);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new MappingJackson2HttpMessageConverter(){
@Override
protected boolean canRead(MediaType mediaType) {
return true;
}
});
this.setMessageConverters(messageConverters);
this.setAccessTokenProvider(new WeixinAuthorizationCodeAccessTokenProvider(messageConverters));
}
@Override
protected URI appendQueryParameter(URI uri, OAuth2AccessToken accessToken) {
uri = super.appendQueryParameter(uri, accessToken);
String url = uri.toString();
if(url.contains("$openid$")){
String openid = (String)accessToken.getAdditionalInformation().get("openid");
try {
uri = new URI(url.replace("$openid$", openid));
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
return uri;
}
}
AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
resource.setAuthenticationScheme(AuthenticationScheme.form);
resource.setClientAuthenticationScheme(AuthenticationScheme.form);
resource.setClientId("xxxxxxxxxxx");
resource.setClientSecret("xxxxxxxxxxx");
resource.setUserAuthorizationUri("https://open.weixin.qq.com/connect/oauth2/authorize");
resource.setAccessTokenUri("https://api.weixin.qq.com/sns/oauth2/access_token");
resource.setScope(Arrays.asList("snsapi_userinfo"));
context.getAccessTokenRequest().setCurrentUri(request.getRequestURL().toString());
// resource.setPreEstablishedRedirectUri("http://www.baidu.com");
// resource.setUseCurrentUri(false);
OAuth2RestTemplate template = new WeixinOAuth2RestTemplate(resource , context);
String url = "https://api.weixin.qq.com/sns/userinfo?lang=zh_CN&openid=$openid$";
ResponseEntity<Object> result = template.getForEntity(url, Object.class);
return result.getBody();
}
}