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

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

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

  1. @EnableWebSecurity
  2. public class MultiHttpSecurityConfig {
  3. @Autowired
  4. public void configureGlobal(AuthenticationManagerBuilder auth) { 1
  5. auth
  6. .inMemoryAuthentication()
  7. .withUser("user").password("password").roles("USER").and()
  8. .withUser("admin").password("password").roles("USER", "ADMIN");
  9. }
  10.  
  11. @Configuration
  12. @Order(1) 2
  13. public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
  14. protected void configure(HttpSecurity http) throws Exception {
  15. http
  16. .antMatcher("/api/**") 3
  17. .authorizeRequests()
  18. .anyRequest().hasRole("ADMIN")
  19. .and()
  20. .httpBasic();
  21. }
  22. }
  23.  
  24. @Configuration 4
  25. public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
  26.  
  27. @Override
  28. protected void configure(HttpSecurity http) throws Exception {
  29. http
  30. .authorizeRequests()
  31. .anyRequest().authenticated()
  32. .and()
  33. .formLogin();
  34. }
  35. }
  36. }

这种情况下有两个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")处理。

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

spring security antMatchers相关内容的更多相关文章

  1. 获取spring security用户相关信息

    在JSP中获得 使用spring security的标签库 在页面中引入标签 <%@ taglib prefix="sec" uri="http://www.spr ...

  2. Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】

    源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...

  3. Spring boot 中 Spring Security 使用改造5部曲(转)

    文章的内容有点长,也是自己学习Spring security的一个总结.如果你从头看到尾,我想你对Spring Security的使用和基本原理应该会有一个比较清晰的认识. 如果有什么理解不对的地方, ...

  4. Spring Boot:整合Spring Security

    综合概述 Spring Security 是 Spring 社区的一个顶级项目,也是 Spring Boot 官方推荐使用的安全框架.除了常规的认证(Authentication)和授权(Author ...

  5. Spring Security 解析(五) —— Spring Security Oauth2 开发

    Spring Security 解析(五) -- Spring Security Oauth2 开发   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决 ...

  6. springBoot整合spring security+JWT实现单点登录与权限管理--筑基中期

    写在前面 在前一篇文章当中,我们介绍了springBoot整合spring security单体应用版,在这篇文章当中,我将介绍springBoot整合spring secury+JWT实现单点登录与 ...

  7. 4-11 Spring Security及SSO

    1. 关于用户身份认证与授权 Spring Security是用于解决认证与授权的框架. 在根项目下创建新的csmall-passport子模块,最基础的依赖项包括spring-boot-starte ...

  8. Spring Security(二十):6.2.3 Form and Basic Login Options

    You might be wondering where the login form came from when you were prompted to log in, since we mad ...

  9. Spring Security 快速了解

    在Spring Security之前 我曾经使用 Interceptor 实现了一个简单网站Demo的登录拦截和Session处理工作,虽然能够实现相应的功能,但是无疑Spring Security提 ...

随机推荐

  1. Qt打包

    先在Qt Creator里release一遍,打开Qt下载时自带的像命令控制台一样的东西,比如我在创建项目时选用的是MinGw编译器, 然后在文件管理器里找到release生成的exe,cd进exe所 ...

  2. Dalvik VM 和JVM的比较

    避免出现版权问题android重写JVM 两者在编译后的文件格式区别: JVM: .java->.class->.jar DALVIK VM:.java->.class->.d ...

  3. Oracle AMERICAN改成简体中文

    64位Oracle连接32位PLSQL_Developer时,在PLSQL_Developer安装的目录中新建了一个TXT文件,之后更名为start.bat,内容如下: @echo off set p ...

  4. oracle 新建用户

    create user username identified by password; grant dba to username; 授权 或 grant connect, resource to ...

  5. ubuntu 安装pip3 遇到Ignoring ensurepip failure: pip 8.1.1 requires SSL/TLS错误

    3.5版本之后的会自动安装pip,所以我们直接从官网下载3.5.2,下载地址:https://www.python.org/ftp/python/ 下载以后,可以用命令解压,也可以右键进行解压, ta ...

  6. NET Core 指令启动

    ASP.NET Core 是新一代的 ASP.NET,早期称为 ASP.NET vNext,并且在推出初期命名为ASP.NET 5,但随着 .NET Core 的成熟,以及 ASP.NET 5的命名会 ...

  7. 转载 R语言颜色基础设置

    原文链接:http://www.biostatistic.net/thread-5065-1-1.html R语言在画图形的时候,经常遇到颜色设定问题,用户可以根据color.rgb值和hsv值来设定 ...

  8. MYSQL常用函数(格式化函数)

    DATE_FORMAT(date,fmt)  依照字符串fmt格式化日期date值 FORMAT(x,y)   把x格式化为以逗号隔开的数字序列,y是结果的小数位数 INET_ATON(ip)   返 ...

  9. mt19937 -- 高质量随机数

    优点:产生速度快, 周期大 用法: #include<bits/stdc++.h> using namespace std; int main() { mt19937 mt_rand(ti ...

  10. js判断字符串与字符串相互包含,以及数组是否包含某个元素;

    需求:判端一个字符串是否包含另一个字符串? 实现: var str  = "adc"; 判断str 中是否包含 "c" if( str.indexOf(&quo ...