【问题标题】:Can't inject dependencies in Spring Security configuration无法在 Spring Security 配置中注入依赖项
【发布时间】:2021-03-18 21:44:44
【问题描述】:

我有一个使用 Spring Security 的 spring 项目。我想将依赖项注入到我的 WebSecurityConfigurerAdapter 扩展类中,但似乎没有注入依赖项。我的控制器也使用依赖注入,它确实在那里工作。

我的 SecSecurity 课程:

@Configuration
@Component
@EnableWebSecurity
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    UserPrincipalDetailsService userPrincipalDetailsService;
    LogoutSuccessHandler handler = new LogoutSuccess();



    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
       auth.authenticationProvider(authenticationProvider());


    }


    @Override
    protected void configure(final HttpSecurity http) throws Exception {
              http.   logout().and().
                      httpBasic()
                      .and()
                      .authorizeRequests()
                      .antMatchers( "/", "/home", "/user", "/vestigingen", "/available", "/logout").permitAll()
                      .anyRequest().authenticated()
                      .and().logout().logoutSuccessHandler(handler).deleteCookies("JSESSIONID").invalidateHttpSession(false).permitAll()
                      .and().csrf()
                      .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());


    }
    @Bean
    public UserDetailsService userDetailsService() {
        return new UserPrincipalDetailsService();
    }



    DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        daoAuthenticationProvider.setUserDetailsService(this.userPrincipalDetailsService);


        return daoAuthenticationProvider;
    }

}

它找不到 userPrincipalDetailsS​​ervice 类的 bean。

UserPrincipalDetailService 类:

@Component
public class UserPrincipalDetailsService implements UserDetailsService {
    private UserRepositorySQL userRepository = new UserRepositorySQL();

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        UserEntity user = userRepository.findUserByUsername(s);

        UserPrincipal userPrincipal = new UserPrincipal(user);

        return userPrincipal;
    }
}

澄清一下:我无法在我的 SecSecurityConfig 类中注入任何依赖项,尽管我已经尝试以多种不同的方式对其进行注释。

【问题讨论】:

    标签: spring spring-security dependency-injection cdi


    【解决方案1】:

    在 UserPrincipalDetailsS​​ervice 类中定义 bean 名称 @Component("userPrincipalDetailsS​​ervice")。

    @Component("userPrincipalDetailsService")
    public class UserPrincipalDetailsService implements UserDetailsService {
        private UserRepositorySQL userRepository = new UserRepositorySQL();
    
        @Override
        public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
            UserEntity user = userRepository.findUserByUsername(s);
    
            UserPrincipal userPrincipal = new UserPrincipal(user);
    
            return userPrincipal;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-17
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 2016-02-03
      • 2016-02-24
      • 2011-06-12
      相关资源
      最近更新 更多