博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring security antMatchers相关内容
阅读量:6970 次
发布时间:2019-06-27

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

一.antMatcher与antMatchers的区别以及使用场景

来源:https://stackoverflow.com/questions/35890540/when-to-use-spring-securitys-antmatcher

antMatcher用在多个HttpSecurity的场景下,用来为每个HttpSecurity过滤

@EnableWebSecuritypublic class MultiHttpSecurityConfig {  @Autowired  public void configureGlobal(AuthenticationManagerBuilder auth) { 1      auth          .inMemoryAuthentication()              .withUser("user").password("password").roles("USER").and()              .withUser("admin").password("password").roles("USER", "ADMIN");  }  @Configuration  @Order(1)                                                        2  public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {      protected void configure(HttpSecurity http) throws Exception {          http              .antMatcher("/api/**")                               3              .authorizeRequests()                  .anyRequest().hasRole("ADMIN")                  .and()              .httpBasic();      }  }      @Configuration                                                   4  public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {      @Override      protected void configure(HttpSecurity http) throws Exception {          http              .authorizeRequests()                  .anyRequest().authenticated()                  .and()              .formLogin();      }  }}

这种情况下有两个HttpSecurity,未定义@Order就默认最后,多个未定义或者相同Order就按照定义顺序。

需要/api/开头的url匹配Role为Admin的User。

.antMatcher("/api/**") 过滤掉非/api/开头的请求,如果不用antMatcher,所有请求都会进入,进入的请求只有两条路,允许通过(permitall)或者导向login(authenticated),当我们并不想处理

二.antMathchers匹配规则

https://www.cnblogs.com/cyjch/archive/2012/03/28/2421353.html

ANT通配符有三种:

? 匹配任何单字符
* 匹配0或者任意数量的字符
** 匹配0或者更多的目录

三.antMatchers的顺序

https://stackoverflow.com/questions/30819337/multiple-antmatchers-in-spring-security

.antMatchers("/admin/**").hasRole("ADMIN")

.antMatchers("/admin/login").permitAll()

范围小的放在前面,范围大的放在后面,想法的话范围小的就不会匹配,按照范围大的处理。

比如上面,/admin/login按照hasRole("ADMIN")处理。

 

.antMatchers("/admin/**").hasRole("ADMIN").antMatchers("/admin/login").permitAll()

转载于:https://www.cnblogs.com/ptqueen/p/8450028.html

你可能感兴趣的文章
unittest===unittest 的几种执行方式
查看>>
Xcode使用小结1
查看>>
bzoj2763
查看>>
《转》struts2动态方法配置 Action,使一个Action可处理多请求
查看>>
[Shoi2007]Vote 善意的投票
查看>>
eval()函数用法详解
查看>>
Angular 基础入门
查看>>
Xcode的一个简单的UITests
查看>>
前端--CSS之使用display:inline-block来布局(转)
查看>>
需求工程——软件建模与分析阅读笔记05
查看>>
备战找工作
查看>>
方维O2O调用UCENTER头像的方法
查看>>
一个非常标准的Java连接Oracle数据库的示例代码
查看>>
深入理解CSS绝对定位absolute
查看>>
poj3261
查看>>
bbs项目实现点赞和评论的功能
查看>>
贪心算法的实现框架
查看>>
循环例题
查看>>
关于form/input 的autocomplete="off"属性
查看>>
Java 8 Nashorn JavaScript
查看>>