我的JEE6 webapp (主要是CDIEJB3.1和JSF2)使用Spring Security3,而不是Spring依赖注入或MVC。我实现了一个Spring AuthenticationProvider来处理登录。在登录期间,我根据一些自定义的业务逻辑向我的用户添加角色。
现在,我想使用JSR250注释来保护我的业务逻辑。我的业务逻辑是使用无状态EJB(3.1版)实现的。
我在web.xml中包含了Spring的context XML文件,如下所示:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/applicationContext-security.xml
</param-value>
</context-param>下面是XML文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<security:global-method-security jsr250-annotations="enabled"/>
<security:http auto-config="false">
<security:intercept-url pattern="/pages/**" access="ROLE_USER"/>
<security:intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<security:form-login login-page="/login.jsf"/>
<security:anonymous/>
<security:custom-filter ref="logoutFilter" position="LOGOUT_FILTER"/>
</security:http>
<bean id="logoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">
<constructor-arg index="0" value="/login.jsf"/>
<constructor-arg index="1">
<list>
<bean id="customLogoutHandler" class="com.example.client.security.CustomLogoutHandler"/>
<bean id="securityContextLogoutHandler" class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"/>
</list>
</constructor-arg>
<property name="filterProcessesUrl" value="/logout.jsf"/>
</bean>
<security:authentication-manager>
<security:authentication-provider ref="customAuthenticationProvider"/>
</security:authentication-manager>
<bean id="customAuthenticationProvider"
class="com.example.client.security.CustomAuthenticationProvider"/>
</beans>在我的类中,我使用类注释(类型级别)来说明所有方法应该只对具有特定角色的用户可访问:
@Model
@RolesAllowed("ROLE_GROUP")
public class UserListAction {但是,仅具有角色ROLE_USER的用户也可以访问此类的任何功能。在使用以下代码调试时,我验证了用户没有错误的角色:
Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();正如预期的那样,authority集合不包含ROLE_GROUP权限。
我的注解似乎完全被忽略了,但是为什么呢?我也尝试了Spring的pre-post注解,但它们似乎也没有效果。
发布于 2012-10-04 20:10:34
默认情况下,Spring Security使用standard Spring AOP,它仅限于由Spring应用程序上下文创建的bean。生命周期不受Spring控制的对象(如EJB)不会受到影响。同样,如果您使用new或其他框架创建对象实例,则会按需创建对象。
将方法安全拦截器应用于所有对象实例的唯一选择是使用Aspectj。this question的答案可能是一个很好的起点。
https://stackoverflow.com/questions/12680608
复制相似问题