2012-03-02 350 views
0

我搜索了答案,但没有找到答案。如果我在我的spring-security.xml的<form-login>里添加<intercept-url pattern="/test*" access="ROLE_USER" />,一切都可以预测。但是,如果我想@RolesAllowed("ROLE_ADMIN")行事的方法:@RolesAllowed不起作用

@RolesAllowed("ROLE_ADMIN") 
@RequestMapping(value="/test", method = RequestMethod.GET) 
public String test() { 
    return "test"; 
}   

如果弹簧security.xml文件看起来是这样的(JSR250的注解已启用):

<http auto-config="true"> 
    <form-login login-page="/login.html" 
       default-target-url="/welcome.html" 
       authentication-failure-url="/loginfailed.html" /> 
    <logout logout-success-url="/logout.html" /> 
</http>   

<authentication-manager> 
    <authentication-provider> 
     <user-service> 
       <user name="john" password="doe" authorities="ROLE_ADMIN" /> 
       <user name="jane" password="doe" authorities="ROLE_USER" /> 
     </user-service> 
    </authentication-provider> 
</authentication-manager> 

<global-method-security secured-annotations="enabled" jsr250-annotations="enabled" /> 

那么,在这种情况下,两个约翰Jane可以访问测试页面。我想我错过了一些基本的东西,将不胜感激。

+0

它是一个关于这个问题的措辞笑话吗?挺滑稽的。我可以用自己的辩护说,我是俄罗斯人。 – 2012-03-02 20:40:35

回答

2

如果将@RolesAllowed更改为@PreAuthorize("hasAuthority('ROLE_ADMIN')")并将global-method-securitypre-post-annotations="enabled"属性一起定义,是否适用?

我猜想它也不起作用,因为你在servlet配置中定义了global-method-security和其他配置,而不是应用程序上下文。

见类似的帖子:https://stackoverflow.com/a/2525048/352708