zoukankan      html  css  js  c++  java
  • OAuth2社交登录Test(例:Gitee)

      1. 我们的的操作流程:

        用户在我们系统登录页面,不用点注册或者登录,而是点击Gitee图标(如下图),跳转到Gitee的登录页,登陆后点击授权按钮,然后获得授权码,然后跳转回我们的系统,,我们用这个授权码获取Token,成功获取到Token后,利用token获取Gitee用户信息,然后,把这个userinfo,注册或者登录我们自己的系统(如果这个社交账号之前没有登陆过,就自动注册并登录,根据Gitee返回的ID判断),

        我们这次的OAuth2,使用授权码方式


       2. 首先我们登录自己的Gitee,在自己的设置页面中,进入第三方应用,并新建一个应用,应用名称和图标,就是我们自己的系统


        3. 创建好应用后,记住自己的Client IDClient Secret,然后还有一个重要的东西是设置好自己的应用回调地址,这个就是用户在gitee点击授权后,Gitee把授权码给我们系统的哪个接口。注意,接口要接收code,

        @RequestParam("code") String code

        4. 现在把gitee小图标放到我们的登录页,img外面包一层a标签,点击后跳转到下面这个地址

          https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code

          注意,把地址中的Client IDredirect_uri替换为,你在第三步设置和获得的值。

        5.现在用户点击图标就会去gitee,登录授权后,就会去我们的回调地址,也就是controller接口,而且会带一个授权码code,这个code,我们就可以拿着去换token,然后拿着Token获取用户信息,然后拿着这个用户信息,我们就可以帮用户登录或者注册了

        这里我把我的回调地址接口贴上来,也把Token和用户信息,映射的实体也贴上来。我把httpUitl这个工具类也贴上来

     1    @GetMapping("/oauth2.0/gitee/success")
     2     public String gitee(@RequestParam("code") String code) throws Exception {
     3         Map<String,String> bodys = new HashMap<>();
     4         bodys.put("grant_type","authorization_code");
     5         bodys.put("code",code);
     6         bodys.put("client_id","你的在第三步获取的client_id");
     7         bodys.put("redirect_uri","你在第三步写的回调地址");
     8         bodys.put("client_secret","你在第三步写的client_secret");
     9         //1.根据code换取GiteeAccessToken;
    10         HttpResponse response_GiteeAccesstoken = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post", new HashMap(), new HashMap(), bodys);
    11 
    12         //处理返回值
    13         if(response_GiteeAccesstoken.getStatusLine().getStatusCode() == 200){
    14             //获取到了GiteeAccessToken
    15             String tokenJson = EntityUtils.toString(response_GiteeAccesstoken.getEntity());
    16             GiteeAccessToken token = JSON.parseObject(tokenJson, GiteeAccessToken.class);
    17             //根据Token,通过查询Gitee Open Api获取用户信息
    18             Map<String,String> query = new HashMap<>();
    19             query.put("access_token", token.getAccess_token());
    20             HttpResponse response_userInfo = HttpUtils.doGet("https://gitee.com", "/api/v5/user", "get", new HashMap(), query);
    21             String giteeUserInfoJson = EntityUtils.toString(response_userInfo.getEntity());
    22             GiteeUserInfo giteeUserInfo = JSON.parseObject(giteeUserInfoJson, GiteeUserInfo.class);
    23 
    24             //登录或者注册这个社交用户
    25             //1.当前用户如果是第一次进入网站,就自动注册(为当前社交用户生成一个会员信息账号,以后这个社交账号就对应指定的会员)
    26 
    27         }else {
    28             return "redirect:http://auth.gulimall.com/login.html";
    29         }
    30 
    31         //2.登录成功后跳回首页
    32         return "redirect:http://gulimall.com";
    33     }

    token的java映射实体

    public class GiteeAccessToken {
    
        private String access_token;
        private String token_type;
        private long expires_in;
        private String refresh_token;
        private String scope;
        private long created_at;
    
    }

    用户信息的映射实体

    public class GiteeUserInfo {
    
        private long id;
    
        private String login;
    
        private String name;
    
        private String avatar_url;
    
        private String url;
    
        private String html_url;
    
        private String followers_url;
    
        private String following_url;
    
        private String gists_url;
    
        private String starred_url;
    
        private String subscriptions_url;
    
        private String organizations_url;
    
        private String repos_url;
    
        private String events_url;
    
        private String received_events_url;
    
        private String type;
    
        private String blog;
    
        private String weibo;
    
        private String bio;
    
        private long public_repos;
    
        private long public_gists;
    
        private long followers;
    
        private long following;
    
        private long stared;
    
        private long watched;
    
        private String created_at;
    
        private String updated_at;
    
        private String email;
    
    }

    HttpUtils工具类

    import org.apache.commons.lang.StringUtils;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpDelete;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.methods.HttpPut;
    import org.apache.http.conn.ClientConnectionManager;
    import org.apache.http.conn.scheme.Scheme;
    import org.apache.http.conn.scheme.SchemeRegistry;
    import org.apache.http.conn.ssl.SSLSocketFactory;
    import org.apache.http.entity.ByteArrayEntity;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    import java.io.UnsupportedEncodingException;
    import java.net.URLEncoder;
    import java.security.KeyManagementException;
    import java.security.NoSuchAlgorithmException;
    import java.security.cert.X509Certificate;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author sunziren
     * @date 2021/8/13 16:10
     */
    public class HttpUtils {
    
        /**
         * get
         *
         * @param host
         * @param path
         * @param method
         * @param headers
         * @param querys
         * @return
         * @throws Exception
         */
        public static HttpResponse doGet(String host, String path, String method,
                                         Map<String, String> headers,
                                         Map<String, String> querys)
                throws Exception {
            HttpClient httpClient = wrapClient(host);
    
            HttpGet request = new HttpGet(buildUrl(host, path, querys));
            for (Map.Entry<String, String> e : headers.entrySet()) {
                request.addHeader(e.getKey(), e.getValue());
            }
    
            return httpClient.execute(request);
        }
    
        /**
         * post form
         *
         * @param host
         * @param path
         * @param method
         * @param headers
         * @param querys
         * @param bodys
         * @return
         * @throws Exception
         */
        public static HttpResponse doPost(String host, String path, String method,
                                          Map<String, String> headers,
                                          Map<String, String> querys,
                                          Map<String, String> bodys)
                throws Exception {
            HttpClient httpClient = wrapClient(host);
    
            HttpPost request = new HttpPost(buildUrl(host, path, querys));
            for (Map.Entry<String, String> e : headers.entrySet()) {
                request.addHeader(e.getKey(), e.getValue());
            }
    
            if (bodys != null) {
                List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
    
                for (String key : bodys.keySet()) {
                    nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
                }
                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
                formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
                request.setEntity(formEntity);
            }
    
            return httpClient.execute(request);
        }
    
        /**
         * Post String
         *
         * @param host
         * @param path
         * @param method
         * @param headers
         * @param querys
         * @param body
         * @return
         * @throws Exception
         */
        public static HttpResponse doPost(String host, String path, String method,
                                          Map<String, String> headers,
                                          Map<String, String> querys,
                                          String body)
                throws Exception {
            HttpClient httpClient = wrapClient(host);
    
            HttpPost request = new HttpPost(buildUrl(host, path, querys));
            for (Map.Entry<String, String> e : headers.entrySet()) {
                request.addHeader(e.getKey(), e.getValue());
            }
    
            if (StringUtils.isNotBlank(body)) {
                request.setEntity(new StringEntity(body, "utf-8"));
            }
    
            return httpClient.execute(request);
        }
    
        /**
         * Post stream
         *
         * @param host
         * @param path
         * @param method
         * @param headers
         * @param querys
         * @param body
         * @return
         * @throws Exception
         */
        public static HttpResponse doPost(String host, String path, String method,
                                          Map<String, String> headers,
                                          Map<String, String> querys,
                                          byte[] body)
                throws Exception {
            HttpClient httpClient = wrapClient(host);
    
            HttpPost request = new HttpPost(buildUrl(host, path, querys));
            for (Map.Entry<String, String> e : headers.entrySet()) {
                request.addHeader(e.getKey(), e.getValue());
            }
    
            if (body != null) {
                request.setEntity(new ByteArrayEntity(body));
            }
    
            return httpClient.execute(request);
        }
    
        /**
         * Put String
         * @param host
         * @param path
         * @param method
         * @param headers
         * @param querys
         * @param body
         * @return
         * @throws Exception
         */
        public static HttpResponse doPut(String host, String path, String method,
                                         Map<String, String> headers,
                                         Map<String, String> querys,
                                         String body)
                throws Exception {
            HttpClient httpClient = wrapClient(host);
    
            HttpPut request = new HttpPut(buildUrl(host, path, querys));
            for (Map.Entry<String, String> e : headers.entrySet()) {
                request.addHeader(e.getKey(), e.getValue());
            }
    
            if (StringUtils.isNotBlank(body)) {
                request.setEntity(new StringEntity(body, "utf-8"));
            }
    
            return httpClient.execute(request);
        }
    
        /**
         * Put stream
         * @param host
         * @param path
         * @param method
         * @param headers
         * @param querys
         * @param body
         * @return
         * @throws Exception
         */
        public static HttpResponse doPut(String host, String path, String method,
                                         Map<String, String> headers,
                                         Map<String, String> querys,
                                         byte[] body)
                throws Exception {
            HttpClient httpClient = wrapClient(host);
    
            HttpPut request = new HttpPut(buildUrl(host, path, querys));
            for (Map.Entry<String, String> e : headers.entrySet()) {
                request.addHeader(e.getKey(), e.getValue());
            }
    
            if (body != null) {
                request.setEntity(new ByteArrayEntity(body));
            }
    
            return httpClient.execute(request);
        }
    
        /**
         * Delete
         *
         * @param host
         * @param path
         * @param method
         * @param headers
         * @param querys
         * @return
         * @throws Exception
         */
        public static HttpResponse doDelete(String host, String path, String method,
                                            Map<String, String> headers,
                                            Map<String, String> querys)
                throws Exception {
            HttpClient httpClient = wrapClient(host);
    
            HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
            for (Map.Entry<String, String> e : headers.entrySet()) {
                request.addHeader(e.getKey(), e.getValue());
            }
    
            return httpClient.execute(request);
        }
    
        private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
            StringBuilder sbUrl = new StringBuilder();
            sbUrl.append(host);
            if (!StringUtils.isBlank(path)) {
                sbUrl.append(path);
            }
            if (null != querys) {
                StringBuilder sbQuery = new StringBuilder();
                for (Map.Entry<String, String> query : querys.entrySet()) {
                    if (0 < sbQuery.length()) {
                        sbQuery.append("&");
                    }
                    if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
                        sbQuery.append(query.getValue());
                    }
                    if (!StringUtils.isBlank(query.getKey())) {
                        sbQuery.append(query.getKey());
                        if (!StringUtils.isBlank(query.getValue())) {
                            sbQuery.append("=");
                            sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
                        }
                    }
                }
                if (0 < sbQuery.length()) {
                    sbUrl.append("?").append(sbQuery);
                }
            }
    
            return sbUrl.toString();
        }
    
        private static HttpClient wrapClient(String host) {
            HttpClient httpClient = new DefaultHttpClient();
            if (host.startsWith("https://")) {
                sslClient(httpClient);
            }
    
            return httpClient;
        }
    
        private static void sslClient(HttpClient httpClient) {
            try {
                SSLContext ctx = SSLContext.getInstance("TLS");
                X509TrustManager tm = new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                    public void checkClientTrusted(X509Certificate[] xcs, String str) {
    
                    }
                    public void checkServerTrusted(X509Certificate[] xcs, String str) {
    
                    }
                };
                ctx.init(null, new TrustManager[] { tm }, null);
                SSLSocketFactory ssf = new SSLSocketFactory(ctx);
                ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                ClientConnectionManager ccm = httpClient.getConnectionManager();
                SchemeRegistry registry = ccm.getSchemeRegistry();
                registry.register(new Scheme("https", 443, ssf));
            } catch (KeyManagementException ex) {
                throw new RuntimeException(ex);
            } catch (NoSuchAlgorithmException ex) {
                throw new RuntimeException(ex);
            }
        }
    
    }

    工具类需要引入的依赖

    <!--  引入HttpUtils 开始 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.15</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.2.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpcore</artifactId>
                <version>4.2.1</version>
            </dependency>
            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>2.6</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-util</artifactId>
                <version>9.3.7.v20160115</version>
            </dependency>
            <!--  引入HttpUtils 结束 -->

     

  • 相关阅读:
    【JVM学习笔记】打破双亲委托机制的例子
    《牛客网》——华为机试在线训练——汽水瓶
    《牛客网》——华为机试在线训练(4-6)
    《牛客网》——华为机试在线训练(1-3)
    《数据结构:邓俊辉版》——二叉树层次遍历
    《数据结构:邓俊辉版》——非递归后序遍历
    《数据结构:邓俊辉版》——非递归中序遍历
    《数据结构:邓俊辉版》——二叉树的非递归先序遍历
    《数据结构:邓俊辉版》——交换排序
    《数据结构:邓俊辉版》——插入排序
  • 原文地址:https://www.cnblogs.com/sunziren/p/15226086.html
Copyright © 2011-2022 走看看