1.概述

Spring Security提供了几种将请求模式配置为不安全或允许所有访问的机制。取决于这些机制中的哪一种 - 这可能意味着根本不在该路径上运行安全过滤器链,或者运行过滤器链并允许访问

2. access=”permitAll”

使用access =“permitAll”设置元素将配置授权,以便在该特定路径上允许所有请求:

<intercept-url pattern="/login*" access="permitAll" />

或者,通过Java配置:

http.authorizeRequests().antMatchers("/login*").permitAll();

这是在不禁用安全过滤器的情况下实现的 - 这些过滤器仍在运行,因此任何与Spring Security相关的功能仍然可用。

3. filters=”none”

这是Spring 3.1之前的功能,已在Spring 3.1中弃用并替换。

filters属性完全在该特定请求路径上禁用Spring Security过滤器链:

<intercept-url pattern="/login*" filters="none" />

当请求的处理需要Spring Security的某些功能时,这可能会导致问题。

由于这是一个不推荐使用的功能Spring版本高于3.0,使用它与Spring 3.1将导致启动时的运行时异常:

SEVERE: Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: The use of "filters='none'" is no longer supported.
Please define a separate <http> element for the pattern you want to exclude
and use the attribute "security='none'".
Offending resource: class path resource [webSecurityConfig.xml]
at o.s.b.f.p.FailFastProblemReporter.error(FailFastProblemReporter.java:68)

4. security=”none”

正如我们在上面的错误消息中看到的那样,Spring 3.1用一个新的表达式替换filters =“none” - security =“none”。

scope也发生了变化 - 不再在元素级别指定。Spring 3.1允许定义多个元素 - 每个元素都有自己的安全过滤器链配置。因此,新的安全属性现在属于元素级别。

在实践中,这将看起来像:

<http pattern="/resources/**" security="none"/>

或者使用Java配置:

web.ignoring().antMatchers("/resources/**");

而不是旧的:

<intercept-url pattern="/resources/**" filters="none"/>

与filters =“none”类似,这也将完全禁用该请求路径的安全过滤器链。因此,当在应用程序中处理请求时,Spring Security功能将不可用。对于上面的示例而言,这不是问题,其主要涉及提供静态资源 - 其中不进行实际处理。但是,如果以某种方式以编程方式处理请求 - 那么安全功能(例如,需求通道,访问当前用户或调用安全方法)将不可用。

出于同样的原因,没有必要在已经使用security =“none”配置的元素上指定其他属性,因为该请求路径是不安全的,并且将简单地忽略属性。

者,access ='IS_AUTHENTICATED_ANONYMOUSLY'可用于允许匿名访问

5. 注意事项:security=”none”

当使用多个元素时,有些配置了security =“none”,请记住,定义这些元素的顺序很重要。我们希望首先使用特定的路径,然后在最后使用通用模式。

另请注意,如果元素未指定模式,则默认情况下,映射到通用匹配模式 - “/ **”。所以,这个元素必须是最后的。如果元素的顺序不正确,则安全过滤器链的创建将失败:

Caused by: java.lang.IllegalArgumentException: A universal match pattern ('/**')
is defined before other patterns in the filter chain, causing them to be ignored.
Please check the ordering in your <security:http> namespace or FilterChainProxy bean configuration
at o.s.s.c.h.DefaultFilterChainValidator.checkPathOrder(DefaultFilterChainValidator.java:49)
at o.s.s.c.h.DefaultFilterChainValidator.validate(DefaultFilterChainValidator.java:39)

六,结论

本文讨论了允许使用Spring Security访问路径的选项 - 重点关注filters =“none”,security =“none”和access =“permitAll”之间的差异。

像往常一样,这些例子可以在GitHub上找

