@PreAuthorize注释不起作用的弹簧安全性

时间:2012-08-07 07:26:19

标签: java spring spring-security

我发现了许多类似的问题,但没有一个问题解决了我的问题。 我的问题是ROLE_USER可以访问ROLE_ADMIN

的功能

我的spring-security.xml代码如下。

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:s="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.0.xsd">   

<s:http auto-config="true" use-expressions="true">
    <s:intercept-url pattern="/index.jsp" access="permitAll" />
    <s:intercept-url pattern="/welcome*" access="hasRole('ROLE_USER')" />
    <s:intercept-url pattern="/helloadmin*" access="hasRole('ROLE_ADMIN')" />

    <s:form-login login-page="/login" default-target-url="/welcome"
        authentication-failure-url="/loginfailed" />
    <s:logout logout-success-url="/logout" />
</s:http>

<s:authentication-manager>
  <s:authentication-provider>
    <s:user-service>
        <s:user name="asif" password="123456" authorities="ROLE_USER,ROLE_ADMIN" />
        <s:user name="raheel" password="123456" authorities="ROLE_USER" />          
    </s:user-service>
  </s:authentication-provider>
</s:authentication-manager>

我添加<s:global-method-security pre-post-annotations="enabled"/>时 我的代码显示资源未找到错误,当我删除我的代码成功执行但ROLE_USER可以访问ROLE_ADMIN函数

我的控制器功能是。

@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value="/delete", method = RequestMethod.GET)
public String DeleteAll(ModelMap model, Principal principal ) {

    org.springframework.security.core.userdetails.User activeUser = (org.springframework.security.core.userdetails.User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    System.out.println("Active user is "+activeUser.getUsername()+"Authorities are "+activeUser.getAuthorities());
    return "deleteUsers";

}

9 个答案:

答案 0 :(得分:31)

如果您使用的是XML配置,请不要忘记添加以下属性:

<s:global-method-security pre-post-annotations="enabled"/>

如果您使用的是Java配置,请不要忘记添加以下注释:

@EnableGlobalMethodSecurity(prePostEnabled = true)

答案 1 :(得分:26)

你应该

<s:global-method-security pre-post-annotations="enabled"/>

如果您希望@PreAuthorize注释有效。


回答评论:

看起来你错过了spring-aop依赖。

如果您正在使用maven,则需要:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>${org.springframework.version}</version>
</dependency>

如果没有,你可以从here获得jar。

答案 2 :(得分:14)

我遇到了同样的问题。当我将下面的元素从applicationContext.xml移动到* -servlet.xml(我的调度程序的配置xml)时,我的问题就解决了。

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

您必须在调度程序的xml上包含此元素,而不是在您的应用程序的xml 上。
Spring FAQ

答案 3 :(得分:4)

我在使用注释时遇到了同样的问题,问题是我的控制器中没有 @Controller 注释。

所以你似乎需要:

  • Spring安全配置中的这些注释:

    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    
  • 控制器中的这些注释:

    @Controller
    public class ServiceRS {
    
        @RequestMapping(value = "/admin", method = RequestMethod.GET)
        @PreAuthorize("hasAuthority('ROLE_ADMIN')")
        public void wsAdmin() {
            [...]
        }
    }
    

另外,如果您仍有问题,请考虑将其添加到您的log4j2配置中:

    <Logger name="org.springframework.security" level="trace" additivity="false">
        <AppenderRef ref="Console" />
        <AppenderRef ref="RollingRandomAccessFile" />
    </Logger>

您将在日志中看到Spring是否考虑了您的注释:

TRACE org.springframework.security.access.prepost.PrePostAnnotationSecurityMetadataSource {} [main] Looking for Pre/Post annotations for method 'wsAdmin' on target class '***.ServiceRS' 
DEBUG org.springframework.security.access.prepost.PrePostAnnotationSecurityMetadataSource {} [main] @org.springframework.security.access.prepost.PreAuthorize(value=hasAuthority('ROLE_ADMIN')) found on specific method: public void ***.ServiceRS.wsAdmin() 
DEBUG org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource {} [main] Caching method [CacheKey[***.ServiceRS; public void ***.ServiceRS.wsAdmin()]] with attributes [[authorize: 'hasAuthority('ROLE_ADMIN')', filter: 'null', filterTarget: 'null']] 

显然,您可以考虑在 org.springframework.security.access.expression.SecurityExpressionRoot 中的相应方法中添加断点,以查看是否对注释中的条件进行了调用。

问候。

答案 4 :(得分:3)

尝试@Secured注释,

然后你会

@Secured("ROLE_ADMIN")
@RequestMapping(value="/delete", method = RequestMethod.GET)
public String DeleteAll(ModelMap model, Principal principal ) {

  ...

}

这是a detailed blog post about it

答案 5 :(得分:2)

这可能会对您有所帮助:

<security:global-method-security secured-annotations="enabled" proxy-target-class="true"/>

问候。

答案 6 :(得分:2)

上述答案都没有对我有用。我不得不去添加安全装饰器的路线。

装饰器放在文件servlet-context.xml内的bean上。

首先将安全架构添加到XML命名空间:

<beans:beans xmlns="http://www.springframework.org/schema/mvc"
...
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

然后将装饰器应用于服务bean的实现,就像这样..

<beans:bean id="myService" class="my.service.impl.MyServiceImpl" scope="request">
    <security:intercept-methods>
        <security:protect access="ROLE_USER" method="get*"/>
        <security:protect access="ROLE_ADMIN" method="set*"/>
    </security:intercept-methods>
</beans:bean>

答案 7 :(得分:1)

使用带有Web异步支持的Servlet 3时会出现此问题。一旦使用Callable方法的匿名方法,Spring Security 3.1.4及更低版本就会丢失其安全上下文。

升级Spring Security to 3.2.0 RC1将解决您的问题。

Maven依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>3.2.0.RC1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>3.2.0.RC1</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>http://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>http://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

答案 8 :(得分:0)

将下面的标记放在另一个spring配置文件中,而不是在spring安全配置文件中。

我也遇到了同样的错误。

<security:global-method-security secured-annotations="enabled" proxy-target-class="true"/>
相关问题