博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Security @PreAuthorize 拦截无效
阅读量:4364 次
发布时间:2019-06-07

本文共 2415 字,大约阅读时间需要 8 分钟。

1. 在使用spring security的时候使用注解,@PreAuthorize("hasAnyRole('ROLE_Admin')")

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

@Configuration@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter {    @Autowired    UserDetailsService userDetailsService;    @Bean    @Override    public AuthenticationManager authenticationManagerBean() throws Exception {        return super.authenticationManagerBean();    }    @Override    protected void configure(AuthenticationManagerBuilder auth) throws Exception {        auth.userDetailsService(userDetailsService);    }    @Override    protected void configure(HttpSecurity http) throws Exception {        http.csrf().disable()            .authorizeRequests()            .antMatchers("/res/**", "/login/login*").permitAll()            .anyRequest().authenticated()            .and().formLogin().loginPage("/login/login").defaultSuccessUrl("/")                .passwordParameter("password")                .usernameParameter("username")            .and().logout().logoutSuccessUrl("/login/login");    }}

  Controller中的方法如下:

@Controller@RequestMapping("/demo")public class DemoController extends CommonController{    @Autowired    private UserService userService;    @PreAuthorize("hasAnyRole('ROLE_Admin')")    @RequestMapping(value = "user-list")    public void userList() {            }}

 

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

修改一下:

@Override    protected void configure(HttpSecurity http) throws Exception {        http.csrf().disable()            .authorizeRequests()            .antMatchers("/res/**", "/login/login*").permitAll()            .antMatchers("/demo/user-list").access("hasRole('ROLE_Admin')")            .anyRequest().authenticated()            .and().formLogin().loginPage("/login/login").defaultSuccessUrl("/")                .passwordParameter("password")                .usernameParameter("username")            .and().logout().logoutSuccessUrl("/login/login");    }

  添加上:

.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();
}

至此可以正常拦截

转载于:https://www.cnblogs.com/ranger2016/p/3914146.html

你可能感兴趣的文章
机器学习降维算法一:PCA(主成分分析算法)
查看>>
第五周总结
查看>>
Beam概念学习系列之Pipeline Runners
查看>>
Elasticsearch之需要注意的问题(es和jdk版本)
查看>>
HBASE启动失败,Failed construction of Master: class org.apache.hadoop.hbase.master.HMaster
查看>>
【Python 19】BMR计算器3.0(字符串分割与格式化输出)
查看>>
函数和模块的使用
查看>>
sqlx使用说明
查看>>
[转载]SQL Plus 一些使用技巧
查看>>
Dashboard集群
查看>>
TMS320F28335——IO控制/定时计操作
查看>>
MyBatis操作指南-与Spring集成(基于注解)
查看>>
23种设计模式的优点与缺点概况
查看>>
透明的iframe
查看>>
[Unity3D]Unity3D游戏开发之怪物AI
查看>>
玩转MySQL之Linux下的简单操作(服务启动与关闭、启动与关闭、查看版本)
查看>>
CTU 2017 J - Punching Power (二分图匹配)
查看>>
Cisco TrustSec(理解)
查看>>
Android Activity类讲解(一)
查看>>
Mysql中代替like模糊查询的一种方法
查看>>