2014-02-21 35 views
2

如何覆盖春季安全消息“验证方法不支持:GET”?春季安全自定义消息“验证方法不支持:GET”

在弹簧安全码3.1.0.RELEASE.jar->组织/ springframework的/安全/ messages.properties没有键以覆盖此消息。

是否有任何其他方式来重写此消息?

我只允许服务器使用post方法进行身份验证。但是当我使用get message直接调用/ j_spring_security_check时,服务器返回上面的消息。

在此先感谢。

回答

2

要更改消息超驰UsernamePasswordAuthenticationFilter类的attemptAuthentication方法,并且其中AuthenticationServiceException抛出设置的自定义消息。

编号:Spring Source Code Link

- 或 -

要覆盖弹簧安全的默认行为postOnly请求时,如果你不想让特定的消息,然后使用下面的方法: org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler将处理所有认证相关的例外。这将处理弹簧安全配置为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>

1

当应用程序收到/j_spring_security_check请求,的AbstractAuthenticationProcessingFilter配置子类被调用。

在我的应用程序中,它是UsernamePasswordAuthenticationFilter,默认postOnly设置为true。

要覆盖postOnly为假,则需要更新,如下验证处理过滤器的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> 

以下变化希望这可以帮助你。

Shishir

+0

我需要使用浏览器时,用户直接打'/ j_spring_security_check'则此消息会来得这么只是想重写只只允许后,但这条信息。我不喜欢支持GET。那么有什么方法可以改变信息吗? –