<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<!--當有請求道來的時候,Spring Security架構開始檢查要通路的資源是否有權通路,
如果目前登入使用者無權或者目前根本就沒有使用者登入,則Spring Securtiy 架構就自動産生一個 登入頁面。-->
<security:http security="none" pattern="/statics/**" />
<security:http security="none" pattern="/public/**"/>
<security:http security="none" pattern="/login*"/>
<security:http security="none" pattern="/maxSessionError*"/>
<security:http security="none" pattern="/forbidden*"/>
<!--這表示,我們要保護應用程式中的所有URL,隻有擁有 ROLE_USER角色的使用者才能通路。-->
<security:http use-expressions="true">
<!--所有url都要被攔截然後進行判斷-->
<security:intercept-url pattern="/**" access="isAuthenticated()"/>
<!-- 不過濾使用者請求登陸url 、使用者登陸失敗跳轉url 、使用者登陸成功請求url-->
<!-- 登出-->
<!--對登入頁面不進行攔截,加*的原因是此請求可以有參數-->
<!--<security:intercept-url pattern="/test/" filters="none"></security:intercept-url>-->
<!--<http>元素是 所有web相關的命名空間功能的上級元素。<intercept-url>元素定義了 pattern,用來比對進入的請求URL,上面的表示攔截根,以及根子目錄下的所有的路徑。
access屬性定義了請求比對了指定模式時的需求。使用預設的配置, 這個一般是一個逗号分隔的角色隊列,一個使用者中的一個必須被允許通路請求。 字首“ROLE_”表示的是一個使用者應該擁有的權限比對。-->
<!--攔截所有的請求但是這個角色可以随意通路-->
<!--對登入頁面不進行攔截-->
<!--<security:intercept-url pattern="/login.jsp*" filters="none"/>-->
<!--<!–管理者界面必須管理者才能登入–>-->
<!--<security:intercept-url pattern="/admin.jsp*" access="ROLE_ADMIN"/>-->
<!--<!–首頁必須都能通路–>-->
<!--<security:intercept-url pattern="/index.jsp*" access="ROLE_ADMIN,ROLE_USE"/>-->
<!--<!–使用者角色經過認證之後都可以通路–>-->
<!--<security:intercept-url pattern="/**" access="ROLE_USER"/>-->
<security:form-login login-page="/login"
default-target-url="/home"
authentication-failure-url="/login"
authentication-success-handler-ref="loginSuccessHandler"/>
<!--登出成功處理-->
<security:logout invalidate-session="true" delete-cookies="true" success-handler-ref="logoutSuccessHandler"/>
<!--通路受限制-->
<security:access-denied-handler error-page="/forbidden"/>
<!--會話管理配置,登入失效-->
<security:session-management session-fixation-protection="newSession" invalid-session-url="/sessionTimeOut.htm">
<!--配置單點登入,注意web.xml也要相應的配置監聽器-->
<security:concurrency-control max-sessions="1" error-if-maximum-exceeded="false" expired-url="/maxSessionError.htm"/>
</security:session-management>
<!--增加一個filter但是不能修改預設的filter-->
<security:custom-filter ref="myFilter" before="FILTER_SECURITY_INTERCEPTOR"/>
</security:http>
<!--配置認證管理:定義role_user-->
<!--驗證配置,認證管理器,實作使用者認證的入口,主要實作UserDetailsService接口即可 -->
<!--通過MyUserDetailService拿到使用者資訊後,authenticationManager對比使用者的密碼(即驗證使用者),然後這個AuthenticationProcessingFilter攔截器就過咯。-->
<security:authentication-manager alias="authenticationManager">
<!--如果使用者名為user,密碼為user的使用者成功登入了,它的角色是ROLE_USER ,那麼他可以通路規定的資源。-->
<security:authentication-provider user-service-ref="userInfoProvider">
<!--密碼采用md5加密方法,admin加密之後21232f297a57a5a743894a0e4a801fc3-->
<security:password-encoder hash="md5" base64="true"/>
<!--<security:user-service>-->
<!--<security:user name="user" password="21232f297a57a5a743894a0e4a801fc3" authorities="ROLE_USER" />-->
<!--</security:user-service>-->
</security:authentication-provider>
</security:authentication-manager>
<!--一個自定義的filter,必須包含authenticationManager,accessDecisionManager,securityMetadataSource三個屬性,我們的所有控制将在這三個類中實作-->
<beans:bean id="myFilter" class="com.flx.base.filter.MyFilterSecurityInterceptor">
<beans:property name="authenticationManager" ref="authenticationManager"/>
<beans:property name="accessDecisionManager" ref="accessDecisionManager"/>
<beans:property name="securityMetadataSource" ref="securityMetadataSource"/>
</beans:bean>
<!--在這個類中,你就可以從資料庫中讀入使用者的密碼,角色資訊,是否鎖定,賬号是否過期等 -->
<beans:bean id="userInfoProvider" class="com.flx.base.security.secuser.service.impl.UserInfoServiceImpl"/>
<!--資源源資料定義,将所有的資源和權限對應關系建立起來,即定義某一資源可以被哪些角色通路 -->
<beans:bean id="securityMetadataSource" class="com.flx.base.filter.MySecurityMetadataSource"/>
<!--通路決策器,決定某個使用者具有的角色,是否有足夠的權限去通路某個資源 -->
<beans:bean id="accessDecisionManager" class="com.flx.base.filter.MyAccessDesisionmanager"/>
<!--登入成功-->
<bean id="loginSuccessHandler" class="com.flx.base.handler.MyLoginSuccessHandler"/>
<!--登出成功-->
<bean id="logoutSuccessHandler" class="com.flx.base.handler.MyLogoutSuccessHandler"/>
<!--登入失敗-->
<bean id="loginFailHandler" class="com.flx.base.handler.MyLoginFailHandler"/>
</beans>