日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Shiro学习(15)单点登录

發(fā)布時(shí)間:2025/3/14 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Shiro学习(15)单点登录 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Shiro 1.2開始提供了Jasig CAS單點(diǎn)登錄的支持,單點(diǎn)登錄主要用于多系統(tǒng)集成,即在多個(gè)系統(tǒng)中,用戶只需要到一個(gè)中央服務(wù)器登錄一次即可訪問這些系統(tǒng)中的任何一個(gè),無須多次登錄。此處我們使用Jasig CAS v4.0.0-RC3版本:

https://github.com/Jasig/cas/tree/v4.0.0-RC3

?

Jasig CAS單點(diǎn)登錄系統(tǒng)分為服務(wù)器端和客戶端,服務(wù)器端提供單點(diǎn)登錄,多個(gè)客戶端(子系統(tǒng))將跳轉(zhuǎn)到該服務(wù)器進(jìn)行登錄驗(yàn)證,大體流程如下:

1、訪問客戶端需要登錄的頁面http://localhost:9080/ client/,此時(shí)會跳到單點(diǎn)登錄服務(wù)器https://localhost:8443/ server/login?service=https://localhost:9443/ client/cas;

2、如果此時(shí)單點(diǎn)登錄服務(wù)器也沒有登錄的話,會顯示登錄表單頁面,輸入用戶名/密碼進(jìn)行登錄;

3、登錄成功后服務(wù)器端會回調(diào)客戶端傳入的地址:https://localhost:9443/client/cas?ticket=ST-1-eh2cIo92F9syvoMs5DOg-cas01.example.org,且?guī)е粋€(gè)ticket;

4、客戶端會把ticket提交給服務(wù)器來驗(yàn)證ticket是否有效;如果有效服務(wù)器端將返回用戶身份;

5、客戶端可以再根據(jù)這個(gè)用戶身份獲取如當(dāng)前系統(tǒng)用戶/角色/權(quán)限信息。

?

本章使用了和《第十四章?SSL》一樣的數(shù)字證書。

?

服務(wù)器端

我們使用了Jasig CAS服務(wù)器v4.0.0-RC3版本,可以到其官方的github下載:https://github.com/Jasig/cas/tree/v4.0.0-RC3下載,然后將其cas-server-webapp模塊封裝到shiro-example-chapter15-server模塊中,具體請參考源碼。

?

1、數(shù)字證書使用和《第十四章?SSL》一樣的數(shù)字證書,即將localhost.keystore拷貝到shiro-example-chapter15-server模塊根目錄下;

?

2、在pom.xml中添加Jetty Maven插件,并添加SSL支持:

