【问题标题】:Spring security for both web services and usersWeb 服务和用户的 Spring 安全性
【发布时间】:2014-08-08 22:06:30
【问题描述】:

我们有一个 web 应用程序,我们想使用 spring security 以两种不同的方式来保护它:

1) 使用登录表单进行身份验证并有权访问某些服务的用户。

2) 使用摘要式身份验证保护的其他服务(用户 + 密码在请求的标头中传递)- 由其他 webapps 使用,因此没有登录表单。

这些都可以单独运行,但我们无法让它们在同一个网络应用程序中运行。 当我们尝试使用两个 xml 运行 webapp 时,我们会收到以下错误:

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

用户的security.xml:

<security:http use-expressions="true">
    <security:intercept-url pattern="/user/login"
        access="permitAll" />
    ...
    <security:intercept-url pattern="/**"
        access="isAuthenticated()" />

    <security:form-login
        authentication-success-handler-ref="userAuthenticationSuccessHandler" />

    <security:logout logout-url="/user/logout"
        logout-success-url="/demo/user/logoutSuccess" />
</security:http>

<bean id="bCryptPasswordEncoder"
    class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />

<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider
        ref="authenticationProvider">
    </security:authentication-provider>
</security:authentication-manager>

用于 Web 服务的 rest-security.xml:

<security:http create-session="stateless"
    entry-point-ref="digestEntryPoint">
    <security:intercept-url pattern="/provider/**"
        access="ROLE_WEBAPP" />

    <security:http-basic />
    <security:custom-filter ref="digestFilter"
        after="BASIC_AUTH_FILTER" />
</security:http>

<bean id="digestFilter"
    class="org.springframework.security.web.authentication.www.DigestAuthenticationFilter">
    <property name="userDetailsService" ref="webappDetailsServiceImpl" />
    <property name="authenticationEntryPoint" ref="digestEntryPoint" />
</bean>

<bean id="digestEntryPoint"
    class="org.springframework.security.web.authentication.www.DigestAuthenticationEntryPoint">
    <property name="realmName" value="Contacts Realm via Digest Authentication" />
    <property name="key" value="acegi" />
</bean>

<security:authentication-manager>
    <security:authentication-provider
        ref="restAuthenticationProvider">
    </security:authentication-provider>
</security:authentication-manager>

有人遇到过这种情况吗?

【问题讨论】:

    标签: java spring security spring-mvc spring-security


    【解决方案1】:

    我在这里找到了解决方案:https://blog.codecentric.de/en/2012/07/spring-security-two-security-realms-in-one-application/

    这篇文章详细说明了我想做的事情。

    诀窍似乎是将pattern="/provider/**" 添加到其余的http 元素中。所以正确的 rest-security 配置是:

    <security:http create-session="stateless"
        entry-point-ref="digestEntryPoint" pattern="/provider/**"
        use-expressions="true">
        <security:intercept-url pattern="/provider/**"
            access="isAuthenticated()" />
    
        <security:http-basic />
        <security:custom-filter ref="digestFilter"
            after="BASIC_AUTH_FILTER" />
    </security:http>
    

    【讨论】:

      【解决方案2】:

      这可能不是您想要的,但我会考虑将这两个 api 分开。一种用于人类,一种用于 Web 服务。

      我们的人机界面就在根上下文中:

      http(s)://your.website.com/...

      然后我们的 Web 服务接口脱离 api 上下文:

      http(s)://your.website.com/api/v1/...

      这使您可以轻松处理您在 spring 中寻找的两种不同类型的安全性。

      【讨论】:

      • 这正是我们正在尝试做的......但它不适用于当前的 spring 安全配置,我找不到一个连贯的例子来说明如何做到这一点。你有关于这个主题的一些文档/教程吗?
      【解决方案3】:

      错误信息

      在过滤器链中在其他模式之前定义通用匹配模式,导致它们被忽略。

      非常简洁。

      您应该检查 web.xml 中 contextConfigLocation 上下文参数的 bean 定义文件的顺序。

      <context-param>  
          <param-name>contextConfigLocation</param-name>  
          <param-value>security.xml,rest-security.xml</param-value>  
      </context-param>
      

      应该重现上面的错误信息。

      <context-param>  
          <param-name>contextConfigLocation</param-name>  
          <param-value>rest-security.xml,security.xml</param-value>  
      </context-param>
      

      应该可以解决问题。

      【讨论】:

      • 这 2 个安全 xml 没有在 contextConfigLocation 中定义,只是应用程序上下文(导入其他 xml)。我尝试了您的解决方案,但仍然收到相同的错误消息。
      • 不过,这似乎是问题的根本原因。您能否将整个安全上下文保存在一个文件中,或者甚至将所有 security:intercept-url 元素放在一个 security:http 元素中?
      猜你喜欢
      • 2013-01-13
      • 2012-01-21
      • 2013-03-20
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 2016-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多