如何覆盖 spring 安全消息"不支持身份验证方法:GET"?
在spring-security-code-3.1.0.RELEASE.jar->org/springframework/security/messages.properties中,没有键可以覆盖此消息。
还有其他方法可以覆盖此消息吗?
我只允许服务器使用post方法进行身份验证。但是当我使用 get 消息直接调用/j_spring_security_check 时,服务器会返回上面的消息。
提前谢谢。
更改消息覆盖attemptAuthentication
UsernamePasswordAuthenticationFilter
类的方法,并在抛出AuthenticationServiceException
位置设置自定义消息。
参考: 弹簧源代码链接
--或--
要覆盖 postOnly 请求的 spring 安全性的默认行为,如果您不想要特定消息,请使用以下方法: org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler
将处理所有与身份验证相关的异常。这将处理当 spring 安全性配置为 postOnly 时抛出org.springframework.security.authentication.AuthenticationServiceException
,因此只需在抛出此异常时重定向到页面即可。
要覆盖默认配置,请在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>
当应用程序收到/j_spring_security_check
请求时,将调用配置的AbstractAuthenticationProcessingFilter
子类。
在我的应用程序中,它是 UsernamePasswordAuthenticationFilter
,默认情况下postOnly
设置为 true。
要将 postOnly 覆盖为 false,您需要更新身份验证处理过滤器的 Bean 定义,如下所示:
<bean id="authenticationFilter"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"
p:postOnly="false" />
在 Web 中还需要进行以下更改.xml
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
希望这对你有帮助。
希希尔