【问题标题】:Can't autowire Spring Security implementation class无法自动装配 Spring Security 实现类
【发布时间】:2017-02-01 06:29:59
【问题描述】:

我正在尝试为我的 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 告诉我

无法自动接线。没有找到“UserAuthService 类型的 bean”

但我在同一个包中有那个 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();
    }
}

那么我该如何解决呢?这里有什么问题?为什么不能自动绑定UserAuthService

【问题讨论】:

  • 除了intellij智能感知错误。尝试运行代码时遇到什么错误?有时,intellij 会出错。
  • 我没有收到任何错误,但我的整个身份验证都不起作用,我认为这正是因为如此。我不知道我是否可以编辑这篇文章,详细说明我的身份验证无效,或者我应该创建一个新的。
  • 我修复了我的另一个问题,这似乎只是 IntelliJ 错误,谢谢。

标签: java spring spring-security


【解决方案1】:

尝试更改代码以注入接口,而不是实现。这是一个事务代理。

private UserDetailsService userAuthService;

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

Spring Autowiring class vs. interface?

【讨论】:

  • 还是不行,现在只是说“无法自动装配。没有找到“UserAuthService 类型的bean”
  • 这是UserDetailsService 不是UserAuthService。确保您进行了正确的更改(2 次更改)。另外,发布错误堆栈跟踪以便更好地理解。
猜你喜欢
  • 1970-01-01
  • 2016-03-29
  • 2015-09-14
  • 2011-01-24
  • 2017-12-18
  • 2016-09-27
  • 2013-01-29
  • 1970-01-01
相关资源
最近更新 更多