spring-security 收到空凭据(用户名和密码)时如何创建自定义响应?

How create a custom response when spring-security receives null credentials (username and password)?

我正在使用 postman 向我的服务器发送 没有值 的用户名和密码;就像 username=nullpassword=null.

enter image description here

为了控制我的服务器的安全性,我使用 spring security 3.2。当它收到这些凭据时 spring-security 响应此错误。

Estado HTTP 500 - Fields must not be empty

java.lang.IllegalArgumentException: Cannot pass null or empty values to constructor
    org.springframework.security.core.userdetails.User.<init>(User.java:99)
    org.springframework.security.core.userdetails.User.<init>(User.java:69)
    com.equifax.product.fraud.applicationprocessing.web.rest.interceptors.security.SecurityAuthenticationProvider.retrieveUser(SecurityAuthenticationProvider.java:59)
    org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:132)
    org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
    org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:177)
    org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:168)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
        org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)

我想输出 JSON 带有错误的自定义消息,我该怎么做?

这是我的 security.xml:

    <security:http create-session="never" use-expressions="true"
    auto-config="false">

    <security:intercept-url pattern="/application/**"
        access="isFullyAuthenticated()" />
    <security:anonymous />
    <security:http-basic entry-point-ref="securityAccessDeniedEntryPoint" />
    <security:access-denied-handler ref="securityAccessDeniedHandler" />
</security:http>

<security:authentication-manager alias="authenticationManager"
    erase-credentials="false">
    <security:authentication-provider
        ref="genericSecurityAuthenticationProvider" />
</security:authentication-manager>

我正在使用 Spring 3.2

你可以做的是定义一个 AuthenticationFailureHandler bean,在你实现这个 bean 之后,你可以在方法 onAuthenticationFailure() 中做你想做的事。

XML 配置:

<beans:bean id="myFailureHandler" class="my.custom.impl.MyCustomAuthenticationFailureHandler"/>
<security:http ....>
<security:form-login authentication-failure-handler-ref="myFailureHandler" />
</security:http>

http://docs.spring.io/autorepo/docs/spring-security/3.2.0.RELEASE/apidocs/org/springframework/security/web/authentication/AuthenticationFailureHandler.html

Obs.: 如果您想做的事情不适用于此解决方案,您可以实施 AuthenticationProvider

使用 spring 安全的标准异常,如果您已经有异常处理程序将消息转换为 Json 响应,它将自行处理。

        catch (Exception exception)
        {
            throw new AuthenticationCredentialsNotFoundException("Fields must not be empty", exception);
        }

您正在传递基本身份验证字符串“:”(在 base64 解码之后),因此正如您所说,这会导致空密码和用户名。 BasicAuthenticationFilter 将这些传递给身份验证提供程序,即 您的自定义代码 (SecurityAuthenticationProvider),因此无法准确说明它的作用。在那里的某个地方,您正在使用这些值创建一个 User 实例,这会引发您看到的异常。相反,您应该检查 AuthenticationProvider 中的空值并抛出 Authenticationexception.

您还需要覆盖 BasicAuthenticationFilter 中的 onUnsuccessfulAuthentication 函数来编写您想要的错误响应。您必须将其配置为 custom filter,而不是使用 <security:http-basic /> 元素。

我在使用 spring 安全性之前从未这样做过,但我认为这篇文章描述了您需要的一切:http://www.mkyong.com/spring-security/customize-http-403-access-denied-page-in-spring-security/