【问题标题】:Authentication for REST-Service with spring-security and Basic Auth使用 spring-security 和 Basic Auth 对 REST-Service 进行身份验证
【发布时间】:2015-01-31 08:53:31
【问题描述】:

我正在尝试使用 spring-security 为我的 REST-Service 实现基本身份验证,并满足以下要求:

  • 授权由应用的其他部分完成(因此过滤器链中没有角色)
  • 我想用普通 bean 配置所有东西,而不需要任何自动配置

让我感到困惑的是,BasicAuthenticationFilter 没有“授权”标头,因此有效地允许访问没有该标头的所有请求。 我排除了此类抛出异常的请求,而不是重定向到 authenticationEntryPoint 。

我的代码如下:

<bean id="filterChainProxy" class="org.springframework.security.web.FilterChainProxy">
    <constructor-arg>
        <list>
            <security:filter-chain pattern="/rest/**" filters="basicAuthenticationFilter,exceptionTranslationFilter" />
        </list>
    </constructor-arg>
</bean>

<bean id="basicAuthenticationFilter" class="authentication.MyBasicAuthenticationFilter">
    <property name="authenticationManager" ref="myAuthenticationManager" />
    <property name="authenticationEntryPoint" ref="authenticationEntryPoint" />
</bean>

<bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint">
    <property name="realmName" value="myRealm" />
</bean>

我的假设是,我可能需要一个 filterSecurityInterceptor 并且在它之后还需要一个 accessDecisionManager。我不想使用那些,因为在我看来他们关心授权(与身份验证相反),而且我在我的应用程序的这一点上没有任何角色。

我只想检查正确的用户名/密码组合 并做出相应的反应(401 或 403)。

我想我遗漏了一些非常基本的东西,因此非常感谢任何提示或帮助。

【问题讨论】:

    标签: spring-security basic-authentication


    【解决方案1】:

    如果没有授权(从 Spring Security 的角度来看),则允许任何请求,因此不需要身份验证。所以是的,你需要一个FilterSecurityInterceptor,即使它只是根据用户是否经过身份验证来做出决定。

    您还需要在过滤器链的开头使用SecurityContextPersistenceFilter,因为即使您的应用程序是无状态的,也需要在每个请求结束时清除安全上下文。

    您可能会发现 this article 非常有用,因为它更详细地讨论了普通 bean 配置。

    【讨论】:

    • 当使用“FilterSecurityInterceptor”时,我必须定义一个“AccessDecisionManager”。因为我只想进行身份验证(即证明某人拥有一对有效的凭据),所以我的假设是我可以通过提供“AccessDecisionManager”并为其提供“SecurityMetadataSource”来实现这一点,如下所示:“-source>'
    猜你喜欢
    • 2014-07-03
    • 2019-05-29
    • 1970-01-01
    • 2011-10-17
    • 2015-07-30
    • 2014-03-18
    • 2012-11-27
    • 2017-08-14
    相关资源
    最近更新 更多