順手貼上CAS 5.2.X官方文檔:https://apereo.github.io/cas/5.2.x/index.html
hugeo的CAS系列:https://blog.csdn.net/u010588262/article/category/7548325
DEMO下載:
part1: https://download.csdn.net/download/u010588262/10327539
part2:https://download.csdn.net/download/u010588262/10372828
只要手頭有用戶名和密碼就可以實現靜默登錄,就是不需要用戶在登錄頁面輸入信息,比如用戶註冊之後,你就知道用戶名和密碼了,就可以用restful的方式根據用戶名和密碼獲得tgt,將tgt塞入cookie即可,大致過程是這樣,下面詳細說一下。
默認情況下CAS服務端校驗用戶身份後會生成tgt
,類似這樣的字符串:
TGT-1-e9OqKGuTlqMHrLgneGXYLc0fUTyz5DKZ09WpXjtochS-wohnm2syP4mZH2VAwLZ1nEI-LAPTOP-UVV0Q09C
然後使用DefaultCasCookieValueManager
將tgt
加上客戶端IP
、客戶端代理信息
後再加密,形成一個複雜的很長的串兒寫入用戶瀏覽器的cookie中取名TGC
,這樣用戶每次訪問服務端校驗時會自動帶着TGC
,服務端收到了就解密校驗了。
所以想要默默地單點登錄就要默默地生成TGC
放在cookie中。 兩種方式實現
推薦方式
注意:此處假設你看過我之前的幾篇博客了,或者說對CAS比較瞭解了的情況下,不會做詳細解釋
通過此方法,登錄流程爲:
1. 用戶註冊成功後,代碼通過rest方式調服務端接口獲得TGT(不知道怎麼獲取的請看下之前有一篇詳細說rest接口驗證的博客)
2. 302跳轉到服務端/setcookie接口,這個接口需要我們自己開發,將用戶要訪問的客戶端地址serviceUrl和第一步獲得tgt傳過去,服務端校驗tgt並生成tgc放入cookie
3. 服務端302跳轉到serviceURL,用戶登錄成功
具體方法:
新增生成cookie的類:
package com.hugeo.cas;
import org.apereo.cas.CipherExecutor;
import org.apereo.cas.web.support.CookieRetrievingCookieGenerator;
import org.apereo.cas.web.support.CookieValueManager;
import org.apereo.cas.web.support.DefaultCasCookieValueManager;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.CookieGenerator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HugeoCookieGenerator extends CookieGenerator {
CookieValueManager cookieValueManager;
public HugeoCookieGenerator(CookieRetrievingCookieGenerator generator,CipherExecutor executor) {
super.setCookieName(generator.getCookieName());
super.setCookiePath(generator.getCookiePath());
this.setCookieDomain(generator.getCookieDomain());
super.setCookieMaxAge(generator.getCookieMaxAge());
super.setCookieSecure(generator.isCookieSecure());
super.setCookieHttpOnly(generator.isCookieHttpOnly());
cookieValueManager = new DefaultCasCookieValueManager(executor);
}
public void addCookie(String tgt) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
HttpServletResponse response = attributes.getResponse();
String theCookieValue = this.cookieValueManager.buildCookieValue(tgt, request);
super.addCookie(response, theCookieValue);
}
}
隨便找個configuration類把類定義爲bean
@Autowired
CipherExecutor cookieCipherExecutor;
@Autowired
@Qualifier("ticketGrantingTicketCookieGenerator")
private CookieRetrievingCookieGenerator generator;
@Bean
HugeoCookieGenerator hugeoCookieGenerator(){
return new HugeoCookieGenerator(generator, cookieCipherExecutor);
}
自己寫個controller通過tgt生成cookie
@Autowired
@Qualifier("centralAuthenticationService")
private CentralAuthenticationService centralAuthenticationService;
@Autowired
HugeoCookieGenerator hugeoCookieGenerator;
@GetMapping("/setcookie")
public String setCookie(@RequestParam("service") String service, @RequestParam("tgt") String tgt) {
try {
// 校驗
Ticket ticket = this.centralAuthenticationService.getTicket(tgt, Ticket.class);
if (ticket == null || ticket.isExpired()) {
// 無效tgt,跳轉到登錄
return "redirect:/login";
}
// 添加cookie
hugeoCookieGenerator.addCookie(tgt);
// 跳轉到客戶端
return "redirect:" + service;
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/login";
}
簡單方式(安全性有降低)
之前的restful博客中寫了怎麼通過接口獲得tgt
,現在問題是怎麼通過tgt
獲得tgc
呢,網上查資料發現tgt
除了默認的加密生成方式外,還有一種就是NoOpCookieValueManager
,這種方式就是直接返回tgt
,也就是說tgc
和tgt
是一模一樣的,CAS是這樣判斷的:
關鍵詞:properties,tgc,crypto
所以到官方文檔中找相應的配置,把tgc的crypto關閉
https://apereo.github.io/cas/5.2.x/installation/Configuration-Properties.html
需要翻一下wall
搜索tgc很容易就找到了:
所以我們在配置文件application.properties
中加上:
# 不要加密tgt
cas.tgc.crypto.enabled=false
但是這樣做有弊端就是安全性有所降低,之前加密的方式使得tgt
與客戶端ip
和agent
綁定,而現在不加密的方式使得生成的tgt可以被任何客戶端和IP使用。當然這也是最簡單的方法。
然後我開始測試了,測試思路是:
1. 使用postman調接口獲得tgt。相當於是用戶註冊成功後,你用代碼調接口獲得tgt
2. 把tgt放到cookie中。我此處是直接在chrome瀏覽器中通過edit this cookie插件實現的,相當於在代碼中setCookie,注意要設置cookie的domain爲CAS服務器端的地址,這樣客戶端重定向到服務端進行校驗時纔會帶着這個cookie。
此時訪問客戶端即爲登錄狀態了
此篇參考了文章:https://blog.csdn.net/cn_yh/article/details/77962467