以前公司做項目時就有接觸過spring security,不過是由他人搭建起來,本身在其基礎實現如單點登錄,輸入錯誤限制等一些小功能。感受本身對該框架的理解不是很深,因而花了一些時間從新搭建spring security框架,整理成文檔,但願可以幫到有須要的人。html
實現用戶登陸時,驗證其合法性,並經過security訪問數據庫能夠獲取到相對應角色和資源信息存儲到SecurityContext裏面。java
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>4.2.2.RELEASE</version> </dependency>
<filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springCharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3.spring-security.xml具體配置mysql
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- @描述: 一、儘可能避免使用auto-config="true"這是個自動配置過濾器(Filter)的屬性,這個能夠幫咱們自動配置login form以及相關配置,默認值是false 二、use-expressions若是開啓,則容許使用spring的EL表達式,例如<intercept-url pattern="/*" access="hasRole('ROLE_USER')"/> --> <http entry-point-ref="myAuthenticationEntryPoint" use-expressions="true"> <!-- @描述:登出後刪除cookie --> <logout delete-cookies="JSESSIONID"/> <!-- 登陸過濾器 --> <custom-filter before="FORM_LOGIN_FILTER" ref="loginFilter"/> <!-- @描述:權限攔截器 --> <custom-filter before="FILTER_SECURITY_INTERCEPTOR" ref="securityFilter"/> <csrf disabled="true"/> <!-- @描述:session管理 --> <session-management invalid-session-url="/loginFailure.page"> <concurrency-control max-sessions="1" expired-url="/loginFailure.page" /> </session-management> </http> <!-- @描述:配置auto_config=false,必須設置一個入口點,僅僅是被ExceptionTranslationFilter引用的, 是在出現認證異常、訪問異常時,經過入口點決定redirect、forward的操做,用來通過ExceptionTranslationFilter過濾器處理後, 先捕獲到訪問拒絕異常,並把跳轉動做交給入口點來處理 --> <beans:bean id="myAuthenticationEntryPoint" class="com.demo.base.security.CustomAuthenticationEntryPoint"> <beans:constructor-arg name="loginFormUrl" value="/index.page"/> </beans:bean> <!-- 登錄過濾器 --> <beans:bean id="loginFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"> <beans:property name="authenticationManager" ref="authenticationManager"/> <beans:property name="authenticationFailureHandler" ref="failureHandler"/> <beans:property name="authenticationSuccessHandler" ref="successHandler"/> <beans:property name="filterProcessesUrl" value="/login.ajax"/> </beans:bean> <!-- @描述:權限過濾器 --> <beans:bean id="securityFilter" class="com.demo.base.security.CustomSecurityInterceptor"> <beans:property name="securityMetadataSource" ref="mySecurityMetadataSource"/> <beans:property name="authenticationManager" ref="authenticationManager"/> <beans:property name="accessDecisionManager" ref="myAccessDecisionManagerBean"/> </beans:bean> <!-- @描述:失敗處理類 --> <beans:bean id="failureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"> <beans:property name="defaultFailureUrl" value="/loginFailure.page"/> </beans:bean> <!-- @描述:成功處理類 --> <beans:bean id="successHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"> <beans:property name="alwaysUseDefaultTargetUrl" value="true"/> <beans:property name="defaultTargetUrl" value="/loginSuccess.page"/> </beans:bean> <!-- 權限鑑定管理類 --> <authentication-manager alias="authenticationManager"> <authentication-provider ref="authenticationProvider"/> </authentication-manager> <beans:bean id="authenticationProvider" class="com.demo.base.security.CustomAuthenticationProvider"> <beans:property name="userDetailsService" ref="customUserDetailsService"/> <beans:property name="hideUserNotFoundExceptions" value="false"/> <beans:property name="passwordEncoder" ref="passwordEncoder"/> </beans:bean> <!-- @描述:自定義密碼驗證類 --> <beans:bean id="passwordEncoder" class="com.demo.base.security.CustomPasswordEncoder"/> <!-- 經過 customUserDetailsService,Spring會控制用戶的訪問級別. 也能夠理解成:之後咱們和數據庫操做就是經過customUserDetailsService來進行關聯. --> <beans:bean id="customUserDetailsService" class="com.demo.base.security.CustomUserDetailsService"/> <!-- 獲取數據庫中全部的url資源,讀出url資源與權限的對應關係 --> <beans:bean id="mySecurityMetadataSource" class="com.demo.base.security.CustomInvocationSecurityMetadataSource"> <beans:constructor-arg name="securityService" ref="sysRoleResourceLogService"/> </beans:bean> <!-- 判斷是否有權限訪問請求的url頁面 --> <beans:bean id="myAccessDecisionManagerBean" class="com.demo.base.security.CustomAccessDecisionManager"/> </beans:beans>