我正在研究一些Spring Security教程并尝试在没有xml的情况下实现它们,我似乎无法找到有关替换默认值的任何内容 UsernamePasswordAuthenticationFilter。
如同 这个问题 我想从登录表单中检索一个额外的参数。我遇到困难的地方是:
<custom-filter ref="customAuthenticationProcessingFilter" position="FORM_LOGIN_FILTER"/>
为了正确设置,我需要从AuthenticationManagerBuilder构建下来吗?还是我错过了什么?
根据此处的Spring Security文档:
http://docs.spring.io/spring-security/site/docs/3.0.x/reference/ns-config.html#filter-stack
FORM_LOGIN_FILTER只是UsernamePasswordAuthenticationFilter类的别名。
所以
http.addFilterBefore(new YourFilter(), UsernamePasswordAuthenticationFilter.class);
应该做的伎俩
谢谢@Juan的好文档链接。但你必须使用 addFilterAt 代替 addFilterBefore。
所以,这是正确的:
http.addFilterAt(newYourFilter(),UsernamePasswordAuthenticationFilter.class);
这将使用CustomFilter替换现有的默认实现。