2012-08-07 60 views
21

我发现了许多类似的问题,但没有解决我的问题。 我的问题是ROLE_USER可以访问的功能ROLE_ADMIN@PreAuthorize注解不起作用弹簧安全

我的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"; 

} 

回答

21

你应该有

<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得到罐子。

+0

是的,我知道,但是当我添加 我的代码显示资源没有发现错误,你知道如何解决它? – Raheel 2012-08-07 09:27:13

+0

将异常发送上下文初始化事件给类的监听器实例org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanDefinitionStoreException:异常解析来自ServletContext资源的XML文档异常[/WEB-INF/spring-security.xml ]。嵌套异常是java.lang.NoClassDefFoundError:org/aopalliance/intercept/MethodInterceptor – Raheel 2012-08-08 04:29:20

+0

和我正在使用spring security3.07 – Raheel 2012-08-08 05:16:07

3

与@Secured注释尝试,

那么你就必须

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

    ... 

} 

这里的a detailed blog post about it

1

这可以帮助你:

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

问候。

+0

非常感谢!它解决了我的问题。 – WhiteWater 2016-08-17 14:37:11

2

以上答案都不适用于我。我不得不去添加安全装饰器的路线。

装饰放在豆文件servlet-context.xml内。

首先添加安全架构的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的实现,像这样..使用Servlet时会出现

<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> 
1

这个问题3与Web异步支持。一旦使用了方法的匿名方法,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> 
13

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

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

你必须不包括此元素在你的调度员的XML对应用程序的XML
Spring FAQ

+1

如果你至少没有在你的servlet的xml config- – chrismarx 2014-07-18 15:47:28

+0

中定义的global-security-method,这绝对是正确的答案。这是一个确切的问题,+1提到Spring常见问题解答链接。 – 2018-02-16 10:21:43

21

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

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

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

@EnableGlobalMethodSecurity(prePostEnabled = true)

0

把下面的标签放在另一个弹簧配置文件中,而不是在spring security configuration fil中即

我也遇到了同样的错误。

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