1. Introduction 在application context中,你的开始使用security 命名空间,必须在application context文件中添加schema声明。 Java代码
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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-2.0.2.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.2.xsd">
...
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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-2.0.2.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.2.xsd">
...
</beans>如果把spring security配置文件单独配置在一个文件,可以把security命名空间设置为默认的,以简化配置。更改beans tag的 配饰,如下:
引用
<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-2.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.1.xsd">
2.1.1. Design of the Namespace
Web/HTTP Security
Business Object (Method) Security
AuthenticationManager
AccessDecisionManager
AuthenticationProviders
UserDetailsService
2.2. Getting Started with Security Namespace Configuration 2.2.1. web.xml Configuration 首先在app的web.xml文件中添加下面的过滤器声明: 2.2.2. A Minimal <http> Configuration 启用web security从一下配置开始: Java代码
<http auto-config='true'>
<intercept-url pattern="/**" access="ROLE_USER" />
</http>
<http auto-config='true'>
<intercept-url pattern="/**" access="ROLE_USER" />
</http>上面的配置说明,所有的URLs都被保护了,只有ROLE_USER角色的用户才可以访问 可以配置多个 <intercept-url>来适应多种不同的访问控制,但这些 <intercept-url>是有作用顺序的, 排在前面的是优先使用的。 添加一些用户,你能定义一些测试数据在namespace中 Java代码
<authentication-provider>
<user-service>
<user name="jimi" password="jimispassword" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="bob" password="bobspassword" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
<authentication-provider>
<user-service>
<user name="jimi" password="jimispassword" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="bob" password="bobspassword" authorities="ROLE_USER" />
</user-service>
</authentication-provider>在上面auto-config设置为true的配置相当于以下的配置: Java代码
<http>
<intercept-url pattern="/**" access="ROLE_USER" />
<form-login />
<anonymous />
<http-basic />
<logout />
<remember-me />
</http>
<http>
<intercept-url pattern="/**" access="ROLE_USER" />
<form-login />
<anonymous />
<http-basic />
<logout />
<remember-me />
</http>同时注意:auto-config的设置要求有一个UserDetailsService 在表单认证中,默认会自动提供一个页面给你,你也可以定制自己的登录页面: Java代码
<http auto-config='true'>
<intercept-url pattern="/login.jsp*" filters="none"/>
<intercept-url pattern="/**" access="ROLE_USER" />
<form-login login-page='/login.jsp'/>
</http>
<http auto-config='true'>
<intercept-url pattern="/login.jsp*" filters="none"/>
<intercept-url pattern="/**" access="ROLE_USER" />
<form-login login-page='/login.jsp'/>
</http>filters="none"表示排除在拦截链之外。 启用Basic Auth的配置 Java代码
<http auto-config="true">
<intercept-url pattern="/**" access="ROLE_USER" />
<http-basic />
</http>
<http auto-config="true">
<intercept-url pattern="/**" access="ROLE_USER" />
<http-basic />
</http>在上面的配置中Form login仍然是可以使用,值是Basic Auth是优先采用的 2.2.3. Using other Authentication Providers 灵活配置authentication-provider Java代码
<authentication-provider user-service-ref='myUserDetailsService'/>
<authentication-provider user-service-ref='myUserDetailsService'/>如果你使用数据库,还可以这样配置: Java代码
<authentication-provider>
<jdbc-user-service data-source-ref="securityDataSource"/>
</authentication-provider>
<authentication-provider>
<jdbc-user-service data-source-ref="securityDataSource"/>
</authentication-provider>这样要求你的数据库表要符合一定的要求,你也可以这配置Spring Security JdbcDaoImpl。 指向user-service-ref。 添加加密配置: Java代码
<authentication-provider>
<password-encoder hash="sha"/>
<user-service>
<user name="jimi" password="d7e6351eaa13189a5a3641bab846c8e8c69ba39f" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="bob" password="4e7421b1b8765d8f9406d87e7cc6aa784c4ab97f" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
<authentication-provider>
<password-encoder hash="sha"/>
<user-service>
<user name="jimi" password="d7e6351eaa13189a5a3641bab846c8e8c69ba39f" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="bob" password="4e7421b1b8765d8f9406d87e7cc6aa784c4ab97f" authorities="ROLE_USER" />
</user-service>
</authentication-provider>你也能定制自己的加密算法,在文件中配置为一个bean。把bean的name指向password-encoder的ref属性, 不过定制的类要实现PasswordEncoder接口。 2.3. Advanced Web Features 如果你的应用支持HTTP 和 HTTPS,你能要求你特定的URLS仅能通过HTTPS访问, 这样的要求可以通过<intercept-url>的requires-channel属性进行设置得到支持,。 Java代码
<http>
<intercept-url pattern="/secure/**" access="ROLE_USER" requires-channel="https"/>
<intercept-url pattern="/**" access="ROLE_USER" requires-channel="any"/>
...
</http>
<http>
<intercept-url pattern="/secure/**" access="ROLE_USER" requires-channel="https"/>
<intercept-url pattern="/**" access="ROLE_USER" requires-channel="any"/>
...
</http>requires-channel有三种值提供设置:http,https和any 如果你使用非标准的HTTP and/or HTTPS端口。可以指定你自己的: Java代码
<http>
...
<port-mappings>
<port-mapping http="9080" https="9443"/>
</port-mappings>
</http>
<http>
...
<port-mappings>
<port-mapping http="9080" https="9443"/>
</port-mappings>
</http>控制同一个用户是否可以同时进行多次登录: 在web.xml文件中添加下面的配置: Java代码
<listener>
<listener-class>org.springframework.security.ui.session.HttpSessionEventPublisher</listener-class>
</listener>
<listener>
<listener-class>org.springframework.security.ui.session.HttpSessionEventPublisher</listener-class>
</listener>Java代码
<http>
...
<concurrent-session-control max-sessions="1" />
</http>
<http>
...
<concurrent-session-control max-sessions="1" />
</http>concurrent-session-control还有一个属性exception-if-maximum-exceeded。如果设置为true,超过最大max-sessions的设置 以后,是允许登录。而false是可以登录的,同时把时间最长没有活动的session设置过期。 如果你以前使用过Spring Security,你应该知道这个框架里维护了一个过滤器链,来提供它的服务。 你也许想把你自己的过滤器添加到链条的特定位置,或者让已存在的过滤器,使用特定的版本。 你如何在命名空间配置里实现这些功能呢?过滤器链现在已经不能之间看到了。 过滤器顺序在使用命名空间的时候是被严格执行的。 每个Spring Security过滤器都实现了Spring的Ordered接口, 这些过滤器在初始化的时候先被排好序了。 标准的过滤器在命名空间里都有自己的假名: table 2.1. 标准过滤器别名和顺序 Java代码
Alias Filter Class
CHANNEL_FILTER ChannelProcessingFilter
CONCURRENT_SESSION_FILTER ConcurrentSessionFilter
SESSION_CONTEXT_INTEGRATION_FILTER HttpSessionContextIntegrationFilter
LOGOUT_FILTER LogoutFilter
X509_FILTER X509PreAuthenticatedProcessigFilter
PRE_AUTH_FILTER Subclass of AstractPreAuthenticatedProcessingFilter
CAS_PROCESSING_FILTER CasProcessingFilter
AUTHENTICATION_PROCESSING_FILTER AuthenticationProcessingFilter
BASIC_PROCESSING_FILTER BasicProcessingFilter
SERVLET_API_SUPPORT_FILTER classname
REMEMBER_ME_FILTER RememberMeProcessingFilter
ANONYMOUS_FILTER AnonymousProcessingFilter
EXCEPTION_TRANSLATION_FILTER ExceptionTranslationFilter
NTLM_FILTER NtlmProcessingFilter
FILTER_SECURITY_INTERCEPTOR FilterSecurityInterceptor
SWITCH_USER_FILTER SwitchUserProcessingFilter
Alias Filter Class
CHANNEL_FILTER ChannelProcessingFilter
CONCURRENT_SESSION_FILTER ConcurrentSessionFilter
SESSION_CONTEXT_INTEGRATION_FILTER HttpSessionContextIntegrationFilter
LOGOUT_FILTER LogoutFilter
X509_FILTER X509PreAuthenticatedProcessigFilter
PRE_AUTH_FILTER Subclass of AstractPreAuthenticatedProcessingFilter
CAS_PROCESSING_FILTER CasProcessingFilter
AUTHENTICATION_PROCESSING_FILTER AuthenticationProcessingFilter
BASIC_PROCESSING_FILTER BasicProcessingFilter
SERVLET_API_SUPPORT_FILTER classname
REMEMBER_ME_FILTER RememberMeProcessingFilter
ANONYMOUS_FILTER AnonymousProcessingFilter
EXCEPTION_TRANSLATION_FILTER ExceptionTranslationFilter
NTLM_FILTER NtlmProcessingFilter
FILTER_SECURITY_INTERCEPTOR FilterSecurityInterceptor
SWITCH_USER_FILTER SwitchUserProcessingFilter 你可以把你自己的过滤器添加到队列中,使用custom-filter元素,使用这些名字中的一个, 来指定你的过滤器应该出现的位置: Java代码
<beans:bean id="myFilter" class="com.mycompany.MySpecialAuthenticationFilter">
<custom-filter position="AUTHENTICATION_PROCESSING_FILTER"/>
</beans:bean>
<beans:bean id="myFilter" class="com.mycompany.MySpecialAuthenticationFilter">
<custom-filter position="AUTHENTICATION_PROCESSING_FILTER"/>
</beans:bean> 你还可以使用after 或 before属性,如果你想把你的过滤器添加到队列中另一个过滤器的前面或后面。 可以使用"FIRST" 或 "LAST"来指定你想让你的过滤器分别出现在队列元素的前面或后面。 2.3.6. Session Fixation Attack Protection Session固定攻击是一个潜在危险,当一个恶意攻击者可以创建一个session访问一个网站的时候, 然后说服另一个用户登录到同一个会话上(比如,发送给他们一个包含了session标识参数的链接)。 Spring Security通过在用户登录时,创建一个新session来防止这个问题。 如果你不需要保护,或者它与其他一些需求冲突,你可以通过使用<http>中的session-fixation-protection属性来配置它的行为, 它有三个选项 migrateSession - 创建一个新session,把原来session中所有属性复制到新session中。这是默认值。 none - 什么也不做,继续使用原来的session。 newSession - 创建一个新的“干净的”session,不会复制session中的数据。 2.4. Method Security Spring Security 2.0大幅改善了对你的服务层方法添加安全。 如果你使用Java 5或更高版本,还支持JSR-250的安全注解, 同框架提供的@secured注解相似。 你可以为单个bean提供安全控制,通过使用intercept-methods元素装饰bean声明, 或者你可以使用AspectJ方式的切点来控制实体服务层里的多个bean。 2.4.1. The <global-method-security> Element 这个元素用来在你的应用程序中启用基于安全的注解(通过在这个元素中设置响应的属性), 也可以用来声明将要应用在你的实体application context中的安全切点组。 你应该只定义一个<global-method-security>元素。 下面的声明同时启用两种类型的注解: Java代码
<global-method-security secured-annotations="enabled" jsr250-annotations="enabled"/>
<global-method-security secured-annotations="enabled" jsr250-annotations="enabled"/>protect-pointcut是非常强大的,它让你可以用简单的声明对多个bean的进行安全声明。 参考下面的例子: Java代码
<global-method-security>
<protect-pointcut expression="execution(* com.mycompany.*Service.*(..))" access="ROLE_USER"/>
</global-method-security>
<global-method-security>
<protect-pointcut expression="execution(* com.mycompany.*Service.*(..))" access="ROLE_USER"/>
</global-method-security> 这样会保护application context中的符合条件的bean的所有方法,这些bean要在com.mycompany包下, 类名以"Service"结尾。 ROLE_USER的角色才能调用这些方法。 就像URL匹配一样,指定的匹配要放在切点队列的最前面, 第一个匹配的表达式才会被用到 2.4.2. The intercept-methods Bean Decorator <bean:bean id="target" class="com.mycompany.myapp.MyBean"> <intercept-methods> <protect method="set*" access="ROLE_ADMIN" /> <protect method="get*" access="ROLE_ADMIN,ROLE_USER" /> <protect method="doSomething" access="ROLE_USER" /> </intercept-methods> </bean:bean> 2.5. The Default AccessDecisionManager 这章假设你有一些Spring Security权限控制有关的架构知识。 如果没有,你可以跳过这段,以后再来看, 因为这章只是为了自定义的用户设置的,需要在简单基于角色安全的基础上加一些客户化的东西。 当你使用命名空间配置时,默认的AccessDecisionManager实例会自动注册,然后用来为方法调用和web URL访问做验证, 这些都是基于你设置的intercept-url和protect-pointcut权限属性内容(和注解中的内容,如果你使用注解控制方法的权限)。 默认的策略是使用一个AffirmativeBased AccessDecisionManager ,以及RoleVoter 和AuthenticatedVoter。 2.5.1. Customizing the AccessDecisionManager 如果你需要使用一个更复杂的访问控制策略,把它设置给方法和web安全是很简单的。 对于方法安全,你可以设置global-security里的access-decision-manager-ref属性, 用对应 AccessDecisionManager bean在application context里的id: <global-method-security access-decision-manager-ref="myAccessDecisionManagerBean"> ... </global-method-security> web安全安全的语法也是一样,但是放在http元素里: <http access-decision-manager-ref="myAccessDecisionManagerBean"> ... </http> 2.5.2. The Authentication Manager 我们大概知道命名空间配置会自动为我们注册一个验证管理器bean。 这是一个Spring Security的ProviderManager类, 如果你以前使用过框架,应该对它很熟悉了。 你也许想为ProviderManager注册另外的AuthenticationProvider bean, 你可以使用
<custom-authentication-provider>元素实现。比如: Java代码
<bean id="casAuthenticationProvider"
class="org.springframework.security.providers.cas.CasAuthenticationProvider">
<security:custom-authentication-provider />
...
</bean>
<bean id="casAuthenticationProvider"
class="org.springframework.security.providers.cas.CasAuthenticationProvider">
<security:custom-authentication-provider />
...
</bean> 另一个常见的需求是,上下文中的另一个bean可能需要引用AuthenticationManager。 这里有一个特殊的元素,可以让你为AuthenticationManager注册一个别名, 然后你可以application context的其他地方使用这个名字。 Java代码
<security:authentication-manager alias="authenticationManager"/>
<bean id="casProcessingFilter" class="org.springframework.security.ui.cas.CasProcessingFilter">
<security:custom-filter position="CAS_PROCESSING_FILTER"/>
<property name="authenticationManager" ref="authenticationManager"/>
...
</bean>