實現百度第三方登陸詳細解答
第一步:前提條件是需要在阿裏雲買個域名,並且買一個服務器。然後將域名解析,和服務器的ip地址綁定。然後需要將服務器備案,別人才能訪問你的網頁。
接下來就是重點看怎麽實現第三方登陸了。。。。。
第二步:登陸百度雲https://developer.baidu.com/如果有賬號就直接登陸,可以手機驗證碼登陸,忘記密碼也不怕。
如果沒有賬號,就註冊一個就行了。
第二步:把鼠標滑動到賬戶名上,會看到應用管理,點擊應用管理。
點擊應用管理後的頁面如下:
然後,點擊創建工程,下面那個是我之前創建過得工程,
在應用名稱裏起一個名字,我就叫“第三方登陸研究”,下面的兩個框框不用管,點擊創建後如圖:
紅色框框圈起來的就是我們後面需要的id和secret值,在JSP和Servlet裏需要使用
然後單擊“安全設置”,會出現下面的頁面,
授權回調頁就是你第三方訪問成功後,進入的頁面,例如我的:http://www.lyxwz.top/venu/BaiDuServlet
根域名綁定:就是你購買的域名
應用服務器ip地址:就是你購買的服務器的ip地址
確認無誤後,點擊確定;
這個時候,需要寫一個項目venu,這個項目的登陸界面需要來一個第三方登陸,
在venu裏的login.jsp 中的代碼中的client_id和redirect_uri改成自己的,client_id 就是我們的API key ,redirect_uri就是我們之前填的授權回調頁。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <script type="text/javascript"> functionbdlogin(){ location.href="https://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=RcMfDQbylVtIvUIhNtNIW0W1&redirect_uri=http://www.lyxwz.top/venu/BaiDuServlet&display=popup"; } </script> <body> <input type="button" value="百度登錄" onclick="bdlogin()"> </body> </html>
首先是我們的自己電腦的瀏覽器向購買服務器發送請求,然後購買的服務器給我們送來了一個登陸界面的信息,
然後我們點擊百度登陸,進入 百度服務器的登陸界面
當我們點擊授權,百度服務器就會核實我們的信息和百度裏面的信息是否相符,如果相符,就會進入我們之前填寫的授權回調頁
我的授權會掉頁裏面的代碼如下:重點:其中下面代碼紅色部分需要根據我們的百度密鑰進行改正,
import java.io.IOException; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; public class BaiDuServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String code = request.getParameter("code"); String redirect_uri = "http://www.lyxwz.top/venu/BaiDuServlet"; String client_secret = "Ri8MRRNMrj3nxGxEHMH04A4dI3qGbzGi"; String client_id = "RcMfDQbylVtIvUIhNtNIW0W1"; String url1 = "https://openapi.baidu.com/oauth/2.0/token?grant_type=authorization_code&code=" + code + "&client_id=" + client_id + "&client_secret=" + client_secret + "&redirect_uri=" + redirect_uri + ""; String content1 = ""; try { // 鍒涘緩涓?涓狧ttpClient瀵矽薄 CloseableHttpClient httpClient = HttpClients.createDefault(); // 鍒涘緩涓?涓狦et璇鋒眰 HttpGet getReq = new HttpGet(url1); getReq.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 "); getReq.addHeader("Accept-Encoding", "gzip, deflate, sdch, br"); getReq.addHeader("Accept-Language", "zh-CN,zh;q=0.8"); getReq.addHeader("Cache-Control", "max-age=0"); getReq.addHeader("Connection", "keep-alive"); getReq.addHeader("Host", "openapi.baidu.com"); getReq.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"); HttpResponse res = httpClient.execute(getReq); HttpEntity entity = res.getEntity(); content1 = EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } Map<String, Object> map = JSON.parseObject(content1, new TypeReference<Map<String, Object>>() { }); String access_token = (String) map.get("access_token"); print(access_token, request, response); } public void print(String access_token,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String content = ""; String url = "https://openapi.baidu.com/rest/2.0/passport/users/getInfo?access_token=" + access_token + ""; try { // 鍒涘緩涓?涓狧ttpClient瀵矽薄 CloseableHttpClient httpClient = HttpClients.createDefault(); // 鍒涘緩涓?涓狦et璇鋒眰 HttpGet getReq = new HttpGet(url); getReq.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 "); getReq.addHeader("Accept-Encoding", "gzip, deflate, sdch, br"); getReq.addHeader("Accept-Language", "zh-CN,zh;q=0.8"); getReq.addHeader("Cache-Control", "max-age=0"); getReq.addHeader("Connection", "keep-alive"); getReq.addHeader("Host", "openapi.baidu.com"); getReq.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"); HttpEntity entity = httpClient.execute(getReq).getEntity(); content = EntityUtils.toString(entity, "UTF-8"); System.out.println(content); } catch (Exception e) { e.printStackTrace(); } Map<String, Object> map = JSON.parseObject(content, new TypeReference<Map<String, Object>>() {}); System.out.println(map); String baiduid = (String) map.get("userid"); System.out.println(baiduid); //List list = JdbcUtils.getList(User.class, "select * from user where baiduid=" + baiduid); // if (list.size() == 0) { request.setAttribute("message", map); request.getRequestDispatcher("/result.jsp").forward(request, response); // } else { // User user = (User) list.get(0); // req.getSession().setAttribute("UserInfo", user); // req.getRequestDispatcher("/success.jsp").forward(req, res); // } } }
然後將這個項目壓縮成war包,復制到購買的服務器上的tommcat裏面的webapps中,點擊lib裏面的startup.bat就發布了 war包也自動解壓了
我們就可以在任何一臺電腦上像登陸百度一樣登陸我們的項目了。
至此,百度第三方登陸完成!!!
實現百度第三方登陸詳細解答