spring boot整合CAS Client實(shí)現(xiàn)單點(diǎn)登陸驗(yàn)證的示例
本文介紹了spring boot整合CAS Client實(shí)現(xiàn)單點(diǎn)登陸驗(yàn)證的示例,分享給大家,也給自己留個(gè)筆記,具體如下:
單點(diǎn)登錄( Single Sign-On , 簡(jiǎn)稱(chēng) SSO )是目前比較流行的服務(wù)于企業(yè)業(yè)務(wù)整合的解決方案之一, SSO 使得在多個(gè)應(yīng)用系統(tǒng)中,用戶(hù)只需要 登錄一次 就可以訪問(wèn)所有相互信任的應(yīng)用系統(tǒng)。
CAS Client
負(fù)責(zé)處理對(duì)客戶(hù)端受保護(hù)資源的訪問(wèn)請(qǐng)求,需要對(duì)請(qǐng)求方進(jìn)行身份認(rèn)證時(shí),重定向到 CAS Server 進(jìn)行認(rèn)證。(原則上,客戶(hù)端應(yīng)用不再接受任何的用戶(hù)名密碼等 Credentials )。
實(shí)現(xiàn)方式一:使用第三方的starter
1、依賴(lài)的jar
<dependency> <groupId>net.unicon.cas</groupId> <artifactId>cas-client-autoconfig-support</artifactId> <version>1.4.0-GA</version> </dependency>
2、增加配置文件
cas.server-url-prefix=http://127.0.0.1 cas.server-login-url=http://127.0.0.1/login cas.client-host-url=http://192.26.4.28:8080 cas.validation-type=CAS
3、開(kāi)啟CAS Client支持
@SpringBootApplication
@ComponentScan(basePackages={"com.chhliu.emailservice"})
@EnableCasClient // 開(kāi)啟CAS支持
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
通過(guò)上面的3步,就可以完成CAS的客戶(hù)端認(rèn)證了!
4、擴(kuò)展
cas.validation-type目前支持3中方式:1、CAS;2、CAS3;3、SAML
其他可用的配置如下:
cas.authentication-url-patterns cas.validation-url-patterns cas.request-wrapper-url-patterns cas.assertion-thread-local-url-patterns cas.gateway cas.use-session cas.redirect-after-validation cas.allowed-proxy-chains cas.proxy-callback-url cas.proxy-receptor-url cas.accept-any-proxy server.context-parameters.renew
具體的含義從名字上就可以很清楚的看出來(lái)。
實(shí)現(xiàn)方式二:手動(dòng)配置
我們?cè)瓉?lái)使用CAS Client,需要在web.xml中做如下配置:
<filter> <filter-name>authenticationFilter</filter-name> <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class> <init-param> <param-name>casServerLoginUrl</param-name> <param-value>http://127.0.0.1/login</param-value> </init-param> <init-param> <param-name>serverName</param-name> <param-value>http://192.26.4.28:8080</param-value> </init-param> </filter> <filter-mapping> <filter-name>authenticationFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 該過(guò)濾器負(fù)責(zé)對(duì)Ticket的校驗(yàn)工作,必須啟用它 --> <filter> <filter-name>validationFilter</filter-name> <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class> <init-param> <param-name>casServerUrlPrefix</param-name> <param-value>http://127.0.0.1</param-value> </init-param> <init-param> <param-name>serverName</param-name> <param-value>http://192.26.4.28:8080</param-value> </init-param> <!-- <init-param> <param-name>redirectAfterValidation</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>useSession</param-name> <param-value>true</param-value> </init-param> --> </filter> <filter-mapping> <filter-name>validationFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 該過(guò)濾器負(fù)責(zé)實(shí)現(xiàn)HttpServletRequest請(qǐng)求的包裹, 比如允許開(kāi)發(fā)者通過(guò)HttpServletRequest的getRemoteUser()方法獲得SSO登錄用戶(hù)的登錄名,可選配置。 --> <filter> <filter-name>httpServletRequestWrapperFilter</filter-name> <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class> </filter> <filter-mapping> <filter-name>httpServletRequestWrapperFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
所以,我們手動(dòng)配置的時(shí)候,需要手動(dòng)配置上面xml中對(duì)應(yīng)的Filter,代碼如下:
@Configuration
@Component
public class CasConfigure {
@Bean
public FilterRegistrationBean authenticationFilterRegistrationBean() {
FilterRegistrationBean authenticationFilter = new FilterRegistrationBean();
authenticationFilter.setFilter(new AuthenticationFilter());
Map<String, String> initParameters = new HashMap<String, String>();
initParameters.put("casServerLoginUrl", "http://127.0.0.1/login");
initParameters.put("serverName", "http://192.26.4.28:8080");
authenticationFilter.setInitParameters(initParameters);
authenticationFilter.setOrder(2);
List<String> urlPatterns = new ArrayList<String>();
urlPatterns.add("/*");// 設(shè)置匹配的url
authenticationFilter.setUrlPatterns(urlPatterns);
return authenticationFilter;
}
@Bean
public FilterRegistrationBean ValidationFilterRegistrationBean(){
FilterRegistrationBean authenticationFilter = new FilterRegistrationBean();
authenticationFilter.setFilter(new Cas20ProxyReceivingTicketValidationFilter());
Map<String, String> initParameters = new HashMap<String, String>();
initParameters.put("casServerUrlPrefix", "http://127.0.0.1");
initParameters.put("serverName", "http://192.26.4.28:8080");
authenticationFilter.setInitParameters(initParameters);
authenticationFilter.setOrder(1);
List<String> urlPatterns = new ArrayList<String>();
urlPatterns.add("/*");// 設(shè)置匹配的url
authenticationFilter.setUrlPatterns(urlPatterns);
return authenticationFilter;
}
@Bean
public FilterRegistrationBean casHttpServletRequestWrapperFilter(){
FilterRegistrationBean authenticationFilter = new FilterRegistrationBean();
authenticationFilter.setFilter(new HttpServletRequestWrapperFilter());
authenticationFilter.setOrder(3);
List<String> urlPatterns = new ArrayList<String>();
urlPatterns.add("/*");// 設(shè)置匹配的url
authenticationFilter.setUrlPatterns(urlPatterns);
return authenticationFilter;
}
@Bean
public FilterRegistrationBean casAssertionThreadLocalFilter(){
FilterRegistrationBean authenticationFilter = new FilterRegistrationBean();
authenticationFilter.setFilter(new AssertionThreadLocalFilter());
authenticationFilter.setOrder(4);
List<String> urlPatterns = new ArrayList<String>();
urlPatterns.add("/*");// 設(shè)置匹配的url
authenticationFilter.setUrlPatterns(urlPatterns);
return authenticationFilter;
}
}
通過(guò)上面的配置,也可以完成CAS Client的認(rèn)證
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringCloud實(shí)現(xiàn)Redis在各個(gè)微服務(wù)的Session共享問(wèn)題
- SpringBoot跨系統(tǒng)單點(diǎn)登陸的實(shí)現(xiàn)方法
- springboot登陸頁(yè)面圖片驗(yàn)證碼簡(jiǎn)單的web項(xiàng)目實(shí)現(xiàn)
- Spring boot搭建web應(yīng)用集成thymeleaf模板實(shí)現(xiàn)登陸
- Spring MVC--攔截器實(shí)現(xiàn)和用戶(hù)登陸例子
- Spring security實(shí)現(xiàn)登陸和權(quán)限角色控制
- springcloud微服務(wù)基于redis集群的單點(diǎn)登錄實(shí)現(xiàn)解析
相關(guān)文章
MyBatis?Generator生成數(shù)據(jù)庫(kù)模型實(shí)現(xiàn)示例
這篇文章主要為大家介紹了MyBatis?Generator生成數(shù)據(jù)庫(kù)模型實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
使用Java獲取html中Select,radio多選的值方法
以下是對(duì)使用Java獲取html中Select,radio多選值的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-08-08
springboot中restful風(fēng)格請(qǐng)求的使用方法示例
RESTful是一種web軟件風(fēng)格,它不是標(biāo)準(zhǔn)也不是協(xié)議,它不一定要采用,只是一種風(fēng)格,它倡導(dǎo)的是一個(gè)資源定位(url)及資源操作的風(fēng)格,下面這篇文章主要給大家介紹了關(guān)于springboot中restful風(fēng)格請(qǐng)求的使用方法,需要的朋友可以參考下2023-02-02
@Valid 校驗(yàn)無(wú)效,BindingResult未獲得錯(cuò)誤的解決
這篇文章主要介紹了@Valid 校驗(yàn)無(wú)效,BindingResult未獲得錯(cuò)誤的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringBoot配置SwaggerUI訪問(wèn)404錯(cuò)誤的解決方法
這篇文章主要為大家詳細(xì)介紹了SpringBoot配置SwaggerUI訪問(wèn)404錯(cuò)誤的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
解決IDEA中 Ctrl+ALT+V這個(gè)快捷鍵無(wú)法使用的情況
這篇文章主要介紹了解決IDEA中 Ctrl+ALT+V這個(gè)快捷鍵無(wú)法使用的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
java線程池prestartCoreThread prestartAllCoreThreads的預(yù)熱源碼解讀
這篇文章主要介紹了java線程池prestartCoreThread prestartAllCoreThreads的預(yù)熱源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
淺談SpringCloud的微服務(wù)架構(gòu)組件
這篇文章主要介紹了淺談SpringCloud的微服務(wù)架構(gòu)組件,Spring Cloud根據(jù)分布式服務(wù)協(xié)調(diào)治理的需求成立了許多子項(xiàng)目,每個(gè)項(xiàng)目通過(guò)特定的組件去實(shí)現(xiàn),需要的朋友可以參考下2023-04-04

