[英]How do I check method level spring security
我已经在控制器方法中实现了spring security。
下面是我的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>
以下是我的控制器
@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";
}
我允许管理员和用户角色使用url / common / admin / **。 但我在管理员控制器中做了一些限制。 当用户作为用户角色进入/ common / admin / *时,他可以,但他也可以进入仅适用于管理员角色的方法。
我该如何解决?
谢谢!
您已经添加了@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("ROLE_USER")
@Secured({"ROLE_USER", "ROLE_ADMIN"})
//如果用户具有此角色之一,则进行大访问 BWT:来自Spring Security 3 Book(http://www.springsecuritybook.com/):
该
@Secured
注释是functionallz和syntactiallz一样@RollesAllowed
......作为@Secured
功能一样的JSR标准@RollesAllowed
那里不是reallz一个令人信服的理由来使用它(@Secured
在新代码中)在...
(不要忘记启用它jsr250-annotations="enabled"
)
我相信你可以使用@Secured注释定义多个角色。 这是你需要的吗?
如果是这种情况,请尝试@RolesAllowed
查看此常见问题 。 如果要将安全性应用于Spring MVC控制器,请确保global-method-security
元素位于Web上下文文件中。
此外,您可能需要启用类代理,使用
<global-method-security secured-annotations="enabled" proxy-target-class="true" />
如果您的控制器实现了一个接口,并且您正在保护的方法不是该接口的一部分(您还需要将cglib作为应用程序中的附加依赖项)。
如果要使用注释,最好将以下内容放在servlet.xml中。 没有必要启用spring-security-xml注释,因为它不会产生任何影响。
将上面的内容放在servlet.xml中就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.