以github为例,简要介绍OAuth第三方登录流程。
参考:

GitHub OAuth 第三方登录示例教程

流程

github第三方登录示例 - 图1

代码

web

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <title>Node OAuth2 Demo</title>
  7. <meta name="viewport" content="width=device-width, initial-scale=1">
  8. </head>
  9. <body>
  10. <a id="login">Login with GitHub</a>
  11. <script>
  12. // fill in your cliend_id
  13. const client_id = '2d6ab4268ae36f01a7a4';
  14. const authorize_uri = 'https://github.com/login/oauth/authorize';
  15. const redirect_uri = 'http://localhost:8080/oauth/redirect';
  16. const link = document.getElementById('login');
  17. link.href = `${authorize_uri}?client_id=${client_id}&redirect_uri=${redirect_uri}`;
  18. </script>
  19. </body>
  20. </html>

本地接口

  1. /**
  2. * @Description
  3. * @Author 田云
  4. * @Date 2019/6/4 16:17
  5. * @Version 1.0
  6. */
  7. @RestController
  8. @RequestMapping("oauth")
  9. public class Login {
  10. private String client_id = "XXX";
  11. private String client_secret = "XXX";
  12. @GetMapping("redirect")
  13. public String login(String code) {
  14. String me = CommonUtil.sendPost
  15. ("https://github.com/login/oauth/access_token?" +
  16. "client_id=" + client_id +
  17. "&client_secret=" + client_secret +
  18. "&code=" + code,
  19. null);
  20. String atoke = me.split("&")[0];
  21. String res = CommonUtil.sendGet("https://api.github.com/user?" + atoke + "");
  22. return res;
  23. }
  24. }

工具类参考

  1. /**
  2. * @Description
  3. * @Author 田云
  4. * @Date 2019/6/4 16:56
  5. * @Version 1.0
  6. */
  7. public class CommonUtil {
  8. /**
  9. * 向指定 URL 发送POST方法的请求
  10. *
  11. * @param url
  12. * 发送请求的 URL
  13. * @param param
  14. * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  15. * @return 所代表远程资源的响应结果
  16. */
  17. public static String sendPost(String url, String param) {
  18. PrintWriter out = null;
  19. BufferedReader in = null;
  20. String result = "";
  21. try {
  22. URL realUrl = new URL(url);
  23. // 打开和URL之间的连接
  24. URLConnection conn = realUrl.openConnection();
  25. // 设置通用的请求属性
  26. conn.setRequestProperty("accept", "*/*");
  27. conn.setRequestProperty("connection", "Keep-Alive");
  28. conn.setRequestProperty("user-agent",
  29. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  30. // 发送POST请求必须设置如下两行
  31. conn.setDoOutput(true);
  32. conn.setDoInput(true);
  33. conn.connect();
  34. // 获取URLConnection对象对应的输出流
  35. out = new PrintWriter(conn.getOutputStream());
  36. // 发送请求参数
  37. out.print(param);
  38. // flush输出流的缓冲
  39. out.flush();
  40. // 定义BufferedReader输入流来读取URL的响应
  41. InputStream instream = conn.getInputStream();
  42. if(instream!=null){
  43. in = new BufferedReader( new InputStreamReader(instream));
  44. String line;
  45. while ((line = in.readLine()) != null) {
  46. result += line;
  47. }
  48. }
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. //使用finally块来关闭输出流、输入流
  53. finally{
  54. try{
  55. if(out!=null){
  56. out.close();
  57. }
  58. if(in!=null){
  59. in.close();
  60. }
  61. }
  62. catch(IOException ex){
  63. ex.printStackTrace();
  64. }
  65. }
  66. return result;
  67. }
  68. /**
  69. * 发起http请求获取返回结果
  70. * @param req_url 请求地址
  71. * @return
  72. */
  73. public static String sendGet(String req_url) {
  74. StringBuffer buffer = new StringBuffer();
  75. try {
  76. URL url = new URL(req_url);
  77. HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
  78. httpUrlConn.setDoOutput(false);
  79. httpUrlConn.setDoInput(true);
  80. httpUrlConn.setUseCaches(false);
  81. httpUrlConn.setRequestMethod("GET");
  82. httpUrlConn.connect();
  83. // 将返回的输入流转换成字符串
  84. InputStream inputStream = httpUrlConn.getInputStream();
  85. InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
  86. BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
  87. String str = null;
  88. while ((str = bufferedReader.readLine()) != null) {
  89. buffer.append(str);
  90. }
  91. //res = new String(buffer.toString().getBytes("iso-8859-1"),"utf-8");
  92. bufferedReader.close();
  93. inputStreamReader.close();
  94. // 释放资源
  95. inputStream.close();
  96. inputStream = null;
  97. httpUrlConn.disconnect();
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100. }
  101. return buffer.toString();
  102. }
  103. }