摘要:所以客戶端的集成主要是單點(diǎn)登錄的集成,客戶端指定需要做安全認(rèn)證的頁(yè)面,然后的安全包檢測(cè)校驗(yàn)用戶登錄情況,并自動(dòng)與登錄頁(yè)面進(jìn)行跳轉(zhuǎn)交互。提供了很多配置的方式,有,,以及其他可查官網(wǎng)。但高度自由的一如既往的,沒有提供可視化操作的界面。
前兩篇介紹了Apereo CAS以及服務(wù)器端的安裝,但還不夠完整,服務(wù)端還沒有Application真正用起來呢!這篇文章將介紹怎么用起來集成的目的
客戶端我們想要與Apereo CAS做什么集成呢?回顧一下Apereo CAS是做什么的?Apereo CAS的一個(gè)功能就是單點(diǎn)登錄,統(tǒng)一的登錄登出接口與頁(yè)面,讓系統(tǒng)中的模塊只需要關(guān)注在業(yè)務(wù)點(diǎn),而把安全認(rèn)證的功能交給統(tǒng)一認(rèn)證來做。所以客戶端的集成主要是單點(diǎn)登錄的集成,客戶端指定需要做安全認(rèn)證的頁(yè)面,然后Apereo CAS的安全包檢測(cè)校驗(yàn)用戶登錄情況,并自動(dòng)與CAS登錄頁(yè)面進(jìn)行跳轉(zhuǎn)交互。
客戶端的配置Apereo CAS提供了Springboot的包,可以讓我們的集成些微方便了那么一丟丟!首先我們創(chuàng)建一個(gè)Springboot的application,里面帶了Apereo CAS start的依賴
org.springframework.security spring-security-cas
同時(shí)在application.properties文件里面指定啟動(dòng)的端口 server.port = 9000
有了Apereo CAS的包之后,我們就可以進(jìn)行代碼的配置??蛻舳说呐渲冒凑誗pringSecurity的安全檢驗(yàn)流程進(jìn)行的:
用戶嘗試打開一個(gè)受保護(hù)的url,比如/admin/user
AuthenticationEntryPoint被觸發(fā)了,把用戶重定向到配置好的CAS登錄頁(yè)面https://localhost:6443/cas
用戶輸入用戶名密碼,登錄成功后, CAS會(huì)跳轉(zhuǎn)回application指定的回調(diào)url http://localhost:9000/login/cas, 并帶上ticket作為查詢參數(shù)
CasAuthenticationFilter一直在監(jiān)聽/login/cas這個(gè)路徑,當(dāng)發(fā)現(xiàn)有請(qǐng)求后,它會(huì)觸發(fā)CasTicketValidator,由CasTickerValidator檢驗(yàn)ticket的有效性
當(dāng)ticket也驗(yàn)證成功后,用戶將會(huì)被跳轉(zhuǎn)回原來請(qǐng)求的受保護(hù)url
下面代碼大致描述了這個(gè)過程:
@Bean public ServiceProperties serviceProperties() { ServiceProperties serviceProperties = new ServiceProperties(); serviceProperties.setService("http://localhost:9000/login/cas"); serviceProperties.setSendRenew(false); return serviceProperties; } @Bean @Primary public AuthenticationEntryPoint authenticationEntryPoint( ServiceProperties sP) { CasAuthenticationEntryPoint entryPoint = new CasAuthenticationEntryPoint(); entryPoint.setLoginUrl("https://localhost:6443/cas/login"); entryPoint.setServiceProperties(sP); return entryPoint; } @Bean public TicketValidator ticketValidator() { return new Cas30ServiceTicketValidator( "https://localhost:6443/cas"); } @Bean public CasAuthenticationProvider casAuthenticationProvider() { CasAuthenticationProvider provider = new CasAuthenticationProvider(); provider.setServiceProperties(serviceProperties()); provider.setTicketValidator(ticketValidator()); provider.setUserDetailsService( s -> new User("casuser", "Mellon", true, true, true, true, AuthorityUtils.createAuthorityList("ROLE_ADMIN"))); provider.setKey("CAS_PROVIDER_LOCALHOST_9000"); return provider; }
@EnableWebSecurity @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { private AuthenticationProvider authenticationProvider; private AuthenticationEntryPoint authenticationEntryPoint; private SingleSignOutFilter singleSignOutFilter; private LogoutFilter logoutFilter; @Autowired public SecurityConfig(CasAuthenticationProvider casAuthenticationProvider, AuthenticationEntryPoint eP, LogoutFilter lF , SingleSignOutFilter ssF ) { this.authenticationProvider = casAuthenticationProvider; this.authenticationEntryPoint = eP; this.logoutFilter = lF; this.singleSignOutFilter = ssF; } // ... @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(authenticationProvider); } @Override protected AuthenticationManager authenticationManager() throws Exception { return new ProviderManager(Arrays.asList(authenticationProvider)); } @Bean public CasAuthenticationFilter casAuthenticationFilter(ServiceProperties sP) throws Exception { CasAuthenticationFilter filter = new CasAuthenticationFilter(); filter.setServiceProperties(sP); filter.setAuthenticationManager(authenticationManager()); return filter; } }
下面這個(gè)文件配置了application中所有/secured/*,login的URL都是受保護(hù)資源,都要經(jīng)過CAS認(rèn)證過才可以訪問:
@EnableWebSecurity @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .regexMatchers("/secured.*", "/login") .authenticated() .and() .authorizeRequests() .regexMatchers("/") .permitAll() .and() .httpBasic() .authenticationEntryPoint(authenticationEntryPoint); } // ... }服務(wù)端Apereo CAS的配置
跟所有統(tǒng)一認(rèn)證平臺(tái)一樣,所有application想要跟CAS做集成的,都需要在CAS配置相應(yīng)的參數(shù)才可以使用。Apereo CAS提供了很多配置的方式,有YML,JSON, MongoDB以及其他(可查官網(wǎng))。但高度自由的CAS一如既往的,沒有提供可視化操作的界面。比如我們采用JSON的方式。首先我們需要通知Apereo CAS我們采用的是JSON的方式,并通知JSON文件的路徑在哪里
cas.serviceRegistry.initFromJson=true cas.serviceRegistry.config.location=classpath:/services
然后我們?cè)谶@個(gè)目錄里面,創(chuàng)建一個(gè)對(duì)應(yīng)的JSON文件,保存我們的客戶端信息,為了方面管理,建議文件名為 application_id.json, 比如"secureApp_9991.json", 內(nèi)容如下:
{ "@class" : "org.apereo.cas.services.RegexRegisteredService", "serviceId" : "^http://localhost:9000/login/cas", "name" : "CAS Spring Secured App", "description": "This is a Spring App that usses the CAS Server for it"s authentication", "id" : 19991, "evaluationOrder" : 1 }
第一次配置從JSON加載客戶端配置的話,需要重啟Apereo CAS。之后再加新的客戶端的話就不用再重啟,Apereo CAS會(huì)自動(dòng)監(jiān)測(cè)這個(gè)文件夾的變動(dòng)
小結(jié)至此我們對(duì)于Apereo CAS就有了一個(gè)稍微完整一點(diǎn)點(diǎn)的了解,從服務(wù)端安裝部署,到配置,以及客戶端如何集成等。但從這個(gè)短時(shí)間的學(xué)習(xí)來看,如果企業(yè)已經(jīng)重度使用了Apereo CAS,那相信它可以很好地服務(wù)支撐企業(yè)的應(yīng)用。但如果是新的項(xiàng)目,特別是項(xiàng)目周期比較緊張的項(xiàng)目,并且團(tuán)隊(duì)之前沒有對(duì)統(tǒng)一認(rèn)證有技術(shù)積累的話,不是很建議采用Apereo CAS,這些細(xì)微的配置以及無(wú)所不在的隱藏功能,會(huì)讓你給項(xiàng)目經(jīng)理催死的! 后面我會(huì)介紹另外一個(gè)統(tǒng)一認(rèn)證的框架,個(gè)人感覺能彌補(bǔ)Apereo CAS的短板的
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/11452.html