首页 > 代码库 > 第三方登录功能的实现

第三方登录功能的实现

刚开始做的时候感觉高大上
为什么要使用第三方登录:

一般稍微作为一个大点的项目,为了提高用户的群体都会做第三方登录(如:QQ,微信,新浪等)

在往下看之前先注册第三方网站的开发者账号,创建应用完成审核。

  1. QQ : QQ开发者平台
  2. 微信: 微信开发者平台
  3. 新浪: 新浪开放平台

要实现第三方登录的功能首先要明白oauth2.0的认证原理,由于第三方登录的认证授权流程大致都是一样的,在这里我只讲QQ的登录授权流程:

官网也有介绍:
这里写链接内容

其授权验证流程示意图如下:
技术分享

  1. client先访问:PC网站:https://graph.qq.com/oauth2.0/authorize?
    参数有: response_type=code 固定
    client_id 申请应用时分配的id
    redirect_uri 回调的url 你自己网站的一个地址

  2. 通过第一步会返回 code码(注意:此code会在10分钟内过期)

  3. 通过Authorization Code获取Access Token(通过地址:https://graph.qq.com/oauth2.0/token?)
    此时参数有:
    grant_type
    client_id
    client_secret: 申请QQ登录成功后,分配给网站的appkey。
    code: 上一步返回的authorization code。
    redirect_uri

  4. 此刻你已经拿到了Access Token , 然后就是获取用户OpenID_OAuth2.0
    https://graph.qq.com/oauth2.0/me?access_token=获取到的Access Token
  5. 根据第4步你会拿到(openid): callback( {“client_id”:”YOUR_APPID”,”openid”:”YOUR_OPENID”} );
  6. 最后就是回去你的用户信息了:
    https://graph.qq.com/user/get_user_info?access_token=YOUR_ACCESS_TOKEN&oauth_consumer_key=YOUR_APP_ID&openid=YOUR_OPENID

返回结果:

{
“ret”:0,
“msg”:”“,
“nickname”:”YOUR_NICK_NAME”,

}
下面直接看代码(控制层):

/**
 * 
 * 首页
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/qqLogin")
public class HomePageController {

    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response, HttpSession session)
            throws IOException {
        try {
            AccessToken accessTokenObj = (new Oauth())
                    .getAccessTokenByRequest(request);
            String accessToken = null, openID = null;
            if (accessTokenObj.getAccessToken().equals("")) {
                System.out.print("没有获取到响应参数");
                return "error";
            } else {
                accessToken = accessTokenObj.getAccessToken();
                session.setAttribute("accessToken",
                        accessToken);
                // 利用获取到的accessToken 去获取当前用的openid 
                OpenID openIDObj = new OpenID(accessToken);
                openID = openIDObj.getUserOpenID();
                session.setAttribute("openID", openID);
                return "success";
            }
        } catch (QQConnectException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "error";
        }
    }

    @RequestMapping("/homePage")
    public String homePage(Model model) {

        return "index";
    }

    @RequestMapping("/inQQ")
    public void inQQ(Model model, HttpServletResponse response,
            HttpServletRequest request) {
        try {
            response.sendRedirect(new Oauth().getAuthorizeURL(request));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (QQConnectException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

视图层:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- <%@include file="/inc/taglib.jsp"%> --%>
<body>
<h2>Hello World!ss</h2>

    <a href="${pageContext.request.contextPath} /qqLogin/inQQ">请使用你的QQ账号登陆</a>

</body>

记得别忘了导包
解开来见证奇迹的时刻到了:
技术分享
技术分享
技术分享

至此结束!!!!!!!!

有需要源码的留下qq邮箱

<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

    第三方登录功能的实现