1.首先我們進入controller層login方法跳轉login頁面
@RequestMapping("login")
public ModelAndView login(){
ModelAndView mv = new ModelAndView();
mv.setViewName("../login");
return mv;
}
2.然後進入login頁面
<%@ 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"> function tolog(){ location='http://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=IDIhE9Fxo2AGexqEmIl3dBO7&redirect_uri=http://localhost:8080/baidu'; } </script> <body> <center> <button οnclick="tolog()">登錄</button> </center> </body> </html>
3.通過鏈接去訪問baidu,並帶參數,通過client_id和redirect_url獲取code,並通過回調url進入controller中的baidu方法
@RequestMapping("baidu")
public ModelAndView baidu(Code c) throws IOException {
String code = c.getCode();
System.out.println(code);
HashMap<String, String> map = new HashMap<String,String>();
map.put("grant_type", "authorization_code");
map.put("code", code);
map.put("client_id", "IDIhE9Fxo2AGexqEmIl3dBO7");
map.put("client_secret", "O2Q6zS6sWYks1tTHKd65TslbsyVXDSgx");
map.put("redirect_uri", "http://localhost:8080/baidu");
String json = HttpUtils.postForm("https://openapi.baidu.com/oauth/2.0/token", map);
System.out.println(json);
ObjectMapper mapper = new ObjectMapper();
BToken token = mapper.readValue(json, BToken.class);
String access_token = token.getAccess_token();
String json2 = HttpUtils.get("https://openapi.baidu.com/rest/2.0/passport/users/getLoggedInUser?access_token=" + access_token);
System.out.println(json2);
ObjectMapper mapper2 = new ObjectMapper();
User user = mapper2.readValue(json2, User.class);
user.setUname(new String(user.getUname().getBytes(),"utf-8"));
ModelAndView mv = new ModelAndView();
mv.addObject("user",user);
mv.setViewName("success");
return mv;
}
4.通過code及其他參數可以獲取Access_Token,通過Access_Token可以獲取用戶信息
然後跳轉到成功頁面
<%@ 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>
<body>
<center>
<img src="http://tb.himg.baidu.com/sys/portrait/item/${user.portrait }"><br>
<h3>${user.uid }</h3>
<h3>${user.uname }</h3>
</center>
</body>
</html>
5.pojo(附代碼)
5.1 User類
package com.jk.pojo;
import java.io.Serializable;
public class User implements Serializable{
private String uid;
private String uname;
private String portrait;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPortrait() {
return portrait;
}
public void setPortrait(String portrait) {
this.portrait = portrait;
}
}
5.2 Code類
package com.jk.pojo;
import java.io.Serializable;
public class Code implements Serializable{
private String code;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
5.3 BToken類
package com.jk.pojo;
import java.io.Serializable;
public class BToken implements Serializable{
private Integer expires_in;
private String refresh_token;
private String access_token;
private String session_secret;
private String session_key;
private String scope;
private String error_code;
private String error_msg;
public String getError_msg() {
return error_msg;
}
public void setError_msg(String error_msg) {
this.error_msg = error_msg;
}
public String getError_code() {
return error_code;
}
public void setError_code(String error_code) {
this.error_code = error_code;
}
public Integer getExpires_in() {
return expires_in;
}
public void setExpires_in(Integer expires_in) {
this.expires_in = expires_in;
}
public String getRefresh_token() {
return refresh_token;
}
public void setRefresh_token(String refresh_token) {
this.refresh_token = refresh_token;
}
public String getSession_secret() {
return session_secret;
}
public void setSession_secret(String session_secret) {
this.session_secret = session_secret;
}
public String getSession_key() {
return session_key;
}
public void setSession_key(String session_key) {
this.session_key = session_key;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
}