spring + shiro + cas 實現(xiàn)sso單點登錄的示例代碼
sso-shiro-cas
spring下使用shiro+cas配置單點登錄,多個系統(tǒng)之間的訪問,每次只需要登錄一次,項目源碼
系統(tǒng)模塊說明
1.cas: 單點登錄模塊,這里直接拿的是cas的項目改了點樣式而已
2.doc: 文檔目錄,里面有數(shù)據(jù)庫生成語句,采用的是MySQL5.0,數(shù)據(jù)庫名為db_test
3.spring-node-1: 應(yīng)用1
4.spring-node-2: 應(yīng)用2
其中node1跟node2都是采用spring + springMVC + mybatis 框架,使用maven做項目管理
cas集成說明
1.首先采用的是查數(shù)據(jù)庫的方式來校驗用戶身份的,在cas/WEB-INF/deployerConfigContext.xml中第135行構(gòu)建了這個類型
<!-- 設(shè)置密碼的加密方式,這里使用的是MD5加密 -->
<bean id="passwordEncoder"
class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder"
c:encodingAlgorithm="MD5"
p:characterEncoding="UTF-8" />
<!-- 通過數(shù)據(jù)庫驗證身份,這個得自己去實現(xiàn) -->
<bean id="primaryAuthenticationHandler"
class="com.distinct.cas.jdbc.QueryDatabaseAuthenticationHandler"
p:dataSource-ref="dataSource"
p:passwordEncoder-ref="passwordEncoder"
p:sql="select password from t_user where account=? and status = 'active'" />
<!-- 設(shè)置數(shù)據(jù)源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/db_test?useUnicode=true&characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
其中QueryDatabaseAuthenticationHandler這個類是自定義構(gòu)建的,在cas/WEB-INF/lib/cas-jdbc-1.0.0.jar里面,有興趣的同學(xué)可以發(fā)編譯看下,關(guān)于幾個屬性的說明
1.dataSource: 數(shù)據(jù)源,配置MySQL的連接信息
2.passwordEncoder: 加密方式,這里用的是MD5
3.sql: sql查詢語句,這個語句就是根據(jù)用戶輸入的賬號查詢其密碼
以上就是單點登錄管理的主要配置
應(yīng)用系統(tǒng)的配置node1
1.應(yīng)用系統(tǒng)采用shiro做權(quán)限控制,并且跟cas集成
2.在/spring-node-1/src/main/resources/conf/shiro.properties 文件中
``` properties shiro.loginUrl=http://127.0.0.1:8080/cas/login?service=http://127.0.0.1:8081/node1/shiro-cas shiro.logoutUrl=http://127.0.0.1:8080/cas/logout?service=http://127.0.0.1:8081/node1/shiro-cas shiro.cas.serverUrlPrefix=http://127.0.0.1:8080/cas shiro.cas.service=http://127.0.0.1:8081/node1/shiro-cas shiro.failureUrl=/users/loginSuccess shiro.successUrl=/users/loginSuccess ```
其中shiro.loginUrl 跟 shiro.logoutUrl的前面是cas驗證的地址,后面的是我們應(yīng)用系統(tǒng)的地址,這樣配置的方式是為了在訪問我們的應(yīng)用系統(tǒng)的時候,先到cas進行驗證,如果驗證成功了,cas將重定向到shiro.successUrl 所表示的地址
3.在/spring-node-1/src/main/resources/conf/shiro.xml 文件中
<!-- Shiro Filter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<!-- 設(shè)定用戶的登錄鏈接,這里為cas登錄頁面的鏈接地址可配置回調(diào)地址 -->
<property name="loginUrl" value="${shiro.loginUrl}" />
<property name="filters">
<map>
<!-- 添加casFilter到shiroFilter -->
<entry key="casFilter" value-ref="casFilter" />
<entry key="logoutFilter" value-ref="logoutFilter" />
</map>
</property>
<property name="filterChainDefinitions">
<value>
/shiro-cas = casFilter
/logout = logoutFilter
/users/** = user
</value>
</property>
</bean>
<bean id="casFilter" class="org.apache.shiro.cas.CasFilter">
<!-- 配置驗證錯誤時的失敗頁面 -->
<property name="failureUrl" value="${shiro.failureUrl}" />
<property name="successUrl" value="${shiro.successUrl}" />
</bean>
<bean id="logoutFilter" class="org.apache.shiro.web.filter.authc.LogoutFilter">
<!-- 配置驗證錯誤時的失敗頁面 -->
<property name="redirectUrl" value="${shiro.logoutUrl}" />
</bean>
<bean id="casRealm" class="com.spring.mybatis.realm.UserRealm">
<!-- 認證通過后的默認角色 -->
<property name="defaultRoles" value="ROLE_USER" />
<!-- cas服務(wù)端地址前綴 -->
<property name="casServerUrlPrefix" value="${shiro.cas.serverUrlPrefix}" />
<!-- 應(yīng)用服務(wù)地址,用來接收cas服務(wù)端票據(jù) -->
<property name="casService" value="${shiro.cas.service}" />
</bean>
<!-- Shiro's main business-tier object for web-enabled applications -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="subjectFactory" ref="casSubjectFactory"></property>
<property name="realm" ref="casRealm" />
</bean>
<bean id="casSubjectFactory" class="org.apache.shiro.cas.CasSubjectFactory"></bean>
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod"
value="org.apache.shiro.SecurityUtils.setSecurityManager"></property>
<property name="arguments" ref="securityManager"></property>
</bean>
其中shiroFilter這個類主要用于需要攔截的url請求,需要注意的是這個是shiro的攔截,我們還需要配置cas的過濾配置casFilter
casRealm這個類是需要我們自己實現(xiàn)的,主要用于shiro的權(quán)限驗證,里面的屬性說明如下
1.defaultRoles: 默認的角色
2.casServerUrlPrefix: cas地址
3.casService: 系統(tǒng)應(yīng)用地址
最后我們還需要在/spring-node-1/src/main/webapp/WEB-INF/web.xml 文件中配置相關(guān)的過濾器攔截全部請求
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
系統(tǒng)運行
1.端口說明,cas:8080,node1:8081,node2:8082,大家可以采用maven提供的tomcat7插件,配置如下:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>8081</port>
<uriEncoding>UTF-8</uriEncoding>
<server>tomcat7</server>
<path>/node1</path>
</configuration>
</plugin>
這樣的配置,我們甚至都不需要配置tomcat服務(wù)器了,建議這種方式
2.各個模塊的訪問地址
cas:http://127.0.0.1:8080/cas
node1:http://127.0.0.1:8081/node1
node2:http://127.0.0.1:8082/node2
3.訪問系統(tǒng)
輸入 http://127.0.0.1:8081/node1/shiro-cas ,進入cas驗證
輸入用戶名 admin,密碼 admin@2015,驗證成功后將會重定向到http://127.0.0.1:8081/node1//users/loginSuccess ,也就是node1系統(tǒng)的主頁,里面的節(jié)點2代表的是node2系統(tǒng)的主頁,你會發(fā)現(xiàn)我們不需要登錄到node2系統(tǒng)就能訪問其中的系統(tǒng)了
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于java下載中g(shù)etContentLength()一直為-1的一些思路
下面小編就為大家?guī)硪黄趈ava下載中g(shù)etContentLength()一直為-1的一些思路。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
SpringSecurit鹽值加密的密碼驗證以及強密碼驗證過程
在密碼加密過程中,鹽值的使用可以增強密碼的安全性,如果忘記存儲鹽值,將無法驗證密碼,強密碼應(yīng)包含數(shù)字、字母和特殊字符,長度應(yīng)在8到30位之間,以提高賬戶安全2023-03-03
SpringBoot利用AOP實現(xiàn)一個日志管理詳解
目前有這么個問題,有兩個系統(tǒng)CSP和OMS,這倆系統(tǒng)共用的是同一套日志操作:Log;目前想?yún)^(qū)分下這倆系統(tǒng)的日志操作,那沒辦法了,只能重寫一份Log的日志操作。本文就將利用AOP實現(xiàn)一個日志管理,需要的可以參考一下2022-09-09
SpringBoot項目中通過@Value給參數(shù)賦值失敗的解決方案
springboot項目中通過@Value給屬性附值失敗,給參數(shù)賦值失敗,打印為空值,文中通過代碼示例給大家介紹的非常詳細,對大家解決問題有一定的幫助,需要的朋友可以參考下2024-04-04
java對ArrayList中元素進行排序的幾種方式總結(jié)
在Java中,ArrayList類提供了多種排序方法,可以根據(jù)不同的需求選擇適合的排序方法,下面這篇文章主要給大家介紹了關(guān)于java對ArrayList中元素進行排序的幾種方式,需要的朋友可以參考下2024-08-08
SpringBoot請求發(fā)送與信息響應(yīng)匹配實現(xiàn)方法介紹
這篇文章主要介紹了SpringBoot請求發(fā)送與信息響應(yīng)匹配實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10

