gpt4 book ai didi

java - Spring Security 3-如何自定义用户名/密码参数?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:29:13 25 4
gpt4 key购买 nike

使用 <http ... /> 时如何自定义 UsernamePasswordAuthenticationFilter usernameParameter (j_username) 和 passwordParameter (j_password) 属性Spring Security 3 命名空间?这是我的理解 <http ... />创建过滤器,但我不知道如何自定义它。

最佳答案

这是我根据 axtavt 的建议创建的解决方案:

Spring 配置:

<beans:bean id="userPassAuthFilterBeanPostProcessor"
class="com.my.package.UserPassAuthFilterBeanPostProcessor">
<beans:property name="usernameParameter" value="username" />
<beans:property name="passwordParameter" value="password" />
</beans:bean>

Java 类:

package com.my.package;

import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.security.web.authentication.
UsernamePasswordAuthenticationFilter;

public class UserPassAuthFilterBeanPostProcessor implements BeanPostProcessor {

private String usernameParameter;
private String passwordParameter;

@Override
public final Object postProcessAfterInitialization(final Object bean,
final String beanName) {
return bean;
}

@Override
public final Object postProcessBeforeInitialization(final Object bean,
final String beanName) {
if (bean instanceof UsernamePasswordAuthenticationFilter) {
final UsernamePasswordAuthenticationFilter filter =
(UsernamePasswordAuthenticationFilter) bean;
filter.setUsernameParameter(getUsernameParameter());
filter.setPasswordParameter(getPasswordParameter());
}

return bean;
}

public final void setUsernameParameter(final String usernameParameter) {
this.usernameParameter = usernameParameter;
}

public final String getUsernameParameter() {
return usernameParameter;
}

public final void setPasswordParameter(final String passwordParameter) {
this.passwordParameter = passwordParameter;
}

public final String getPasswordParameter() {
return passwordParameter;
}

}

关于java - Spring Security 3-如何自定义用户名/密码参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2483992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com