spring security oauths2 如何实现微信网页授权登录, 10C

spring security oauths2 如何实现微信网页授权登录,目前实现了普通用户的用户名密码登录以及验证码登录,如何加入微信网页授权登录???、

2个回答

看一下,是不是类似于开源中国这样的 支持微信登陆?https://www.oschina.net/home/login?goto_page=https%3A%2F%2Fwww.oschina.net%2F
去看下微信开放平台https://open.weixin.qq.com/cgi-bin/index?t=home/index&lang=zh_CN

hahaxiou
hahaxiou 问题不在这里 在于和oauth2如何集成
一年多之前 回复

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> 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 result = template.getForEntity(url, Object.class);

return result.getBody();

}

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!