使用GlobalMethodSecurity定制UserDetailService循环引用

时间:2016-04-20 20:36:22

标签: java spring spring-security spring-java-config

我正在尝试将security.xml转换为JavaConfig

**
 *  <security:global-method-security
 secured-annotations="enabled"
 jsr250-annotations="enabled"
 access-decision-manager-ref="accessDecisionManager">
 </security:global-method-security>
 */
@Configuration
@EnableGlobalMethodSecurity( jsr250Enabled = true, securedEnabled = true )
public class SecurityConfig
{
    private UserDetailsService userDetailsService;
    /**
     *  <bean id="accessDecisionManager"
     class="org.springframework.security.access.vote.AffirmativeBased">
     <constructor-arg>
     <list>
     <bean class="com.myapp.security.access.voter.MyAppRoleVoter">
     <property name="rolePrefix" value="" />
     </bean>
     <bean class="org.springframework.security.access.vote.AuthenticatedVoter" />
     </list>
     </constructor-arg>
     </bean>
     */
    @Bean
    AccessDecisionManager accessDecisionManager( ) {
        return new AffirmativeBased( Collections.singletonList( new MyAppRoleVoter() ) );
    }

    /**
     *  <security:authentication-manager alias="authenticationManager">
     <security:authentication-provider user-service-ref="clientDetailsUserService"/>
     <security:authentication-provider user-service-ref="principalService">
     <security:password-encoder ref="passwordEncoder" />
     </security:authentication-provider>
     </security:authentication-manager>
     */
    @Autowired
    void configureGlobal(
        final AuthenticationManagerBuilder auth,
        @Qualifier( PasswordEncryptionConfig.PASSWORD_ENCODER ) final PasswordEncoder passwordEncoder
    ) throws Exception {
        auth.userDetailsService( userDetailsService ).passwordEncoder( passwordEncoder );
    }

    @Autowired
    public void setUserDetailsService(
        @Qualifier( PrincipalService.BEAN_ID ) final UserDetailsService userDetailsService )
    {
        this.userDetailsService = userDetailsService;
    }
}

这个例外并不特别令人惊讶

 Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.myapp.config.SecurityConfig.setUserDetailsService(org.springframework.security.core.userdetails.UserDetailsService); nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'methodSecurityInterceptor': Requested bean is currently in creation: Is there an unresolvable circular reference?

如何编写我的javaconfig,以便它与之前的XML做同样的事情? (更新:在撰写本文时,这是在平台BOM 2.0.5上的Spring Security 4.0.4中发生的)

0 个答案:

没有答案