关于Spring Boot下Spring Security权限访问设置@PreAuthorize("hasRole('ROLE_ADMIN')")没有用
2016-08-25 11:19
591 查看
承接上篇:Spring Security整合后post数据不了,403拒绝访问
前几天想要限制不同角色的访问权限,于是就直接使用:
注解来标注一个实现类的方法上,但是其他权限依然可以访问 orz,于是我怀疑是放的位置不对,于是放在了Service接口里的方法上,也未果。于是直接放在Controller层的访问方法上,还是未果 ==|||
好了,上网查了一番:Spring Security @PreAuthorize 拦截无效
这篇文章刚好戳中要点:
没有设置开启prePostEnable=true;因为这个默认为false;
如上,添加:
以及:
就酱紫~权限访问完美解决了!!
前几天想要限制不同角色的访问权限,于是就直接使用:
@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()); }
就酱紫~权限访问完美解决了!!
相关文章推荐
- 为spring-boot-admin配置spring security(用于控制访问)
- 详解关于springboot-actuator监控的401无权限访问
- 关于springboot-actuator监控的401无权限访问
- 关于springboot-actuator监控的401无权限访问
- 关于springboot-actuator监控的401无权限访问
- 关于springboot-actuator监控的401无权限访问
- 关于springboot-actuator监控的401无权限访问
- 在 SQL Server 2008年中打开 SQL Server 配置管理器时出现错误消息:"无法连接到 WMI 提供程序。您没有权限,或者服务器无法访问"
- "计算机无法访问,您可能没有权限使用网络资源.请与这台服务器的管理员联系"的解决办法
- 关于win7下,"错误0x0070522:客户端没有所需的权限。"解决方法
- TFS "TF30063: 您没有权限访问 Microsoft-IIS/7.0."
- Nagios 请检查HTTP服务器关于该CGI的访问权限设置
- Nagios 请检查HTTP服务器关于该CGI的访问权限设置
- 您可能没有权限使用网络资源,请......您是否有访问权限"的解决办法
- 关于耳机插入,设备管理中:声音设置中却显示"没有耳机插入"
- 部署错误:尚未授予访问 Tomcat 服务器的权限。请在服务器管理器的 Tomcat 定制器中设置 "manager-script" 角色的正确用户名和口令。 有关详细信息,请查看服务器日志。
- SQLSERVER2005的配置管理器打不开,老是显示“无法连接到WMI提供程序。您没有权限或者该服务器无法访问"
- 当前标识没有对"Temporary ASP.NET Files"的写访问权限的解决办法
- nagios报错 无权查看任何主机的信息。 请检查HTTP服务器关于该CGI的访问权限设置。
- Window访问Linux共享"您可能没有权限使用网络资源"问题解决