Spring Security 3- How to customize username/password parameters?

前端 未结 2 610
挽巷
挽巷 2020-12-31 18:09

How do you customize the UsernamePasswordAuthenticationFilter usernameParameter (j_username) and passwordParameter (j_password) properties when using the

相关标签:
2条回答
  • 2020-12-31 18:30

    Here is the solution I created based on axtavt's suggestion:

    Spring configuration:

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

    Java class:

    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;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-31 18:30

    Filter is configured using form-login element, but that element doesn't provide ability to set custom names for username and password.

    You can configure directly, as describe in Spring Reference

    0 讨论(0)
提交回复
热议问题