简体   繁体   English

如何检查方法级弹簧安全性

[英]How do I check method level spring security

I have implemented spring security in controller method. 我已经在控制器方法中实现了spring security。

Below is my spring security.xml 下面是我的spring security.xml

--> - >

<!-- URL pattern based security -->
<security:http auto-config="false" entry-point-ref="authenticationEntryPoint"
    use-expressions="true">
    <custom-filter ref="authenticationFilter" position="FORM_LOGIN_FILTER" />
    <security:intercept-url access="hasAnyRole('ROLE_ADMIN','ROLE_USER')" pattern="/common/admin/**" />
    <security:intercept-url pattern="/common/accounting/**" access="hasRole('ROLE_USER')" />
    <security:logout logout-url="/j_spring_security_logout" invalidate-session="true" logout-success-url="/login"/>

</security:http>

Below is my controller 以下是我的控制器

@Secured({"ROLE_ADMIN"})
@RequestMapping(value = "/common/admin/addAdmin", method = RequestMethod.GET)
public String add(ModelMap map) {
    map.addAttribute(new Administrator());
    return "/common/admin/addAdmin";
}

@Secured({"ROLE_ADMIN"})
@RequestMapping(value = "/common/admin/addAdmin", method = RequestMethod.POST)
public String processadd(
        @ModelAttribute("administrator") Administrator administrator) {
    this.administratorManager.addAdmin(administrator);
    return "/common/admin/success";
}

I allow the url /common/admin/** for both admin and user role. 我允许管理员和用户角色使用url / common / admin / **。 But i do some restriction in the admin controller. 但我在管理员控制器中做了一些限制。 when user is go in to /common/admin/* as a user role, he can but he can also go in to method that is only for admin role only. 当用户作为用户角色进入/ common / admin / *时,他可以,但他也可以进入仅适用于管理员角色的方法。

How can I solve it? 我该如何解决?

Thanks! 谢谢!

You already have added the @Secured annotation. 您已经添加了@Secured注释。

But you need to enable it: 但是你需要启用它:

<!-- 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 can take a single or several roles. @Secured可以承担一个或多个角色。

  • @Secured("ROLE_USER")
  • @Secured({"ROLE_USER", "ROLE_ADMIN"}) //grand access if the user has one of this roles @Secured({"ROLE_USER", "ROLE_ADMIN"}) //如果用户具有此角色之一,则进行大访问

BWT: From Spring Security 3 Book (http://www.springsecuritybook.com/): BWT:来自Spring Security 3 Book(http://www.springsecuritybook.com/):

The @Secured annotation is functionallz and syntactiallz the same as @RollesAllowed ... As @Secured functions the same as the JSR standard @RollesAllowed there's not reallz a compelling reason to use it ( @Secured ) in in new code... @Secured注释是functionallz和syntactiallz一样@RollesAllowed ......作为@Secured功能一样的JSR标准@RollesAllowed那里不是reallz一个令人信服的理由来使用它( @Secured在新代码中)在...

(do not forgett to enable it jsr250-annotations="enabled" ) (不要忘记启用它jsr250-annotations="enabled"

I believe you could have multiple roles defined with @Secured annotation . 我相信你可以使用@Secured注释定义多个角色。 Is this what you need? 这是你需要的吗?

If this is the case , try @RolesAllowed 如果是这种情况,请尝试@RolesAllowed

Check this FAQ . 查看此常见问题 Make sure the global-method-security element is in the web context file if you want to apply security to Spring MVC controllers. 如果要将安全性应用于Spring MVC控制器,请确保global-method-security元素位于Web上下文文件中。

Also, you may need to enable class proxying, using 此外,您可能需要启用类代理,使用

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

if your controller implements an interface and the method you are securing is not part of that interface (you'll also need cglib as an additional dependency in your app for this). 如果您的控制器实现了一个接口,并且您正在保护的方法不是该接口的一部分(您还需要将cglib作为应用程序中的附加依赖项)。

IF you want to use annotations, better put the following in servlet.xml. 如果要使用注释,最好将以下内容放在servlet.xml中。 There is no point of enabling the annotations n spring-security-xml as it will not take any effect. 没有必要启用spring-security-xml注释,因为它不会产生任何影响。

Putting above in servlet.xml will do the trick. 将上面的内容放在servlet.xml中就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM