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

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

@Configuration
@EnableWebSecurity
public 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();
}

至此可以正常拦截

最新文章

  1. JS toFixed 四舍六入五成双
  2. js获取缓存数据
  3. bzoj 1028 暴力枚举判断
  4. Http之Get/Post请求区别
  5. Android 儿子Activity在启动过程中的流程组件 &amp;amp;&amp;amp; 儿子Activity在一个新的进程组件启动过程
  6. windows自带的线程池
  7. Python可视化学习(2):Matplotlib快速绘图基础
  8. unity 本地帮助文档 慢
  9. 莫烦keras学习自修第三天【回归问题】
  10. POJ3252-RoundNumbers-排列组合
  11. 【译】如何在 Android 5.0 上获取 SD卡 的读写权限
  12. Java知多少(52)内置异常
  13. display:table和display:table-cell结合使用
  14. 数学建模三剑客MSN
  15. 力特ZE398C驱动光盘-USB转RS232-支持Windows 10/Mac
  16. pyDay8
  17. XMU 1246
  18. 原来你是这样的http2......
  19. 【UOJ 79】 一般图最大匹配 (✿带花树开花)
  20. ubuntu16.04下安装docker

热门文章

  1. Linux常用命令-jdk和Tomcat
  2. 【二】python内置类型
  3. 自研框架wap.js实践
  4. # Instrument Time Profiler教程之Time Profiler
  5. PowerShell管道入门,看看你都会不(管道例子大全)
  6. redis集群搭建实践
  7. 网络编程应用:基于UDP协议【实现聊天程序】--练习
  8. Maven基本安装与配置
  9. Spring+SpringMVC+MyBatis深入学习及搭建(八)——MyBatis查询缓存
  10. BOM中的各种height