Spring Security 手册

来源:互联网 发布:java switch 条件 编辑:程序博客网 时间:2024/06/05 20:08

1、Session管理

  (1) 限制用户并发登录系统:

           在web.xml设置<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>

 

然后在application.xml添加如下:

<http>
...
<session-management>
<concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
</session-management>
</http>

(2)阻止Session重放攻击模式 ,用户登录时创建新的session,设置sessionfixation-protection属性,有三种值:

migrateSession:创建新的Session,将旧的session中的数据拷贝到新的Session中,默认配置

none:不做任何事情,原始的session依旧存在

newSession:创建Session,不拷贝数据。

2、方法安全注解

<global-method-security secured-annotations="enabled" /> 将启动Spring Security's @Secured注解

例如:

public interface BankService {
@Secured("IS_AUTHENTICATED_ANONYMOUSLY")
public Account readAccount(Long id);
@Secured("IS_AUTHENTICATED_ANONYMOUSLY")
public Account[] findAccounts();
@Secured("ROLE_TELLER")
public Account post(Account account, double amount);
}

 

<global-method-security pre-post-annotations="enabled" />将启动基于表达式的语法:

public interface BankService {
@PreAuthorize("isAnonymous()")
public Account readAccount(Long id);
@PreAuthorize("isAnonymous()")
public Account[] findAccounts();
@PreAuthorize("hasAuthority('ROLE_TELLER')")
public Account post(Account account, double amount);
}

 

<global-method-security>
<protect-pointcut expression="execution(* com.mycompany.*Service.*(..))"
access="ROLE_USER"/>
</global-method-security> 启动AspectJ方式的注解,

将会包含com.myconpany包内的已Service结尾的类的所有方法,只有使用了Role_users的角色才能触发这些类的调用

 

3、AccessDecisionManager的使用

当使用简单的基于角色的登录时,将会注册一个默认的AccessDecisionManager实例,这个实例会用来对方法调用和url访问权限做出决策,根据我们的intercept-url和protect-pointcut的配置信息。

定制化AccessDecisionManager

 

对于方法级的安全,使用下面的方式

<global-method-security access-decision-manager-ref="myAccessDecisionManagerBean">
...
</global-method-security>

 

对于Web的安全,使用下面的方式

<http access-decision-manager-ref="myAccessDecisionManagerBean">
...
</http>

 

4、认证管理

AuthenticationManager.提供了基本的认证管理接口,使用方式

<authentication-manager>
<authentication-provider ref="casAuthenticationProvider"/>
</authentication-manager>
<bean id="casAuthenticationProvider"
class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
...
</bean>

另一种方式,为AuthenticationManager创建一个别名,一般启动组件使用

<security:authentication-manager alias="authenticationManager">
...
</security:authentication-manager>
<bean id="customizedFormLoginFilter"
class="com.somecompany.security.web.CustomFormLoginFilter">
<property name="authenticationManager" ref="authenticationManager"/>
...
</bean>