1. 程式人生 > >(一)關於spring security的簡要介紹以及相關配置和jar包認識

(一)關於spring security的簡要介紹以及相關配置和jar包認識

重要 force cnblogs control 自定義攔截器 compute 編寫 -- 靈活

Spring Security是一個能夠為基於Spring的企業應用系統提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反轉Inversion of Control ,DI:Dependency Injection 依賴註入)和AOP(面向切面編程)功能,為應用系統提供聲明式的安全訪問控制功能,減少了為企業系統安全控制編寫大量重復代碼的工作。

安全主要包括兩個操作,"認證和驗證(權限控制)",現在是spring security面向的兩個主要方向,認證” 是為用戶建立一個他所聲明的主體的過程, (主體”一般是指用戶,設備或可以在你系統中執行行動的其他系統)。 驗證”指的一個用戶能否在你的應用中執行某個操作。

在身份驗證層面,Spring Security廣泛支持各種身份驗證模式。 這些驗證模型絕大多數都由第三方提供,或正在開發的有關標準機構提供的,例如Internet Engineering Task Force。 作為補充,Spring Security也提供了自己的一套驗證功能。 Spring Security目前支持認證一體化和如下認證技術:

  • HTTP BASIC authentication headers (一個基於IETF RFC的標準)

  • HTTP Digest authentication headers (一個基於IETF RFC的標準)

  • HTTP X.509 client certificate exchange (一個基於IETF RFC的標準)

  • LDAP (一個非常常見的跨平臺認證需要做法,特別是在大環境)

  • Form-based authentication (提供簡單用戶接口的需求)

  • OpenID authentication

  • 基於預先建立的請求頭進行認證 (比如Computer Associates Siteminder)

  • JA-SIG Central Authentication Service (也被稱為CAS,這是一個流行的開源單點登錄系統)

  • Transparent authentication context propagation for Remote Method Invocation (RMI) and HttpInvoker (一個Spring遠程調用協議)

  • Automatic "remember-me" authentication (這樣你可以設置一段時間,避免在一段時間內還需要重新驗證)

  • Anonymous authentication (允許未認證的任何調用,自動假設一個特定的安全主體)

  • Run-as authentication (這在一個會話內使用不同安全身份的時候是非常有用的)

  • Java Authentication and Authorization Service (JAAS)

  • JEE Container autentication (這樣,你可以繼續使用容器管理認證,如果想的話)

  • Kerberos

  • Java Open Source Single Sign On (JOSSO) *

  • OpenNMS Network Management Platform *

  • AppFuse *

  • AndroMDA *

  • Mule ESB *

  • Direct Web Request (DWR) *

  • Grails *

  • Tapestry *

  • JTrac *

  • Jasypt *

  • Roller *

  • Elastic Plath *

  • Atlassian Crowd *

  • 你自己的認證系統(向下看)

(* 是指由第三方提供。)

許多獨立軟件供應商(ISVs, independent software vendors)采用Spring Security,是因為它擁有豐富靈活的驗證模型。 這樣,無論終端用戶需要什麽,他們都可以快速集成到系統中,不用花很多功夫,也不用讓用戶改變運行環境。 如果上述的驗證機制都沒有滿足你的需要,Spring Security是一個開放的平臺,編寫自己的驗證機制是十分簡單的。 Spring Security的許多企業用戶需要整合不遵循任何特定安全標準的“遺留”系統,Spring Security在這類系統上也表現的很好。

Spring Security不僅提供認證功能,也提供了完備的授權功能。 在授權方面主要有三個領域,授權web請求,授權被調用方法,授權訪問單個對象的實例。 為了幫你了解它們之間的區別,對照考慮授在Servlet規範web模式安全,EJB容器管理安全,和文件系統安全方面的授權方式。 Spring Security在所有這些重要領域都提供了完備的能力

好了,簡述到這裏下面主要介紹一下spring security相關的一些jar包:

1.4.1.1. Core - spring-security-core.jar

包含了核心認證和權限控制類和接口, 運程支持和基本供應API。使用Spring Security所必須的。支持單獨運行的應用, 遠程客戶端,方法(服務層)安全和JDBC用戶供應。包含頂級包:

  • org.springframework.security.core

  • org.springframework.security.access

  • org.springframework.security.authentication

  • org.springframework.security.provisioning

1.4.1.2. 遠程調用 - spring-security-remoting.jar

提供與Spring遠程調用的集成。你不需要它,除非你寫了一個遠程客戶端 使用Spring Remoting。 主包是 org.springframework.security.remoting

1.4.1.3. Web - spring-security-web.jar

包含過濾器和對應的web安全架構代碼。任何需要依賴servlet API的。 你將需要它,如果你需要Spring Security Web認證服務和基於URL的權限控制。 主包是org.springframework.security.web

1.4.1.4. Config - spring-security-config.jar

包含安全命名控制解析代碼,你需要它, 如果使用了Spring Security XML命名控制來進行配置。主包是 org.springframework.security.config。這些類都不應該在應用中直接使用。

1.4.1.5. LDAP - spring-security-ldap.jar

LDAP認證和實現代碼,如果你需要使用LDAP認證或管理LDAP用戶實體就是必須的。 頂級包是org.springframework.security.ldap

1.4.1.6. ACL - spring-security-acl.jar

處理領域對象ACL實現。用來提供安全給特定的領域對象實例,在你的應用中。 頂級包是org.springframework.security.acls

1.4.1.7. CAS - spring-security-cas.jar

Spring Security的CAs客戶端集成。如果你希望使用Spring Security web認證 整合一個CAS單點登錄服務器。頂級包是 org.springframework.security.cas

1.4.1.8. OpenID - spring-security-openid.jar

