Spring Security WebSecurityConfigurerAdapter:AuthenticationManagerBuilder - 覆盖配置方法或自动装配 globalUserDe

Posted

技术标签:

【中文标题】Spring Security WebSecurityConfigurerAdapter:AuthenticationManagerBuilder - 覆盖配置方法或自动装配 globalUserDetails?【英文标题】:Spring Security WebSecurityConfigurerAdapter: AuthenticationManagerBuilder - override configure method or autowire globalUserDetails? 【发布时间】:2020-07-22 21:13:33 【问题描述】:

尊敬的社区:我写这篇文章是为了询问我必须如何配置 AuthenticationManagerBuilder

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter 
  @Autowired
  private UserDetailsService secUserDetailsService;

  @Autowired
  private PasswordEncoder secPasswordEncoder;

  . . .

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

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception 
      auth.authenticationProvider(authenticationProvider());
  

  @Autowired
  public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception 
    auth.userDetailsService(secUserDetailsService).passwordEncoder(secPasswordEncoder);
  

  @Bean
  public DaoAuthenticationProvider authenticationProvider() 
    DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
    authenticationProvider.setUserDetailsService(secUserDetailsService);
    authenticationProvider.setPasswordEncoder(secPasswordEncoder);
    return authenticationProvider;
  


我应该遵循哪个实现

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception 
      auth.authenticationProvider(authenticationProvider());
  

  @Autowired
  public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception 
    auth.userDetailsService(secUserDetailsService).passwordEncoder(secPasswordEncoder);
  

另外,我发现如果我评论整个 '@Override protected void configure(AuthenticationManagerBuilder auth) 方法,我的应用程序运行良好,但是当我评论 @Autowired public void globalUserDetails(AuthenticationManagerBuilder auth ),出现以下错误:

o.s.s.o.p.endpoint.TokenEndpoint.handleException:169 - Handling error: IllegalStateException, UserDetailsService is required.
java.lang.IllegalStateException: UserDetailsService is required.
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:464)
    at org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper.loadUserDetails(UserDetailsByNameServiceWrapper.java:68)
    at org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider.authenticate(PreAuthenticatedAuthenticationProvider.java:103)
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:175)
...

我应该使用哪种方法?此外,(这是我最关心的问题)如果我同时使用这两种方法,是否存在任何性能问题

【问题讨论】:

如果同时注释掉AuthenticationManager bean 和globalUserDetails 方法,只留下configure(AuthenticationManagerBuilder) 方法,是否有效? @pero_hero 然后应用程序无法启动:AuthorizationServerConfiguration 中的字段 authenticationManager 需要一个找不到的 'org.springframework.security.authentication.AuthenticationManager' 类型的 bean。调用 refreshToken API 时出现上述错误(os.s.o.p.endpoint.TokenEndpoint)。 如果将configure 方法更改为与globalUserDetails 方法相同,会发生什么情况:auth.userDetailsService(secUserDetailsService).passwordEncoder(secPasswordEncoder) @pero_hero 发生了同样的错误。 (UserDetailsService 是必需的)。顺便说一句,我找到了一些有用的链接:github.com/spring-projects/spring-security/issues/4571。但仍然不确定同时使用它们不会导致任何性能问题。到目前为止似乎还可以:p 【参考方案1】:

据此guide

使用的区别

@Overrideconfigure(AuthenticationManagerBuilder auth)

一个@Autowired AuthenticationManagerBuilder 到一个方法中

第一个是创建一个本地AuthenticationManager,如果它无法处理身份验证请求,它会委托给默认的spring-security父AuthenticationManager,而后一个(使用@Autowired)配置一个新的全局 AuthenticationManager 实例。

所以使用globalUserDetails 是完全可以的,但是没有必要同时使用两者,因为你会创建一个本地的,它与父级相同,这是无用的。

【讨论】:

以上是关于Spring Security WebSecurityConfigurerAdapter:AuthenticationManagerBuilder - 覆盖配置方法或自动装配 globalUserDe的主要内容,如果未能解决你的问题,请参考以下文章

Spring Security:2.4 Getting Spring Security

没有 JSP 的 Spring Security /j_spring_security_check

Spring-Security

Spring Security 登录错误:HTTP 状态 404 - /j_spring_security_check

未调用 Spring Security j_spring_security_check

Spring Security入门(3-7)Spring Security处理页面的ajax请求