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

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

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

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

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. windows service in vs

    用 vs2013 创建 windows service 程序 vs2015开发Windows服务 VS 2010一步步开发windows服务(windows service)

  2. Python多线程爬虫

    前言 用上多线程,感觉爬虫跑起来带着风 运行情况 爬取了9万多条文本记录,耗时比较短,一会儿就是几千条 关键点 多个线程对同一全局变量进行修改要加锁 # 获取锁,用于线程同步 threadLock.a ...

  3. 登陆ArcGIS Server Manager时一直显示”请稍后……”

    登陆Server Manager时一直显示”请稍后……”新建完成ARcGIS ServerSite,设置用户名和密码.但是登陆时却发现一直处于等待状态,如下图: 更换为IE浏览器后发现,已经可以登陆并 ...

  4. web自动化测试与Appuim自动化测试对比

    web自动化测试-打开浏览器: from selenium import webdriver driver = webdriver.Chrome() #定义chrome驱动 driver.maximi ...

  5. [HTML]js读取XML文件并解析

    xml文件:test.xml <?xml version="1.0"?> <note> <to>George</to> <fr ...

  6. Windows上搭建Flume运行环境

    1.如果没有安装过Java环境,则需首先安装JDK. 可参考<Windows上搭建Kafka运行环境>中的搭建环境安装JDK部分 2.官方下载Flume(当前为apache-flume-1 ...

  7. 第 3 章 镜像 - 015 - 调试 Dockerfile

    如何 debug Dockerfile 通过 Dockerfile 构建镜像的过程 从 base 镜像运行一个容器 执行命令对容器做修改 执行类似 docker commit 的操作,生成一个新的镜像 ...

  8. Android 如何将手机屏幕投影到 PC 屏幕上或者投影仪上做演示?

    Android 如何将手机屏幕投影到 PC 屏幕上或者投影仪上做演示? 公司开发款APP,要给领导演示,总不能用手机面对面演示吧.所以找了好久,找到一款体验超好的: Total Control-帮助你 ...

  9. Linux 各种软件的安装 - svn

    首先感谢这篇博文 https://www.cnblogs.com/mymelon/p/5483215.html 按照他的配置,svn顺利搭好. 1:yum -y install subversion ...

  10. 为 昂达 v891 安装上了 remix OS 了

    起因: 默认的ROM自带一堆垃圾app,最主要的是没有root , 所以卸载不了. 然后试了 Root大师 , 刷机精灵 之类的软件. 我 CTMD , 简直比出厂ROM 还流氓, 不断的强制安装各种 ...