怎么在spring boot中利用CAS Client實(shí)現(xiàn)單點(diǎn)登陸驗(yàn)證?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
君山網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。成都創(chuàng)新互聯(lián)于2013年創(chuàng)立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
CAS Client
負(fù)責(zé)處理對(duì)客戶端受保護(hù)資源的訪問請(qǐng)求,需要對(duì)請(qǐng)求方進(jìn)行身份認(rèn)證時(shí),重定向到 CAS Server 進(jìn)行認(rèn)證。(原則上,客戶端應(yīng)用不再接受任何的用戶名密碼等 Credentials )。
實(shí)現(xiàn)方式一:使用第三方的starter
1、依賴的jar
net.unicon.cas cas-client-autoconfig-support 1.4.0-GA
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、開啟CAS Client支持
@SpringBootApplication @ComponentScan(basePackages={"com.chhliu.emailservice"}) @EnableCasClient // 開啟CAS支持 public class Application extends SpringBootServletInitializer{ public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
通過上面的3步,就可以完成CAS的客戶端認(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
具體的含義從名字上就可以很清楚的看出來。
實(shí)現(xiàn)方式二:手動(dòng)配置
我們?cè)瓉硎褂肅AS Client,需要在web.xml中做如下配置:
authenticationFilter org.jasig.cas.client.authentication.AuthenticationFilter casServerLoginUrl http://127.0.0.1/login serverName http://192.26.4.28:8080 authenticationFilter /* validationFilter org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter casServerUrlPrefix http://127.0.0.1 serverName http://192.26.4.28:8080 validationFilter /* httpServletRequestWrapperFilter org.jasig.cas.client.util.HttpServletRequestWrapperFilter httpServletRequestWrapperFilter /*
所以,我們手動(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()); MapinitParameters = new HashMap (); 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 urlPatterns = new ArrayList (); urlPatterns.add("/*");// 設(shè)置匹配的url authenticationFilter.setUrlPatterns(urlPatterns); return authenticationFilter; } @Bean public FilterRegistrationBean ValidationFilterRegistrationBean(){ FilterRegistrationBean authenticationFilter = new FilterRegistrationBean(); authenticationFilter.setFilter(new Cas20ProxyReceivingTicketValidationFilter()); Map initParameters = new HashMap (); initParameters.put("casServerUrlPrefix", "http://127.0.0.1"); initParameters.put("serverName", "http://192.26.4.28:8080"); authenticationFilter.setInitParameters(initParameters); authenticationFilter.setOrder(1); List urlPatterns = new ArrayList (); 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 urlPatterns = new ArrayList (); 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 urlPatterns = new ArrayList (); urlPatterns.add("/*");// 設(shè)置匹配的url authenticationFilter.setUrlPatterns(urlPatterns); return authenticationFilter; } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。