怎么在spring boot中利用CAS Client實(shí)現(xiàn)單點(diǎn)登陸驗(yàn)證?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)公司長(zhǎng)期為千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為伊吾企業(yè)提供專業(yè)的成都做網(wǎng)站、網(wǎng)站制作,伊吾網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。CAS Client
負(fù)責(zé)處理對(duì)客戶端受保護(hù)資源的訪問(wèn)請(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
<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、開啟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); } }
通過(guò)上面的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
具體的含義從名字上就可以很清楚的看出來(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)求的包裹, 比如允許開發(fā)者通過(guò)HttpServletRequest的getRemoteUser()方法獲得SSO登錄用戶的登錄名,可選配置。 --> <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; } }
看完上述內(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)的支持。
網(wǎng)頁(yè)名稱:怎么在springboot中利用CASClient實(shí)現(xiàn)單點(diǎn)登陸驗(yàn)證-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://jinyejixie.com/article44/ceooee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、網(wǎng)站策劃、用戶體驗(yàn)、面包屑導(dǎo)航、全網(wǎng)營(yíng)銷推廣、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容