我正在使用邮递员将无值的用户名和密码发送到我的服务器;它就像username=null
和password=null
。
为了控制服务器的安全性,我使用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
答案 0 :(得分:4)
您正在传递基本身份验证字符串&#34;:&#34; (在base64解码后),所以你说这会导致一个空密码用户名。 BasicAuthenticationFilter
将这些传递给身份验证提供程序,即您的自定义代码(SecurityAuthenticationProvider
),因此无法准确说出它的作用。在某处您正在使用这些值创建User
实例,这会抛出您看到的异常。相反,您应该检查AuthenticationProvider
中的空值并抛出Authenticationexception
。
您还需要覆盖onUnsuccessfulAuthentication
中的BasicAuthenticationFilter
函数以编写所需的错误响应。您必须将其配置为custom filter,而不是使用<security:http-basic />
元素。
答案 1 :(得分:3)
使用spring security的标准异常,如果你已经有一个异常处理程序将消息转换为Json响应,它将自行处理。
catch (Exception exception)
{
throw new AuthenticationCredentialsNotFoundException("Fields must not be empty", exception);
}
答案 2 :(得分:2)
What you can do is define a AuthenticationFailureHandler bean, after you implement this bean, you can do what you want inside the method onAuthenticationFailure().
XML Config:
<beans:bean id="myFailureHandler" class="my.custom.impl.MyCustomAuthenticationFailureHandler"/>
<security:http ....>
<security:form-login authentication-failure-handler-ref="myFailureHandler" />
</security:http>
Obs.: If what you want to do don't work with this solution you can implement a AuthenticationProvider
答案 3 :(得分:0)
我在使用spring security之前从未这样做过,但我认为这篇文章描述了你需要的一切:http://www.mkyong.com/spring-security/customize-http-403-access-denied-page-in-spring-security/