2015-10-08 72 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) 

使您的訪問控制規則是春天開機,否則將配置您的默認設置之前應用。

相關問題