OpenID web認證支持。用來認證用戶,通過一個外部的OpenID服務。 org.springframework.security.openid。需要OpenID4Java。

在pom.xml中的配置如下請看:

 1 <dependency>
 2 <groupId>org.springframework.security</groupId>
 3 <artifactId>spring-security-web</artifactId>
 4 <version>4.1.2.RELEASE</version>
 5 </dependency>
 6 <dependency>
 7 <groupId>org.springframework.security</groupId>
 8 <artifactId>spring-security-config</artifactId>
 9 <version>4.1.2.RELEASE</version>
10 </dependency>
11 <dependency>
12 <groupId>org.springframework.security</groupId>
13 <artifactId>spring-security-taglibs</artifactId>
14 <version>4.1.2.RELEASE</version>
15 </dependency>

spring security在web.xml中的配置:

1 <!-- Spring Secutiry4.1的過濾器鏈配置 -->
2 <filter>
3 <filter-name>springSecurityFilterChain</filter-name>
4 <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
5 </filter>
6 <filter-mapping>
7 <filter-name>springSecurityFilterChain</filter-name>
8 <url-pattern>/*</url-pattern>
9 </filter-mapping>

spring security.xml配置如下:

技術分享
<?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:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
          http://www.springframework.org/schema/beans/spring-beans.xsd  
          http://www.springframework.org/schema/context  
          http://www.springframework.org/schema/context/spring-context.xsd  
          http://www.springframework.org/schema/security  
          http://www.springframework.org/schema/security/spring-security.xsd">

           
         <http pattern="/**/js/**/*" security="none"></http>
    <http pattern="/**/css/**/*" security="none"></http>
    <http pattern="/**/img/**/*" security="none"></http>
    <http pattern="/plugins/**/*" security="none"></http>
    <http pattern="/bootstrap/**/*" security="none"></http>
    <http pattern="/images/**/*" security="none"></http>
    <http pattern="/upload/**/*.*" security="none"></http>


        <http auto-config="false" use-expressions="true" entry-point-ref="myAuthenticationEntryPoint" >  


        <!--配置自定義的過濾器-->
        <custom-filter before="FILTER_SECURITY_INTERCEPTOR" ref="securityInterceptor"/>
 
         <!-- 退出處理 -->  
         <logout logout-url="/j_spring_security_logout"   
                logout-success-url="/loginlog/logout.do"   
                invalidate-session="true"  />  
        <custom-filter ref="loginFilter" position="FORM_LOGIN_FILTER" />
        <!-- 登錄成功後拒絕訪問跳轉的頁面 -->         
        <access-denied-handler ref="accessDeniedHandler" />  
        <csrf disabled="true" />  
    </http>
         

        <!-- 自定義權限不足處理程序 -->
    <beans:bean id="accessDeniedHandler"
        class="com.seawin.webapp.base.security.MyAccessDeniedHandler">
        <beans:property name="errorPage" value="/sysadmin/error.jsp?errorCode=1"></beans:property>
    </beans:bean>
  
    <!-- 使用自定義類myUserDetailsService從數據庫獲取用戶信息 -->  
    <authentication-manager alias="myAuthenticationManager">    
        <authentication-provider user-service-ref="myUserDetailsService"></authentication-provider>  
    </authentication-manager>  
    
    <!-- 自定義用戶資源信息獲取 -->
    <beans:bean id="myUserDetailsService"
        class="com.seawin.webapp.base.security.MyUserDetailService">
    </beans:bean>
      
    <!-- 被認證請求根據所需權限跳轉到不同的登錄界面 -->  
    <beans:bean id="myAuthenticationEntryPoint"   
        class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">  
        <beans:constructor-arg name="loginFormUrl" value="/login.html"></beans:constructor-arg>  
    </beans:bean>  
    
    <!-- 驗證成功後操作 -->
    <beans:bean id="authenticationSuccessHandler" class="com.seawin.webapp.base.security.MySimpleUrlAuthenticationSuccessHandler" />

    <!-- 驗證失敗後操作 -->
    <beans:bean id="authenticationFailureHandler"
        class="com.seawin.webapp.base.security.MySimpleUrlAuthenticationFailureHandler">
        <beans:property name="defaultFailureUrl" value="/login.html" />
    </beans:bean>
    
    <!-- 自定義登陸攔截器 -->
    <beans:bean id="loginFilter"
        class="com.seawin.webapp.base.security.MyAuthenticationFilter">
        <beans:property name="authenticationManager" ref="myAuthenticationManager" />
        <beans:property name="filterProcessesUrl" value="/j_spring_security_check" />
         <!-- 認證成功用自定義類authenticationSuccessHandler處理 -->  
        <beans:property name="authenticationSuccessHandler" ref="authenticationSuccessHandler"/>
        <beans:property name="authenticationFailureHandler" ref="authenticationFailureHandler"/>
    </beans:bean>
    
    <!-- 自定義攔截器 -->
    <beans:bean id="securityInterceptor" class="com.seawin.webapp.base.security.MyAuthorizationFilter">  
        <beans:property name="authenticationManager" ref="myAuthenticationManager"/>  
        <beans:property name="accessDecisionManager" ref="mysecurityAccessDecisionManager"/>  
        <beans:property name="securityMetadataSource" ref="secureResourceFilterInvocationDefinitionSource" />  
    </beans:bean> 
    
    <beans:bean id="mysecurityAccessDecisionManager" class="com.seawin.webapp.base.security.MyAccessDecisionManager" /> 

    <beans:bean id="secureResourceFilterInvocationDefinitionSource" class="com.seawin.webapp.base.security.MySecurityMetadataSource" />

</beans:beans> 
View Code

(一)關於spring security的簡要介紹以及相關配置和jar包認識