zoukankan      html  css  js  c++  java
  • Spring-Security-OAuth2微信网页授权

    @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();

    }

    }

  • 相关阅读:
    Python(八)进程、线程、协程篇
    Python(七)Socket编程、IO多路复用、SocketServer
    Python(六)面向对象、异常处理、反射、单例模式
    Python并发编程之多进程
    ORM框架SQLAlchemy
    Python并发编程之IO模型
    Python并发编程之同步异步and阻塞非阻塞
    二分查找
    插入排序
    单向链表
  • 原文地址:https://www.cnblogs.com/kingsy/p/6375881.html
Copyright © 2011-2022 走看看