• Spring Security – security none, filters none, access permitAll


    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上找

  • 相关阅读:
    rpm yum 等命令无响应的解决方法
    ulimit open files linux打开文件数设置验证
    解析XtraBackup备份MySQL的原理和过程(转)
    Xtrabackup之innobackupex备份恢复详解(转)
    Linux中内存查看命令free详解(转)
    mysql innobackupex xtrabackup 大数据量 备份 还原(转)
    Percona Xtrabackup备份mysql(转)
    安装完 MySQL 后必须调整的 10 项配置(转)
    MySQL 5.5 服务器变量详解二(转)
    MySQL 5.5 服务器变量详解一(转)
  • 原文地址:https://www.cnblogs.com/xjknight/p/10892027.html
Copyright © 2020-2023  润新知