@RolesAllowed 不起作用

Joh*_*Doe 2 java annotations spring-security

我寻找答案,但没有找到。如果我在spring-security.xml<intercept-url pattern="/test*" access="ROLE_USER" />中添加,一切都会按预期进行。<form-login>但如果我想@RolesAllowed("ROLE_ADMIN")执行该方法:

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

如果 spring-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" />
Run Code Online (Sandbox Code Playgroud)

好吧,在这种情况下,John 和 Jane 都可以访问测试页。我想我错过了一些基本的东西,我们将不胜感激。

Sla*_*hin 7

如果改成with属性@RolesAllowed@PreAuthorize("hasAuthority('ROLE_ADMIN')")定义,还可以吗?global-method-securitypre-post-annotations="enabled"

另外,我认为它不起作用,因为您global-method-security在 servlet 配置而不是应用程序上下文中定义了其他配置。

请参阅类似的帖子:https ://stackoverflow.com/a/2525048/352708