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

    }

    }

  • 相关阅读:
    Kafka设计解析(二)- Kafka High Availability (上)
    Kafka设计解析(三)- Kafka High Availability (下)
    Kafka深度解析
    Cloudera Manager(CDH5)内部结构、功能包括配置文件、目录位置等
    Failed to start /etc/rc.d/rc.local Compatibility
    Offset Management For Apache Kafka With Apache Spark Streaming
    maven-assembly-plugin打包可执行的jar包
    How Cigna Tuned Its Spark Streaming App for Real-time Processing with Apache Kafka
    SystemTap Beginners Guide
    数据可视化的开源方案: Superset vs Redash vs Metabase (二)
  • 原文地址:https://www.cnblogs.com/kingsy/p/6375881.html
Copyright © 2020-2023  润新知