一.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. nsswitch & pam

    nsswitch & pam nsswitch是名称解析框架服务,pam是认证框架服务 对主机来说,有两个功能可能用到框架性服务 1.名称解析: name: id 2.认证服务:验证当前请求获 ...

  2. Hexo搭建(VPS)

    都说 hexo 是静态的 Blog,当时不明觉厉= =.后来终于知道了什么意思......所谓的静态,其实就是因为你不能改云端,而是依赖本地数据,然后使用命令将本地数据变成 web 数据再使用浏览器进 ...

  3. FI 业务

    f-02 post f-03 clear[account]-> f-04 post with clear fb70/f-22 f-32 clear[account]->f-28 post ...

  4. HDU 1298 T9(字典树+dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...

  5. 宠物属性控制_pet

    classIndex 职业索引 DmgAddPct 根据职业的法伤或攻强来计算宠物增加的物理伤害,增加的伤害值等于玩家法伤或攻强的百分比 SpAddPct 根据职业的法伤或攻强来计算宠物增加的法术伤害 ...

  6. docker启动:Got permission denied while trying to connect to the Docker daemon

    权限问题: 1.查看所有用户组与用户 vim /etc/group     /etc/group 的内容包括用户组(Group).用户组口令.GID及该用户组所包含的用户(User),每个用户组一条记 ...

  7. [转][osg]关于PagedLOD 加载卸载机制

    你的PagedLOD 为什么没有卸载 转自:http://bbs.osgchina.org/forum.php?mod=viewthread&tid=7612&highlight=Pa ...

  8. scala中option、None、some对象

    转载:http://www.jianshu.com/p/95896d06a94d 1.option类型避免对象是空值,造成空指针异常. 2.None对象表示null,在没有对象返回时使用,some在有 ...

  9. Spring分布式事务实现概览

    分布式事务,一直是实现分布式系统过程中最大的挑战.在只有单个数据源的单服务系统当中,只要这个数据源支持事务,例如大部分关系型数据库,和一些MQ服务,如activeMQ等,我们就可以很容易的实现事务. ...

  10. 颜色选择器 rgb 与16进制 颜色转换

    1. h5 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...