您的位置:首页 > 编程语言 > Java开发

Spring Security @PreAuthorize 拦截无效

2017-03-06 18:28 1236 查看
1. 在使用spring security的时候使用注解,@PreAuthorize("hasAnyRole('ROLE_Admin')")

放在对方法的访问权限进行控制失效,其中配置如:

  Controller中的方法如下:

 

使用一个没有ROLE_Admin权限的用户去访问此方法发现无效。

修改一下:

  添加上:

.antMatchers("/demo/user-list").access("hasRole('ROLE_Admin')")

可以被正常拦截,说明是方法拦截没有生效。

如果是基于xml,则需要在配置文件中加上:

<security:global-method-security

pre-post-annotations="enabled" proxy-target-class="true" />

换成Annotation方式以后,则需要使用@EnableGlobalMethodSecurity(prePostEnabled=true)注解来开启。

并且需要提供以下方法:

@Bean

@Override

public AuthenticationManager authenticationManagerBean() throws Exception {

return super.authenticationManagerBean();

}

至此可以正常拦截
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: