如何覆盖Spring安全消息“不支持身份验证方法:GET”?
在 spring-security-code-3.1.0.RELEASE.jar->org/springframework/security/messages.properties 中,没有可以覆盖此消息的 key 。
还有其他方法可以覆盖此消息吗?
我只允许服务器使用 post 方法进行身份验证。但是当我使用 get message 直接调用/j_spring_security_check 时,服务器返回上述消息。
提前致谢。
最佳答案
更改消息覆盖 attemptAuthentication
方法UsernamePasswordAuthenticationFilter
类,并设置自定义消息,其中 AuthenticationServiceException
抛出。
--或--
要覆盖 spring security 对于 postOnly 请求的默认行为,如果您不想要特定消息,请使用以下方法:
org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler
将处理所有与身份验证相关的异常。哪个将处理org.springframework.security.authentication.AuthenticationServiceException
当 spring security 配置为 postOnly 时抛出该异常,因此只需在抛出此异常时重定向到页面即可。
要覆盖默认配置,请在applicationContext.xml
中添加以下代码
<bean id="authenticationFailureHandler"
class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler">
<property name="defaultFailureUrl" value="LOGIN_ERROR_PAGE_URL" />
<property name="exceptionMappings">
<props>
<prop key="org.springframework.security.authentication.AuthenticationServiceException">REDIRECT_PAGE_URL</prop>
</props>
</property>
</bean>
关于java - Spring Security 自定义消息 "Authentication method not supported: GET",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21935693/