【问题标题】:What is the simplest way to override BasicAuthenticationEntryPoint in SpringSecurity 4?在 Spring Security 4 中覆盖 BasicAuthenticationEntryPoint 的最简单方法是什么?
【发布时间】:2016-01-29 07:00:43
【问题描述】:

我无法在 SO 上找到答案(例如这里。 Spring Security: Commence method in class extending BasicAuthenticationEntryPoint no being called)

我只想覆盖 BasicAuthenticationEntryPoint 而不覆盖其他过滤器和其他人员:

<bean id="authenticationEntryPoint" name="authenticationEntryPoint"
      class="com.myclass.BasicAuthenticationEntryPoint">
    <property name="realmName" value="myapp" />
</bean>

很遗憾,它不起作用,我需要配置过滤器。

<security:http auto-config="true" ..
<sec:custom-filter ref="basicAuthenticationFilter"
                                before="BASIC_AUTH_FILTER" />

</sec:http>

<bean id="basicAuthenticationFilter"
      class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
    <constructor-arg name="authenticationManager" ref="authenticationManager" />
    <constructor-arg name="authenticationEntryPoint" ref="authenticationEntryPoint" />
</bean>

然后我有这个警告。

WARN 2015-10-29 09:44:05,330 [localhost-startStop-1::DefaultFilterChainValidator] [user:system] Possible error: Filters at position 2 and 3 are both instances of org.springframework.security.web.authentication.www.BasicAuthenticationFilter

因此我需要禁用自动配置,但我不想这样做:

<security:http auto-config="false" ...

在 SpringSecurity 4 中覆盖 BasicAuthenticationEntryPoint 的最简单方法是什么?

【问题讨论】:

  • 是关于 REST 身份验证的吗?
  • 是REST认证,但问题很笼统。

标签: spring-security spring-security4


【解决方案1】:

这适用于 Spring Security 3(我认为它应该适用于 Spring 4),无需配置任何过滤器:

public class CustomBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {

    @Override
    public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException) throws IOException, ServletException {

        response.setStatus( HttpServletResponse.SC_UNAUTHORIZED);
    }
}

更新:

CustomBasicAuthenticationEntryPoint 是一个 Spring Bean。你必须告诉 Spring 这件事。就像在您的帖子中一样(我刚刚在答案中更改了它的名称):

<bean id="authenticationEntryPoint" name="authenticationEntryPoint"
      class="com.myclass.CustomBasicAuthenticationEntryPoint">
    <property name="realmName" value="myapp" />
</bean>

你还需要告诉 Spring Security 使用这个 bean 作为入口点而不是默认的入口点:

<security:http entry-point-ref="authenticationEntryPoint" ...

默认配置在未通过身份验证时将客户端重定向到登录页面。当您覆盖此默认行为时,您只会发送 401 代码状态(未经身份验证)并且您不会重定向客户端。

【讨论】:

  • 您使用任何注释吗?没有配置如何工作?
  • 感谢您的帮助!我能够在 http 元素上配置 authenticationEntryPoint:&lt;http entry-point-ref="authenticationEntryPoint"&gt; 请注意,它配置了 ExceptionTranslationFilter 的 authenticationEntryPoint。为了完成解决方案,我在 http-basic 元素(BasicAuthenticationFilter 的配置)&lt;http-basic entry-point-ref="authenticationEntryPoint"/&gt; 处配置了 authenticationEntryPoint
  • 不客气。我没有配置任何过滤器,它对我有用。很高兴它有效。
  • 如果您提供错误的凭据,它将无法工作。
  • 您需要重写 SimpleUrlAuthenticationFailureHandler 来处理身份验证失败。
【解决方案2】:

完整解决方案:

1) 在http元素配置authenticationEntryPoint:

<http entry-point-ref="authenticationEntryPoint" ...>
</http>

配置ExceptionTranslationFilter的authenticationEntryPoint。

2) 在 http-basic 元素上配置 authenticationEntryPoint

<http-basic entry-point-ref="authenticationEntryPoint"/>

配置BasicAuthenticationFilter的authenticationEntryPoint

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多