【问题标题】:Spring Security configuration issue with http basic : IllegalArgumenthttp basic 的 Spring Security 配置问题:IllegalArgument
【发布时间】:2017-05-09 17:08:26
【问题描述】:

我似乎无法弄清楚为什么此配置会产生 IllegalArgumentException。错误是:

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

配置是:

    <!-- Disable Spring Security for static content -->
<http pattern="/css/**" security="none"/>
<http pattern="/js/**" security="none"/>

<!-- Web app security -->
<http use-expressions="true" authentication-manager-ref="pvDatabase">   
    <!-- Insecure endpoints -->
    <intercept-url pattern="/" access="permitAll"/>
    <intercept-url pattern="/spring/login" access="permitAll"/>
    <intercept-url pattern="/spring/loginfail" access="permitAll"/>
    <intercept-url pattern="/spring/loggedout" access="permitAll"/>     
    <intercept-url pattern="/insecure/**" access="permitAll"/>

    <!-- Secure endpoints -->               
    <intercept-url pattern="/secure/admin/**" access="hasAnyRole('ADMIN')"/>
    <intercept-url pattern="/spring/**" access="hasAnyRole('ADMIN', 'USER')"/>
    <intercept-url pattern="/secure/**" access="hasAnyRole('ADMIN', 'USER')"/>      

    <!-- Authentication Entrypoint is FORM-LOGIN -->
    <form-login login-page="/spring/login" 
        login-processing-url="/spring/login"
        authentication-failure-url="/spring/loginfail" 
        default-target-url="/spring/loginsuccess" 
        always-use-default-target="true" />
    <logout logout-url="/spring/logout" logout-success-url="/spring/loggedout" delete-cookies="JSESSIONID" invalidate-session="true"/>
    <csrf/>

    <!-- HTTP 403 Access denied custom handling -->
    <access-denied-handler ref="pvAccessDeniedHandler"/>
</http>

<!-- Web services security : this section generates an error -->
<http use-expressions="true" create-session="stateless" authentication-manager-ref="pvDatabase">
    <!-- Authentication Entrypoint is HTTP-BASIC -->
    <http-basic entry-point-ref="PVBasicAuthenticationEntryPoint"/>

    <!-- secure endpoints : web services -->
    <intercept-url pattern="/services/api/**" access="hasAnyRole('ADMIN', 'WEBSERVICES')"/>

    <!-- HTTP 403 Access denied custom handling -->
    <access-denied-handler ref="pvAccessDeniedHandler"/>
</http>

如果我删除整个 Web 服务安全部分,安全性会很好,我想要的是能够使用 basic-auth 保护 /services/api/** 模式,此外还限制它只提供给具有 ADMIN 和 WEBSERVICES 角色的用户。

我不确定我是否理解错误,因为没有定义其他通用匹配的 url 模式,我没有在任何地方映射 /**。

我的应用由 2 个 Dispatcher servlet 组成,第一个映射到 /spring/*,第二个映射到 /services/api/*。 Spring Security 过滤器链映射到 /*

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    这个错误是因为 http 块也是按顺序考虑的,http 块的默认模式是 /**。如果除了最后一个 http 块之外的所有块都没有模式属性,则永远不会看到另一个块。

    将模式添加到第一个 http 块应该可以解决您的问题。如果模式不起作用,您还可以使用带有 request-matcher-ref 的 RequestMatcher 的自定义实例。

    【讨论】:

      猜你喜欢
      • 2020-06-21
      • 2016-05-31
      • 2018-02-02
      • 2016-12-17
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多