本文介绍了这是为什么BeanPostProcessor的需要,除了在今年春季3.0认证例如UserDetailsS​​ervice的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解一个Spring 3.0应用程序,它包含以下BeanPostProcessor的实现。这是code需要什么样的呢?我以为的UserDetailsS​​ervice足以用于获取和设置用户帐户信息。

  @Service
公共类UserPassAuthFilterBeanPostProcessor实现的BeanPostProcessor
{    / **
     *用户名参数。
     * /
    私人字符串usernameParameter;    / **
     *密码参数。
     * /
    私人字符串passwordParameter;    @覆盖
    公众最终目标postProcessAfterInitialization(最终目标豆,最后弦乐beanName)
    {
        返回豆;
    }    @覆盖
    公众最终目标postProcessBeforeInitialization(最终目标豆,最后弦乐beanName)
    {
        如果(豆的instanceof UsernamePasswordAut​​henticationFilter)
        {
            最后UsernamePasswordAut​​henticationFilter过滤=(UsernamePasswordAut​​henticationFilter)豆;
            filter.setUsernameParameter(getUsernameParameter());
            filter.setPasswordParameter(getPasswordParameter());
        }        返回豆;
    }    / **
     *设置用户名参数。
     *
     * @参数usernameParameter
     * username参数
     * /
    公众最终无效setUsernameParameter(最后弦乐usernameParameter)
    {
        this.usernameParameter = usernameParameter;
    }    / **
     *获取用户名参数。
     *
     返回:用户名参数
     * /
    公共最后弦乐getUsernameParameter()
    {
        返回usernameParameter;
    }    / **
     *设置密码参数。
     *
     * @参数passwordParameter
     * password参数
     * /
    公众最终无效setPasswordParameter(最后弦乐passwordParameter)
    {
        this.passwordParameter = passwordParameter;
    }    / **
     *获取密码参数。
     *
     返回:password参数
     * /
    公共最后弦乐getPasswordParameter()
    {
        返回passwordParameter;
    }}


解决方案

的UserDetailsS​​ervice 就足够了。

的BeanPostProcessor ​​变化的登录请求的用户名和密码参数(在登录表单字段即名称)的名称 - 这些属性无法通过命名空间配置配置,并使用 BeanPostProcessor的,以定制这种特性是一个丑陋但相当普遍的做法:S

I'm trying to understand a Spring 3.0 application which contains the following BeanPostProcessor implementation. What is this code needed for? I thought the UserDetailsService was sufficient for getting and setting the User account information.

@Service
public class UserPassAuthFilterBeanPostProcessor implements BeanPostProcessor
{

    /**
     * The username parameter.
     */
    private String usernameParameter;

    /**
     * The password parameter.
     */
    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;
    }

    /**
     * Sets the username parameter.
     * 
     * @param usernameParameter
     *         the username parameter
     */
    public final void setUsernameParameter(final String usernameParameter)
    {
        this.usernameParameter = usernameParameter;
    }

    /**
     * Gets the username parameter.
     * 
     * @return the username parameter
     */
    public final String getUsernameParameter()
    {
        return usernameParameter;
    }

    /**
     * Sets the password parameter.
     * 
     * @param passwordParameter
     *         the password parameter
     */
    public final void setPasswordParameter(final String passwordParameter)
    {
        this.passwordParameter = passwordParameter;
    }

    /**
     * Gets the password parameter.
     * 
     * @return the password parameter
     */
    public final String getPasswordParameter()
    {
        return passwordParameter;
    }

}
解决方案

Yes, UserDetailsService is sufficient.

This BeanPostProcessor changes the names of username and password parameters in login request (i.e. names of fields in login form) - these properties can't be configured via namespace configuration, and using BeanPostProcessorss in order to customize such properties is an ugly but quite common practice.

这篇关于这是为什么BeanPostProcessor的需要,除了在今年春季3.0认证例如UserDetailsS​​ervice的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 09:09