【用户授权设计】java第三方登录(微博,QQ)详细代码

本文详细介绍了使用Java实现第三方登录,如微博和QQ的完整流程,包括获取code、access_token和用户信息的步骤。通过点击登录按钮,用户授权后将跳转至指定回调页面,实现安全登录。
摘要由CSDN通过智能技术生成

第三方登录流程是:先获取code---->然后获取access_token----->根据token获取用户信息。

前台页面实现步骤:点击微博登录按钮---->打开一个子窗口,进行授权------>授权完成,跳转到首页或上次浏览的页面。

1、写第三方登录的按钮,点击按钮时,打开一个子窗口。

redirect_uri是你在微博上设置的回调地址。

复制代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <script type="text/javascript" src="http://statics.2cto.com/js/jquery.min.js"></script>
  </head>
  <script type="text/javascript">
  
      var qqAuthWin,weiboAuthWin;
      
      /**
     * 关闭QQ子窗口
     */
    function closeQQWin(){
        var result = $("#qq").val();
        if(result != ""){
            console.log(result);
            qqAuthWin.close();
        }else{
            console.log("值为空");
        }
    }
      
    /**
     * QQ登录
     * http://localhost:9090/logback/qq.jsp  QQ互联上设置的回调地址
     */
      function loginQQ(){
          qqAuthWin = window.open("https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=CLIENT_ID&state=register&redirect_uri=http://localhost:9090/logback/qq.jsp",
                    'QQ授权登录','width=770,height=600,menubar=0,scrollbars=1,'+
                   'resizable=1,status=1,titlebar=0,toolbar=0,location=1');
      }
    
      /**
     * 关闭微博子窗口
     */
    function closeWeiboWin(){
        var result = $("#weibo").val();
        if(result != ""){
            console.log(result);
            weiboAuthWin.close();
        }else{
            console.log("值为空");
        }
    }
    
      /**
     * 微博登录
     * http://localhost:9090/logback/weibo.jsp这个就是在微博上设置的回调地址
     */
      function loginWeibo(){
          weiboAuthWin = window.open("https://api.weibo.com/oauth2/authorize?client_id=CLIENT_ID&response_type=code&state=register&redirect_uri=http://localhost:9090/logback/weibo.jsp",
                    '微博授权登录','width=770,height=600,menubar=0,scrollbars=1,'+
           'resizable=1,status=1,titlebar=0,toolbar=0,location=1');
      }
  </script>
  <body>
      <input type="hidden" id="qq" value="">
    <a  href="#" onClick="loginQQ()">QQ登录</a>
    
    <br><br>
    <hr>
    <br>
    
    <input type="hidden" id="weibo" value="">
    <a href="#" onClick="loginWeibo()">微博登录</a>
  </body>
</html>
复制代码

2、回调地址页(qq.jsp、weibo.jsp)

复制代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

String code = request.getParameter("code");//获取QQ返回的code
String state = request.getParameter("state");
%>
<!DOCTYPE HTML>
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'weibo.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <script type="text/javascript" src="http://statics.2cto.com/js/jquery.min.js"></script>
    <script>
        $(function(){
                var code = "<%=code%>";
                var state = "<%=state%>";
                $.ajax({
                    url:"http://localhost:8080/cms_manage/api/qqLogin",
                    type:"post",
                    data:{code:code,state:state},
                    dataType:"json",
                    success:function(result){
                        result = JSON.stringify(result);
                        console.log(result);
                        //把返回的数据传给父窗口的隐藏域中
                        window.opener.document.getElementById("qq").value = result;
                        //授权完成后,关闭子窗口
                        window.opener.closeQQWin();
                    }
                });
        });
    </script>
  </head>
  
  <body>
        登录成功。
  </body>
</html>
复制代码

qq.jsp和weibo,jsp是一样的。。。

3、java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值