关于Spring Boot下Spring Security权限访问设置@PreAuthorize("hasRole('ROLE_ADMIN')")没有用
来源:互联网 发布:查看交换机端口速率 编辑:程序博客网 时间:2024/04/28 23:50
承接上篇:Spring Security整合后post数据不了,403拒绝访问
前几天想要限制不同角色的访问权限,于是就直接使用:
@PreAuthorize("hasRole('ROLE_ADMIN')")
注解来标注一个实现类的方法上,但是其他权限依然可以访问 orz,于是我怀疑是放的位置不对,于是放在了Service接口里的方法上,也未果。于是直接放在Controller层的访问方法上,还是未果 ==|||
好了,上网查了一番:Spring Security @PreAuthorize 拦截无效
这篇文章刚好戳中要点:
没有设置开启prePostEnable=true;因为这个默认为false;
@Configuration@EnableGlobalMethodSecurity(prePostEnabled=true)public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ @Bean UserDetailsService customUserService() { return new CustomUserService(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(customUserService()); } @Bean @Override protected AuthenticationManager authenticationManager() throws Exception { return super.authenticationManager(); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/test","/test1").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage(RequestUrls.LoginUrl) .failureUrl(RequestUrls.LoginUrl+"?error") .permitAll() .and() .logout().permitAll(); }}
如上,添加:
@EnableGlobalMethodSecurity(prePostEnabled=true)
@Bean @Override protected AuthenticationManager authenticationManager() throws Exception { return super.authenticationManager(); }
以及:
@Autowired//注意这个方法是注入的 public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(customUserService()); }
就酱紫~权限访问完美解决了!!
2 0
- 关于Spring Boot下Spring Security权限访问设置@PreAuthorize("hasRole('ROLE_ADMIN')")没有用
- Spring Security @PreAuthorize 无效
- Spring security @preauthorize
- Spring Security @PreAuthorize 拦截无效
- Spring Boot添加Spring Security权限
- 关于spring security权限控制
- spring security spring boot
- spring security @PreAuthorize在controller中配置失效解决方法
- spring security 采用角色控制访问权限
- 在Spring Boot中使用Spring Security实现权限控制
- 在Spring Boot中使用Spring Security实现权限控制
- 在Spring Boot中使用Spring Security实现权限控制
- Spring Boot中使用 Spring Security 构建权限系统
- Spring Boot+Spring Security+JWT 实现 RESTful Api 权限控制
- Spring Boot中使用Spring Security实现权限控制
- 在Spring Boot中使用Spring Security实现权限控制
- 在Spring Boot中使用Spring Security实现权限控制
- spring boot 中spring security使用数据库保存权限
- 华创网表v6.8无捆绑官方正式版
- Occlusion-free Face Alignment: Deep Regression Networks Coupled with De-corrupt AutoEncoders阅读笔记
- 解决u-blox在u-center中无法显示北斗卫星的问题
- Socket的基础编程(三)
- bzoj1087[SCOI2005]互不侵犯King
- 关于Spring Boot下Spring Security权限访问设置@PreAuthorize("hasRole('ROLE_ADMIN')")没有用
- 异步压力测试探索:什么是异步非阻塞
- JS改变字体,大、中、小
- 使用static与const关键字时需要掌握的知识
- WEB系统性能问题的分析定位方法
- xcode的基本使用
- 【Algothrim】动态规划实例 (Giving Coins for Candies)
- 代理工具Charles使用
- 性能测试用户模型(三):基础数据分析、场景数据