Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何检查方法级别的spring安全性_Java_Spring_Url_Annotations_Spring Security - Fatal编程技术网

Java 如何检查方法级别的spring安全性

Java 如何检查方法级别的spring安全性,java,spring,url,annotations,spring-security,Java,Spring,Url,Annotations,Spring Security,我已经在控制器方法中实现了spring安全性 下面是我的spring security.xml --> 我允许管理员和用户角色使用url/common/admin/**。但是我在管理员控制器中做了一些限制。当用户作为用户角色转到/common/admin/*时,他可以,但他也可以转到仅用于admin角色的方法 我怎样才能解决它 谢谢 我相信您可以使用@Secured annotation定义多个角色。这是你需要的吗 如果是这种情况,请尝试@RolesAllowed您已经添加了@Secured

我已经在控制器方法中实现了spring安全性

下面是我的spring security.xml

-->

我允许管理员和用户角色使用url/common/admin/**。但是我在管理员控制器中做了一些限制。当用户作为用户角色转到/common/admin/*时,他可以,但他也可以转到仅用于admin角色的方法

我怎样才能解决它


谢谢

我相信您可以使用@Secured annotation定义多个角色。这是你需要的吗


如果是这种情况,请尝试@RolesAllowed

您已经添加了
@Secured
注释

但您需要启用它:

<!-- secured-annotations = (@Secured("ROLE_ADMIN")) -->
<!-- jsr250-annotations = (@RunAs @RolesAllowed @PermitAll @DenyAll @DeclareRoles) -->
<!-- pre-post-annotations = @PreAuthorized("hasAuthority('ROLE_ADMIN')") -->
<global-method-security
    secured-annotations="enabled" 
    jsr250-annotations="disabled"
    pre-post-annotations="disabled">        
</global-method-security>

@Secured
可以担任一个或多个角色

  • @Secured(“角色用户”)
  • @Secured({“ROLE\u USER”,“ROLE\u ADMIN”})
    //如果用户具有此角色之一,则具有大访问权限
BWT:来自SpringSecurity3Book(http://www.springsecuritybook.com/):

@Secured
注释在功能和语法上与
@RollesAllowed
相同。。。由于
@Secured
的功能与JSR标准
@RollesAllowed
相同,因此在新代码中使用它(
@Secured
)并没有令人信服的理由

(不要忘记启用它
jsr250 annotations=“enabled”

检查。如果要对Spring MVC控制器应用安全性,请确保web上下文文件中有
全局方法安全性
元素

此外,您可能需要使用启用类代理

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


如果您的控制器实现了一个接口,而您正在保护的方法不是该接口的一部分(您还需要在应用程序中使用cglib作为附加依赖项)。

如果您想使用注释,最好将以下内容放在servlet.xml中。启用spring安全xml的注释没有任何意义,因为它不会产生任何效果


把上面的内容放到servlet.xml中就可以了。

嗨,拉尔夫,我已经在security.xml中添加了。但是检查安全性的方法不起作用。我该怎么办?@sudo:请恰当地描述你的问题——在问题中,文本不是明确的问题,也没有暗示任何东西没有按预期工作。我不同意关于
@Secured
注释在功能上与
@RolesAllowed
相同的断言。如果您只是检查角色列表中的一个,那么这可能是正确的,但是
@Secured
注释更强大,因为它们使用Spring Security的
AccessDecisionManager
和投票系统。属性并不局限于简单的角色。@Luke Taylor:我非常怀疑我使用的是Spring(Secrity)应用程序,其中使用了
@RolesAllowed
,没有使用AccessDecisionManager和投票者。关键是
RolesAllowed
只用于做一件事,基于JSR-250规范。它的属性由
Jsr250Voter
使用,如果用户具有这些角色之一,它将允许访问。使用
@Secured
,您可以使用自己的自定义投票者(或多个投票者)来解释属性。
<!-- secured-annotations = (@Secured("ROLE_ADMIN")) -->
<!-- jsr250-annotations = (@RunAs @RolesAllowed @PermitAll @DenyAll @DeclareRoles) -->
<!-- pre-post-annotations = @PreAuthorized("hasAuthority('ROLE_ADMIN')") -->
<global-method-security
    secured-annotations="enabled" 
    jsr250-annotations="disabled"
    pre-post-annotations="disabled">        
</global-method-security>
<global-method-security secured-annotations="enabled" proxy-target-class="true" />