Java代碼??
  • <plugin>??
  • ??<groupId>org.mortbay.jetty</groupId>??
  • ??<artifactId>jetty-maven-plugin</artifactId>??
  • ??<version>8.1.8.v20121106</version>??
  • ??<configuration>??
  • ????<webAppConfig>??
  • ??????<contextPath>/${project.build.finalName}</contextPath>??
  • ????</webAppConfig>??
  • ????<connectors>??
  • ??????<connector?implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">??
  • ????????<port>8080</port>??
  • ??????</connector>??
  • ??????<connector?implementation="org.eclipse.jetty.server.ssl.SslSocketConnector">??
  • ????????<port>8443</port>??
  • ????????<keystore>${project.basedir}/localhost.keystore</keystore>??
  • ???????<password>123456</password>??
  • ????????<keyPassword>123456</keyPassword>??
  • ??????</connector>??
  • ????</connectors>??
  • ??</configuration>??
  • </plugin>??
  • ?

    3、修改src/main/webapp/WEB-INF/deployerConfigContext.xml,找到primaryAuthenticationHandler,然后添加一個(gè)賬戶:

    Java代碼??
  • <entry?key="zhang"?value="123"/>??
  • 其也支持如JDBC查詢,可以自己定制;具體請參考文檔。

    ?

    4、mvn jetty:run啟動服務(wù)器測試即可:

    訪問https://localhost:8443/chapter15-server/login將彈出如下登錄頁面:


    ?

    輸入用戶名/密碼,如zhang/123,將顯示登錄成功頁面:

    ?

    到此服務(wù)器端的簡單配置就完成了。?

    ?

    ?

    客戶端

    1、首先使用localhost.keystore導(dǎo)出數(shù)字證書(公鑰)到D:\localhost.cer

    Java代碼??
  • keytool?-export?-alias?localhost?-file?D:\localhost.cer?-keystore?D:\localhost.keystore???
  • ?

    2、因?yàn)镃AS client需要使用該證書進(jìn)行驗(yàn)證,需要將證書導(dǎo)入到JDK中:?

    Java代碼??
  • cd?D:\jdk1.7.0_21\jre\lib\security??
  • keytool?-import?-alias?localhost?-file?D:\localhost.cer?-noprompt?-trustcacerts?-storetype?jks?-keystore?cacerts?-storepass?123456???
  • 如果導(dǎo)入失敗,可以先把security?目錄下的cacerts刪掉;?

    ?

    3、按照服務(wù)器端的Jetty Maven插件的配置方式配置Jetty插件;

    ?

    4、在shiro-example-chapter15-client模塊中導(dǎo)入shiro-cas依賴,具體請參考其pom.xml;?

    ?

    5、自定義CasRealm:

    Java代碼??
  • public?class?MyCasRealm?extends?CasRealm?{??
  • ????private?UserService?userService;??
  • ????public?void?setUserService(UserService?userService)?{??
  • ????????this.userService?=?userService;??
  • ????}??
  • ????@Override??
  • ????protected?AuthorizationInfo?doGetAuthorizationInfo(PrincipalCollection?principals)?{??
  • ????????String?username?=?(String)principals.getPrimaryPrincipal();??
  • ????????SimpleAuthorizationInfo?authorizationInfo?=?new?SimpleAuthorizationInfo();??
  • ????????authorizationInfo.setRoles(userService.findRoles(username));??
  • ????????authorizationInfo.setStringPermissions(userService.findPermissions(username));??
  • ????????return?authorizationInfo;??
  • ????}??
  • }???
  • CasRealm根據(jù)CAS服務(wù)器端返回的用戶身份獲取相應(yīng)的角色/權(quán)限信息。?

    ?

    ?

    6、spring-shiro-web.xml配置:

    Java代碼??
  • <bean?id="casRealm"?class="com.github.zhangkaitao.shiro.chapter13.realm.MyCasRealm">??
  • ????<property?name="userService"?ref="userService"/>??
  • ????……??
  • ????<property?name="casServerUrlPrefix"?value="https://localhost:8443/chapter14-server"/>??
  • ????<property?name="casService"?value="https://localhost:9443/chapter14-client/cas"/>??
  • </bean>???
  • casServerUrlPrefix:是CAS Server服務(wù)器端地址;

    casService:是當(dāng)前應(yīng)用CAS服務(wù)URL,即用于接收并處理登錄成功后的Ticket的;

    ?

    如果角色/權(quán)限信息是由服務(wù)器端提供的話,我們可以直接使用CasRealm:?

    Java代碼??
  • <bean?id="casRealm"?class="org.apache.shiro.cas.CasRealm">??
  • ????……??
  • ????<property?name="defaultRoles"?value="admin,user"/>??
  • ????<property?name="defaultPermissions"?value="user:create,user:update"/>??
  • ????<property?name="roleAttributeNames"?value="roles"/>??
  • ????<property?name="permissionAttributeNames"?value="permissions"/>??
  • ????<property?name="casServerUrlPrefix"?value="https://localhost:8443/chapter14-server"/>??
  • ????<property?name="casService"?value="https://localhost:9443/chapter14-client/cas"/>??
  • </bean>???
  • defaultRoles/ defaultPermissions:默認(rèn)添加給所有CAS登錄成功用戶的角色和權(quán)限信息;

    roleAttributeNames/ permissionAttributeNames:角色屬性/權(quán)限屬性名稱,如果用戶的角色/權(quán)限信息是從服務(wù)器端返回的(即返回的CAS Principal中除了Principal之外還有如一些Attributes),此時(shí)可以使用roleAttributeNames/ permissionAttributeNames得到Attributes中的角色/權(quán)限數(shù)據(jù);請自行查詢CAS獲取用戶更多信息。

    ?

    Java代碼??
  • <bean?id="casFilter"?class="org.apache.shiro.cas.CasFilter">??
  • ????<property?name="failureUrl"?value="/casFailure.jsp"/>??
  • </bean>???
  • CasFilter類似于FormAuthenticationFilter,只不過其驗(yàn)證服務(wù)器端返回的CAS Service Ticket。?

    ?

    Java代碼??
  • <bean?id="shiroFilter"?class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">??
  • ????<property?name="securityManager"?ref="securityManager"/>??
  • ????<property?name="loginUrl"?value="https://localhost:8443/chapter14-server/login?service=https://localhost:9443/chapter14-client/cas"/>??
  • ????<property?name="successUrl"?value="/"/>??
  • ????<property?name="filters">??
  • ????????<util:map>??
  • ????????????<entry?key="cas"?value-ref="casFilter"/>??
  • ????????</util:map>??
  • ????</property>??
  • ????<property?name="filterChainDefinitions">??
  • ????????<value>??
  • ????????????/casFailure.jsp?=?anon??
  • ????????????/cas?=?cas??
  • ????????????/logout?=?logout??
  • ????????????/**?=?user??
  • ????????</value>??
  • ????</property>??
  • </bean>???
  • loginUrl:https://localhost:8443/chapter15-server/login表示服務(wù)端端登錄地址,登錄成功后跳轉(zhuǎn)到?service參數(shù)對于的地址進(jìn)行客戶端驗(yàn)證及登錄;

    “/cas=cas”:即/cas地址是服務(wù)器端回調(diào)地址,使用CasFilter獲取Ticket進(jìn)行登錄。

    ?

    7、測試,輸入http://localhost:9080/chapter15-client地址進(jìn)行測試即可,可以使用如Chrome開這debug觀察網(wǎng)絡(luò)請求的變化。

    ?

    如果遇到以下異常,一般是證書導(dǎo)入錯(cuò)誤造成的,請嘗試重新導(dǎo)入,如果還是不行,有可能是運(yùn)行應(yīng)用的JDK和安裝數(shù)字證書的JDK不是同一個(gè)造成的:

    Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    ?????? at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.Java:385)

    ?????? at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)

    ?????? at sun.security.validator.Validator.validate(Validator.java:260)

    ?????? at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)

    ?????? at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)

    ?????? at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)

    ?????? at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1323)

    ?????? ... 67 more

    Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    ?????? at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)

    ?????? at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)

    ?????? at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)

    ?????? ... 73 more

    ??????

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/guoziyi/p/7131167.html

    與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

    總結(jié)

    以上是生活随笔為你收集整理的Shiro学习(15)单点登录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。