没有用于AuthenticationManagerBuilder的bean

时间:2017-12-09 09:48:39

标签: spring unit-testing security junit spring-security-oauth2

我正在使用OAuth2进行基本身份验证的一些教程。整件事都有效,但测试没有。甚至有简单的班级单元测试无法正常工作。我有:

No qualifying bean of type org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder'.

AuthorizationServerConfiguration:

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
private AuthenticationManager authenticationManager;
@Autowired
public AuthorizationServerConfig(AuthenticationManager authenticationManager, PasswordEncoder passwordEncoder){
    this.authenticationManager = authenticationManager;
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.authenticationManager(authenticationManager);
}

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory().withClient("clientId")
            .authorizedGrantTypes("client_credentials", "password")
            .authorities("ROLE_CLIENT","ROLE_TRUSTED_CLIENT").scopes("read","write","trust")
            .resourceIds("oauth2-resource").accessTokenValiditySeconds(3600).secret("secret");
}

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
    security.checkTokenAccess("isAuthenticated()");
}
}

UserDetails实施:

public class CustomUserDetails implements UserDetails {
private static final long serialVersionUID = -3962786160780714576L;
private Collection<? extends GrantedAuthority> authorities;
private String password;
private String username;
private boolean enabled;

public CustomUserDetails(User user) {
    this.username = user.getUsername();
    this.password = user.getPassword();
    this.authorities = translate(user.getRoles());
    this.enabled = user.getEnabled();
}

private Collection<? extends GrantedAuthority> translate(List<Role> roles) {
    List<GrantedAuthority> authorities = new ArrayList<>();
    roles.stream().forEach(x -> authorities.add(new SimpleGrantedAuthority(x.getName())));
    return authorities;
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
    return authorities;
}

@Override
public String getPassword() {
    return password;
}

@Override
public String getUsername() {
    return username;
}

@Override
public boolean isAccountNonExpired() {
    return true;
}

@Override
public boolean isAccountNonLocked() {
    return true;
}

@Override
public boolean isCredentialsNonExpired() {
    return true;
}

@Override
public boolean isEnabled() {
    return enabled;
}
}

ResourceServerConfigurerAdapter:

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
    http.headers().frameOptions().disable().and()
            .authorizeRequests()
            .antMatchers("/","/home","/register","/login").permitAll()
            .antMatchers("/dev/hello-world").authenticated();
}
}


@SpringBootApplication
public class LimeApplication {
    public static void main(String[] args) {
        SpringApplication.run(LimeApplication.class, args);
    }

主要课程:

@SpringBootApplication
public class SomeApplication {
    public static void main(String[] args) {
        SpringApplication.run(SomeApplication.class, args);
    }

@Autowired
public void authenticationManager(AuthenticationManagerBuilder builder, IUserDAO dao, PasswordEncoder encoder) throws Exception {
    builder.userDetailsService(userDetailsService(dao)).passwordEncoder(encoder);
}


private UserDetailsService userDetailsService(final IUserDAO dao) {
    return username -> new CustomUserDetails(dao.findByUsername(username));
}

}

application.yml

security:
  oauth2:
    resource:
      filter-order: 3

我正在寻找,我试图通过

test/resources/application.yml中禁用身份验证
security:
  basic:
    enabled: false

但这不起作用。我找不到任何答案如何纠正或嘲笑这个。谁能帮我?有谁知道如何嘲笑它?或者在测试中获得这个bean?

编辑: 测试正在使用@DataJpaTest @RunWith(SpringRunner.class)注释

运行

0 个答案:

没有答案