Как создать настраиваемый ответ, когда spring -security получает нулевые учетные данные (имя пользователя и пароль)?
Я использую почтальон для отправки имени пользователя и пароля без значения на мой сервер; это как username=null
и password=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
Ответы
Ответ 1
Используйте стандартное исключение из безопасности spring, оно будет обрабатываться само по себе, если у вас уже есть обработчик исключений для преобразования сообщений в ответ Json.
catch (Exception exception)
{
throw new AuthenticationCredentialsNotFoundException("Fields must not be empty", exception);
}
Ответ 2
Вы передаете базовую аутентификацию String ":" (после декодирования base64), так как вы говорите, что это приводит к пустующему паролю имя пользователя. BasicAuthenticationFilter
передает их провайдеру аутентификации, который является вашим пользовательским кодом (SecurityAuthenticationProvider
), поэтому невозможно точно сказать, что он делает. Где-то там вы создаете экземпляр User
с этими значениями, который вызывает исключение, которое вы видите. Вместо этого вы должны проверить пустые значения в AuthenticationProvider
и выбросить Authenticationexception
.
Вам также необходимо переопределить функцию onUnsuccessfulAuthentication
в BasicAuthenticationFilter
, чтобы записать требуемый ответ об ошибке. Вам нужно будет настроить его как настраиваемый фильтр, а не использовать элемент <security:http-basic />
.
Ответ 3
Что вы можете сделать, это определить 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
Обь.: Если то, что вы хотите сделать, не работает с этим решением, вы можете реализовать AuthenticationProvider
Ответ 4
Я никогда не делаю этого перед использованием безопасности spring, но я думаю, что в этой статье описывается все, что вам нужно: http://www.mkyong.com/spring-security/customize-http-403-access-denied-page-in-spring-security/