SSO CAS

一、單點登錄與CAS協議

  1. SSO(概念)
    目前比較流行的企業業務整合的解決方案之一,SSO使得在多個應用系統中,用戶只需要登錄一次就可以訪問所有相互信任的應用系統。

  2. CAS(實現)
    定義:開源的企業級單點登錄解決方案。
    CAS框架:CAS(Central Authentication Service)是實現SSO單點登錄的框架。
    CAS Server爲需要獨立部署的Web應用;CAS Client支持非常多的客戶端。

訪問服務 -> 丁香認證 -> 用戶認證 -> 發放票據 -> 驗證票據 -> 傳遞用戶信息

  • 名次
    Ticket Granting ticket(TGT):可以認爲是CAS Server根據用戶名密碼生成的一張票,存在server端。
    Ticket-granting cookie(TGC) :其實就是一個cookie,存放用戶身份信息,由server發給client端。
    Service ticket(ST):由TGT生成的一次性票據,用於驗證,只能用一次。相當於server發給client一張票,然後client拿着這個票再來找server驗證,看看是不是server簽發的。

CAS過程
搭建方法


二、搭建Tomcat-CAS——CAS Server端

Step 1. 步驟一:配置Tomcat
下載Tomcat(建議8以上版本)

//複製war包進入web-apps,運行Tomcat
sudo sh startup.sh
sh shutdown.sh

1.修改端口號
(1) tomcat-config-service.xml中修改爲9100(protocol=“HTTP/1.1”)
(2) cas-WEB-INF-cas.properties中修改爲9100(server.name=http://localhost:9100)

2.去除https認證(secure)
避開SSL證書,方便測試和編碼過程。
(1) cas-WEB-INF-deployerConfigContext.xml中修改requireSecure

<bean id="proxyAuthenticationHandler" class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler" p:httpClient-ref="httpClient" p:requireSecure="false"/>

(2) cas-WEB-INF-spring-configuration-ticketGrantingTicketCookieGenerator.xml中修改

p:cookieSecure="false"
p:cookieMaxAge="3600" <!-- cookie有效時間爲3600s -->

(3) cas-WEB-INF-spring-configuration-warnCookieGenerator.xml中修改

p:cookieSecure="false"
p:cookieMaxAge="3600"

3.允許退出登錄後重定向到其他頁面(…?service=…)
cas-WEB-INF-spring-configuration-warnCookieGenerator.xml中修改

 <bean id="logoutAction" class="org.jasig.cas.web.flow.LogoutAction" p:servicesManager-ref="servicesManager" p:followServiceRedirects="${cas.logout.followServiceRedirects:true}"/>

三、代碼實現——CAS Client端

導入相應的依賴:

<dependency>
    <groupId>net.unicon.cas</groupId>
    <artifactId>cas-client-autoconfig-support</artifactId>
    <version>1.4.0-GA</version>
</dependency>

application.properties配置:

server.port=9001

cas.server-url-prefix=http://cas.server.com:8443/cas
cas.server-login-url=http://cas.server.com:8443/cas/login
cas.client-host-url=http://cas.client1.com:9001
cas.use-session=true
cas.validation-type=cas

#自定義的退出url,退出成功後跳轉到 http://cas.client1.com:9001/logout/success
casClientLogoutUrl=http://cas.server.com:8443/cas/logout?service=http://cas.client1.com:9001/logout/success

Springboot 啓動類添加標籤@EnableCasClient,啓用CAS Client。
自定義Controller類,添加login和logout邏輯。

Config類需要配置忽略授權的URL:

@Configuration
public class CASConfig {
    @Value("${cas.server-url-prefix}")
    private String serverUrlPrefix;
    @Value("${cas.server-login-url}")
    private String serverLoginUrl;
    @Value("${cas.client-host-url}")
    private String clientHostUrl;
 
    /**
     * 授權過濾器,添加一個id爲filterAuthenticationRegistration的bean標籤
     * @return
     */
    @Bean
    public FilterRegistrationBean filterAuthenticationRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new AuthenticationFilter());
        // 設定匹配的路徑
        registration.addUrlPatterns("/*");
        Map<String,String> initParameters = new HashMap<String, String>();
        initParameters.put("casServerLoginUrl", serverUrlPrefix);
        initParameters.put("serverName", clientHostUrl);
        //忽略的url,"|"分隔多個url
        initParameters.put("ignorePattern", "/logout/success|/index.html|.xml|/filename/*");
        registration.setInitParameters(initParameters);
        // 設定加載的順序
        registration.setOrder(1);
        return registration;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章