1 回答

TA貢獻1793條經驗 獲得超6個贊
您的猜測是正確的:) 您應該AuthenticationManager正確配置。您引用的鏈接沒有明確表明這一點。
配置它的方法有很多種:顯式提供 的實現AuthenticationManager,或者配置將創建 的構建器AuthenticationManager,或者AuthenticationManager通過 XML 進行配置等。下面是配置它的多種可能方法中的 2 種。
1.提供自己的AuthenticationManager
對于某些真正的身份驗證,您可以實現AuthenticationManager基于 LDAP 或 JDBC 的身份驗證。為了演示這個想法,這里有一個虛擬實現,足以使您的代碼運行。
public class DummyAuthenticationManager implements AuthenticationManager {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// Dummy implementation. We don't check anything here.
return authentication;
}
}
在您SecurityConfiguration創建它的實例,如下所示:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return new DummyAuthenticationManager();
}
...
}
通過這些更改,您的代碼將運行,并且您可以繼續逐步擴展它。
2.使用AuthenticationManagerBuilder
AuthenticationManager您可以配置AuthenticationManagerBuilder將為您構建AuthenticationManager所需的內容,而不是實現。
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1").password("password1").roles("USER").and()
.withUser("user2").password("password2").roles("USER").and()
.withUser("admin").password("password3").roles("USER", "ADMIN");
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManager();
}
...
}
通過這些更改,您的代碼將運行,并且您可以繼續逐步擴展它。例如,對于實際的東西,inMemoryAuthentication()您可以使用ldapAuthentication()或jdbcAuthentication()或其他一些配置器。
添加回答
舉報