2015-10-08 135 views
0

我已创建其他身份验证提供程序。我注册他们如下:春季安全无法添加自定义身份验证提供程序

@Configuration 
@EnableWebMvcSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
class SecurityConfig extends WebSecurityConfigurerAdapter{ 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(tokenAP()); 
     auth.authenticationProvider(usernameAndPasswordAP()); 
     auth.userDetailsService(getUserDetailsService()); 
    } 

后来在我的代码我使用AuthenticationManager来认证用户。问题是我只有一个身份验证提供程序在身份验证管理器中注册,该身份验证提供程序是DaoAuthenticationProvider。它看起来像我的身份验证提供程序根本没有注册。我应该做一些额外的配置,使其工作?我正在使用弹簧启动1.2.6预先感谢任何提示。最好的问候

回答

2

当你重写configure(AuthenticationManagerBuilder auth),底层的AuthenticationManager在2种方式之一暴露:

1)内SecurityConfig,你可以简单地调用authenticationManager()

2)如果你需要的AuthenticationManager你SecurityConfig你之外将需要将其公开为bean,例如:

class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(tokenAP()); 
     auth.authenticationProvider(usernameAndPasswordAP()); 
     auth.userDetailsService(getUserDetailsService()); 
    } 

    @Bean 
    @Override 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
    return super.authenticationManagerBean(); 
    } 
} 
1

我们在Spring Boot Web应用程序中配置身份验证提供程序的方式类似于the example Spring Security Java configuration from the current release reference guide中讨论的方法,该方法修改了默认的自动装配AuthenticationManagerBuilder。用你的方法,它可能看起来像:

@Autowired 
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
    auth.authenticationProvider(tokenAP()) 
    .authenticationProvider(usernameAndPasswordAP()) 
    .userDetailsService(getUserDetailsService()); 
} 

如果我正确地读the Javadocs for the configure(AuthenticationManagerBuilder) method,当你重写此方法,您必须指定自己的AuthenticationManager。通过使用如上所述的自动布线实例,默认AuthenticationManager(即ProviderManager,其转而委托给一个或多个配置的AuthorizationProvider实例)。

您可能还需要与注释您的配置类:

@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 

使您的访问控制规则是春天开机,否则将配置您的默认设置之前应用。

相关问题