java - 无法 Autowiring Spring Security 实现类

标签 java spring spring-security

我正在尝试为我的 REST API 制作 BasicAuth,这是我的问题。

帐户配置

// Spring Security uses accounts from our database
@Configuration
public class AccountConfiguration extends GlobalAuthenticationConfigurerAdapter {

    private UserAuthService userAuthService;

    @Autowired
    public AccountConfiguration(UserAuthService userAuthService) {
        this.userAuthService = userAuthService;
    }

    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userAuthService);
    }
}

在构造函数中 IntelliJ 告诉我

Could not autowire. No beans of "UserAuthService type found

但我在同一个包中有那个 bean,它是:

@Service
@Transactional
public class UserAuthService implements UserDetailsService {

    private UserRepository userRepository;

    @Autowired
    public UserAuthService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(username);

        if (user == null) {
            throw new UsernameNotFoundException("Could not find the user: " + username);
        }

        return new org.springframework.security.core.userdetails.User(
                user.getUsername(),
                user.getPassword(),
                true,
                true,
                true,
                true,
                AuthorityUtils.createAuthorityList("USER"));
    }
}

这是我的 Spring Security 的第三个配置文件:

@EnableWebSecurity
@Configuration
public class WebConfiguration extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // allow everyone to register an account; /console is just for testing
        http
            .authorizeRequests()
                .antMatchers("/register", "/console/**").permitAll();

        http
            .authorizeRequests()
                .anyRequest().fullyAuthenticated();

        // making H2 console working
        http
            .headers()
                .frameOptions().disable();

        /*
        https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html#when-to-use-csrf-protection
        for non-browser APIs there is no need to use csrf protection
        */
        http
            .csrf().disable();
    }
}

那么我该如何解决这个问题呢?这里有什么问题?为什么它无法 Autowiring UserAuthService

最佳答案

尝试更改代码以注入(inject)接口(interface),而不是实现。它是一个事务代理。

private UserDetailsService userAuthService;

@Autowired
public AccountConfiguration(UserDetailsService userAuthService) {
    this.userAuthService = userAuthService;
}

Spring Autowiring class vs. interface?

关于java - 无法 Autowiring Spring Security 实现类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41968509/

相关文章:

java - Spring Hibernate,尝试获取实体时堆栈溢出

java - 将 vector 转换为列表

java - 无法执行目标 org.openjfx :javafx-maven-plugin

java - 如何根据用户设置在 Spring MVC 中创建动态格式化程序?

security - 如何在Spring Security 3中为匿名用户从数据库加载角色?

java - 盈透证券 API - 执行多笔交易

java - Lambda - ClassNotFoundException 异常

spring - 无法执行目标 org.maven.plugins :maven-compiler-plugin:3. 8.1:compile (default-compile) on project (target 11)

java - tomcat重启后基于spring security java的配置

java - 使用浏览器获取请求发送 JWT token