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

3zwjbxry  于 12个月前  发布在  Spring
关注(0)|答案(3)|浏览(202)

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

dxpyg8gm

dxpyg8gm1#

以下是我根据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;
    }

}

siv3szwd

siv3szwd2#

使用form-login元素配置筛选器,但该元素不提供为用户名和密码设置自定义名称的功能。
您可以直接配置,如Spring参考中所述

6za6bjd0

6za6bjd03#

对于那些使用http XML指令spring配置的人,所需的配置如下

<form-login
     username-parameter="userId" password-parameter="password"  authentication-success-handler-ref="userAuthenticationSuccessHandler"
     authentication-failure-handler-ref="userAuthenticationFailureHandler"  />

字符串

相关问题