首页 > 编程语言 > 详细

SpringSecurity(二十三):authorizeRequests顺序

时间:2021-05-30 20:30:04      阅读:50      评论:0      收藏:0      [点我收藏+]

我们在使用spring Security时,需要注意authorizeRequests的顺序

 @Override
    protected void configure(HttpSecurity http) throws Exception {
     http.authorizeRequests()
             .anyRequest().authenticated()
             .antMatchers("/hello").permitAll();
    }

由于是按照从上往下顺序依次执行,如上所示,当我们访问/hello时,会发现此时仍然需要登录!

所以我们往往会把.anyRequest().authenticated()放在最后

 @Override
    protected void configure(HttpSecurity http) throws Exception {
     http.authorizeRequests()
             .antMatchers("/hello").hasRole("USER")
             .antMatchers("/hi").hasRole("ADMIN")
             .anyRequest().authenticated();
    }

如上就表示。访问/hello接口需要USER角色,访问/hi需要ADMIN角色,访问其他接口需要认证。

那么访问/hello和/hi不需要认证吗?

也需要,毕竟只有认证了,我们才能从authentication中获得角色信息!

SpringSecurity(二十三):authorizeRequests顺序

原文:https://www.cnblogs.com/wangstudyblog/p/14828301.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!