Spring Security – security none, filters none, access permitAll的更多相关文章

  1. Maven+Spring+Mybatis+Security+Mysql简短的框架

    一段时间想搞个框架做开发,但是网上好多代码建立的都太杂乱.有的开源的东西我感觉用不了.本人太笨,不懂怎么上传到github上,就写在博客里,留作记录.以后用的时候也方便. 1.首先让我们看一下项目结构 ...

  2. Spring Boot Security 整合 JWT 实现 无状态的分布式API接口

    简介 JSON Web Token(缩写 JWT)是目前最流行的跨域认证解决方案.JSON Web Token 入门教程 - 阮一峰,这篇文章可以帮你了解JWT的概念.本文重点讲解Spring Boo ...

  3. Spring Boot Security 整合 OAuth2 设计安全API接口服务

    简介 OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版.本文重点讲解Spring Boot项目对OAuth2进行的实现,如果你对OAut ...

  4. Spring Boot Security OAuth2 实现支持JWT令牌的授权服务器

    概要 之前的两篇文章,讲述了Spring Security 结合 OAuth2 .JWT 的使用,这一节要求对 OAuth2.JWT 有了解,若不清楚,先移步到下面两篇提前了解下. Spring Bo ...

  5. Spring Boot Security Oauth2之客户端模式及密码模式实现

    Spring Boot Security Oauth2之客户端模式及密码模式实现 示例主要内容 1.多认证模式(密码模式.客户端模式) 2.token存到redis支持 3.资源保护 4.密码模式用户 ...

  6. Spring Boot Security And JSON Web Token

    Spring Boot Security And JSON Web Token 说明 流程说明 何时生成和使用jwt,其实我们主要是token更有意义并携带一些信息 https://github.co ...

  7. Spring Boot Security 保护你的程序

    Spring Boot Security 本示例要内容 基于角色的权限访问控制 加密.解密 基于Spring Boot Security 权限管理框架保护应用程序 String Security介绍 ...

  8. Spring boot Security 登陆安全配置

    实现的效果 访问url时,如果未登录时跳转到Login界面,要求用户登陆,如果登陆过返回请求的数据. 效果图 访问数据时,未登录返回login界面 登陆操作 登陆成功进入登出界面 登陆成功后再次访问数 ...

  9. Spring Boot Security 使用教程

    虽然,我在实际项目中使用的是 shiro 进行权限管理,但 spring boot security 早已大名鼎鼎,虽然他的入门要相对复杂一点,但是设计视乎更加吸引人. 本章节就是以一篇快速入门 sp ...

随机推荐

  1. git比较两个版本,获取所有代码有差别的文件,并拷贝到一个文件夹中

    git diff 3b3855d a024af5 --name-only | xargs -i cp '{}' ./update/ --parents 解释:通过xargs 命令,把git diff  ...

  2. centos 安装配置kettle

    安装JDK1.8: step1 下载JDK1.8 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133 ...

  3. 最基本PSO算法的C++实现

    按照James Kennedy & Russell Eberhart (1995)的版本,算法过程如下: [x*] = PSO() P = Particle_Initialization(); ...

  4. Java类加载器回顾

    Java类加载采用了全盘委托机制,默认加载类时子类先会委托给父类加载,但父类加载不到时,子类才会自己尝试加载类.这种机制可以有效防止一个类被加载多次,同时也一定程度上防止重写JDK自身的类[Java自 ...

  5. bzoj 5072 [Lydsy1710月赛]小A的树——树形dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5072 发现对于每个子树,黑点个数确定时,连通块的大小取值范围一定是一段区间:所以考虑只最小化 ...

  6. bzoj 4559 [JLoi2016]成绩比较 —— DP+拉格朗日插值

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4559 看了看拉格朗日插值:http://www.cnblogs.com/ECJTUACM-8 ...

  7. hdu 3932 Groundhog Build Home —— 模拟退火

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3932 找一个位置使距离最远的点的距离最小: 上模拟退火: 每次向距离最远的点移动,注意判断一下距离最远的点 ...

  8. spellchecker inspection helps locate typeos and misspelling in your code, comments and literals, and fix them in one click

    项目layout文件中出现 spellchecker inspection helps locate typos and misspelling in your code, comments and ...

  9. SQL 电子书

    http://vdisk.weibo.com/search/?type=&sortby=default&keyword=SQL+Server&filetype=&pag ...

  10. SpringMVC数据绑定二(List、Set和Map类型)

    1.List类型绑定 //联系信息类 用户类的子属性 public class ContactInfo { private String phone; private String address; ...