社交登入
社交登入(微博)
・
・
OAuth2.0
OAuth: OAuth(開放授權)是一個開放標準,允許使用者授權第三方網站訪問他們儲存在另外的服務提供者上的資訊,而不需要將使用者名稱和密碼提供給第三方網站或分享他們資料的所有內容。
OAuth2.0: 對於使用者相關的OpenAPI(例如獲取使用者資訊,動態同步,照片,日誌,分享等),為了保護使用者資料的安全和隱私,第三方網站訪問使用者資料前都需要顯式的向使用者徵求授權。
登入流程示意圖:
・
・
・
・
(A)使用者開啟客戶端以後,客戶端要求使用者給予授權。
(B)使用者同意給予客戶端授權。
(C)客戶端使用上一步獲得的授權,向認證伺服器申請令牌。
(D)認證伺服器對客戶端進行認證以後,確認無誤,同意發放令牌。
(E)客戶端使用令牌,向資源伺服器申請獲取資源。
(F)資源伺服器確認令牌無誤,同意向客戶端開放資源。
・
・
微博登陸準備工作
・
1、進入微博開放平臺
・
・
2、登陸微博,進入微連線,選擇網站接入
・
・
3、選擇立即接入
・
・
4、建立自己的應用
・
・
5、我們可以在開發階段進行測試,記住自己的app key和app secret我們一會兒用。
・
・
6、進入高階資訊,填寫授權回撥頁的地址(一個授權成功,訪問我們專案的地址,一個授權失敗訪問我們專案的地址)
・
・
微博登陸測試
・
・
1、引導使用者到如下地址
https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI
・
YOUR_CLIENT_ID : 就是app key,YOUR_REGISTERED_REDIRECT_URI: 就是返回成功的uri http://gulimall.com/success。
・
・
2、使用者登入成功返回成功頁面帶著code,我們用code去換取token令牌
・
・
・
3、編寫Controller來接收並處理code
・
・
(1)引入HttpUtils工具類
・
pom
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>9.3.7.v20160115</version>
</dependency>
HttpUtils.class
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpUtils {
/**
* get
*
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @return
* @throws Exception
*/
public static HttpResponse doGet(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpGet request = new HttpGet(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
return httpClient.execute(request);
}
/**
* post form
*
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param bodys
* @return
* @throws Exception
*/
public static HttpResponse doPost(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys,
Map<String, String> bodys)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPost request = new HttpPost(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
if (bodys != null) {
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
for (String key : bodys.keySet()) {
nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
request.setEntity(formEntity);
}
return httpClient.execute(request);
}
/**
* Post String
*
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param body
* @return
* @throws Exception
*/
public static HttpResponse doPost(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys,
String body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPost request = new HttpPost(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
if (StringUtils.isNotBlank(body)) {
request.setEntity(new StringEntity(body, "utf-8"));
}
return httpClient.execute(request);
}
/**
* Post stream
*
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param body
* @return
* @throws Exception
*/
public static HttpResponse doPost(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys,
byte[] body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPost request = new HttpPost(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
if (body != null) {
request.setEntity(new ByteArrayEntity(body));
}
return httpClient.execute(request);
}
/**
* Put String
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param body
* @return
* @throws Exception
*/
public static HttpResponse doPut(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys,
String body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPut request = new HttpPut(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
if (StringUtils.isNotBlank(body)) {
request.setEntity(new StringEntity(body, "utf-8"));
}
return httpClient.execute(request);
}
/**
* Put stream
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param body
* @return
* @throws Exception
*/
public static HttpResponse doPut(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys,
byte[] body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPut request = new HttpPut(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
if (body != null) {
request.setEntity(new ByteArrayEntity(body));
}
return httpClient.execute(request);
}
/**
* Delete
*
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @return
* @throws Exception
*/
public static HttpResponse doDelete(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
return httpClient.execute(request);
}
private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
StringBuilder sbUrl = new StringBuilder();
sbUrl.append(host);
if (!StringUtils.isBlank(path)) {
sbUrl.append(path);
}
if (null != querys) {
StringBuilder sbQuery = new StringBuilder();
for (Map.Entry<String, String> query : querys.entrySet()) {
if (0 < sbQuery.length()) {
sbQuery.append("&");
}
if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
sbQuery.append(query.getValue());
}
if (!StringUtils.isBlank(query.getKey())) {
sbQuery.append(query.getKey());
if (!StringUtils.isBlank(query.getValue())) {
sbQuery.append("=");
sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
}
}
}
if (0 < sbQuery.length()) {
sbUrl.append("?").append(sbQuery);
}
}
return sbUrl.toString();
}
private static HttpClient wrapClient(String host) {
HttpClient httpClient = new DefaultHttpClient();
if (host.startsWith("https://")) {
sslClient(httpClient);
}
return httpClient;
}
private static void sslClient(HttpClient httpClient) {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] xcs, String str) {
}
public void checkServerTrusted(X509Certificate[] xcs, String str) {
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = httpClient.getConnectionManager();
SchemeRegistry registry = ccm.getSchemeRegistry();
registry.register(new Scheme("https", 443, ssf));
} catch (KeyManagementException ex) {
throw new RuntimeException(ex);
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException(ex);
}
}
}
(2)編寫Controller
・
@GetMapping("/success")
public String weibo(@RequestParam("code")String code) throws Exception {
Map<String, String> map = new HashMap<>();
map.put("client_id","143199348");//App Key
map.put("client_secret","17204275a4023c81b61eb270b2455890");//App Secret
map.put("grant_type","authorization_code");
map.put("redirect_uri","http://auth.gulimall.com/success");//返回成功頁面
map.put("code",code);
//1、根據使用者授權返回的code換取access_token
HttpResponse response = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token",
"post", new HashMap<>(), map, new HashMap<>());
//2、處理
if(response.getStatusLine().getStatusCode()==200){
//獲取到了access_token
String json = EntityUtils.toString(response.getEntity());
SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
//知道當前是那個社交使用者(進行關聯為當前社交使用者生成一個會員資訊帳號)
R r = memberFeignService.oauthLogin(socialUser);//遠端呼叫Member服務進行查詢或註冊使用者
if(r.getCode()==0){
MemverRespVo data = r.getData("data", new TypeReference<MemverRespVo>() {
});
//認證登入成功
return "redirect:http://gulimall.com";
}else {
return "redirect:http://auth.gulimall.com/login.html";
}
}else {
return "redirect:http://auth.gulimall.com/login.html";
}
}
}
相關文章
- Spring Security原始碼分析六:Spring Social社交登入原始碼解析Spring原始碼
- 【網頁登入】QQ 登入、微信登入、微博登入、GitHub 登入網頁Github
- Spring Security原始碼分析十四:Spring Social社交登入繫結與解綁Spring原始碼
- Spring Security系列之Spring Social社交登入的繫結與解綁(十五)Spring
- 鴻蒙 Next 社交應用中的安全登入與密碼管理實戰鴻蒙密碼
- uniapp 完成兩種方式登入 驗證碼登入 密碼登入APP密碼
- [API 寫法] QQ 登入、微信登入、Facebook、google、蘋果登入APIGo蘋果
- 掃碼登入是這樣登入的
- linux 免登入以及配置別名登入Linux
- flask 登入Flask
- 谷歌登入谷歌
- 登入功能
- token 登入
- JavaScript登入JavaScript
- Ant design pro使用(五):未登入時自動跳轉到登入頁,登入之後不再返回登入頁
- springmvc入門登入功能SpringMVC
- 帝國cms登入後臺提示“登入成功”,接著又提示“您還未登入”
- vnc登入工具,好用的vnc登入工具,具體登入vnc客戶端使用教程VNC客戶端
- 聊聊“密碼登入”、“手機快捷登入”和“第三方聯合登入”密碼
- 密碼登入密碼
- TCP合法登入TCP
- Flask——登入、退出Flask
- Oracle登入命令Oracle
- 單點登入
- laravel 登入失效Laravel
- 微信登入
- 登入頁面
- vnc登入,vnc遠端登入工具的使用方法VNC
- vnc批次登入,2種VNC批次登入Linux的方法VNCLinux
- vnc批次登入,vnc批次登入Linux的方法介紹VNCLinux
- Jmeter 登入使用了 jsencrypt 加密密碼的登入介面JMeterJS加密密碼
- unbuntu16.04 伺服器的 免密登入、秘鑰登入和禁止密碼登入 配置伺服器密碼
- unbuntu16.04 伺服器的 免密登入、祕鑰登入和禁止密碼登入 配置伺服器密碼
- 【轉】linux設定金鑰登入(只允許金鑰登入)Linux
- Luffy /4/ 多方式登入介面&登入註冊前端頁面前端
- Auth Session 退出他人正登入的賬號、passport 退出登入SessionPassport
- 為爬蟲獲取登入cookies:登入的恩恩怨怨爬蟲Cookie
- FTP登入時一直彈出登入視窗,就算輸入正確的賬號密碼也不能登入FTP密碼