具有 spring-security 和 Basic Auth 的 REST-Service 身份验证



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

  • 授权由应用的其他部分完成(因此筛选器链中没有角色(
  • 我想用普通豆子配置所有内容,而无需任何自动配置

让我感到困惑的是,BasicAuthenticationFilter 没有"授权"标头,因此有效地允许所有没有该标头的请求访问。我排除了抛出异常的此类请求,而不是重定向到/身份验证入口点

我的代码如下:

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

我的假设是,我可能需要一个过滤器安全拦截器,之后还有一个访问决策管理器。我不想使用这些,因为在我看来,他们关心授权(与身份验证相反(,而且我在应用程序的这一点上没有任何角色。

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

我想我错过了一些非常基本的东西,所以任何提示或帮助将不胜感激。

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

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

您可能会发现本文很有用,因为它更详细地讨论了普通 Bean 配置。

最新更新