对authentication-manager的多个别名来解决有无MD5编码的问题

    本人初出茅庐菜鸟一枚,希望把自己在工作中遇到的问题,并如何解决的记录下来希望对大家都能有所帮助。

和公司有合作关系的另外一个公司和我们整合了,希望调用我们的接口可以无需注册直接访问(两家的用户数据已经同步)。但是登录对方账户时,密码已经进行了MD5编码,调用我们的接口传参数后会又再次对password进行编码。我接到的任务解决这个问题,提供一个不在进行MD5编码的方法。

     公司之前对password转码时用的spring Security,的

spring-security.xml 配置如下

<!-- 指定一个自定义的authentication-manager :filterSecurityUser -->
<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="filterSecurityUser">
        <password-encoder ref="passwordEncoder">
        </password-encoder>
    </authentication-provider>
</authentication-manager>

这时候我想要再在authentication-manager 取另外一个别名使得传过来的password不用MD5转码。

<!-- 指定一个自定义的authentication-manager :filterUser -->
<authentication-manager alias="authenticationMushRoomManager">
    <authentication-provider user-service-ref="filterSecurityUser">
        <password-encoder ref="passwordMushRoomEncoder">
        </password-encoder>
    </authentication-provider>
</authentication-manager>

applicationContext.xml

<!-- security对密码进行MD5编码  -->
<bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.Md5PasswordEncoder"/>
<!-- 明文 -->
<bean id="passwordMushRoomEncoder" class="org.springframework.security.authentication.encoding.PlaintextPasswordEncoder"/>

运行后报错,错误如下:

GalaContext  erro!  Error creating bean with name 'userCenterServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.authentication.AuthenticationManager com.gala.service.impl.UserCenterServiceImpl.authenticationMushRoomManager; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.security.authentication.AuthenticationManager] is defined: expected single matching bean but found 3: org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0,org.springframework.security.authentication.ProviderManager#0,org.springframework.security.authenticationManager
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userCenterServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.authentication.AuthenticationManager com.gala.service.impl.UserCenterServiceImpl.authenticationMushRoomManager; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.security.authentication.AuthenticationManager] is defined: expected single matching bean but found 3: org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0,org.springframework.security.authentication.ProviderManager#0,org.springframework.security.authenticationManager
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(Abst

经过查询发现

<authentication-manager alias="authenticationManager">
默认的只能是一个,如果想要加另外一个必须用id

<!-- 指定一个自定义的authentication-manager :filterUser -->
<authentication-manager id="authenticationMushRoomManager">
    <authentication-provider user-service-ref="filterSecurityUser">
        <password-encoder ref="passwordMushRoomEncoder">
        </password-encoder>
    </authentication-provider>
</authentication-manager>

当然在接口注入的时候要修改

@Autowired
@Qualifier("authenticationMushRoomManager")
AuthenticationManager authenticationMushRoomManager;
@Autowired
AuthenticationManager authenticationManager;

以上就是解决问题的方法了,中间调试的时候总有问题,得感谢大神涛哥的帮助。希望能够尽快成长。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值