今天我将我的项目从 Spring Boot 1.5.9 更新到 2.1.1,我的一些测试停止工作。当我开始测试时,控制台上会弹出错误:com.example.rest.config.SecurityConfig 中的字段 authEntryPoint 需要找不到类型为“com.example.rest.service.auth.entrypoints.AuthenticationEntryPoint”的 bean。问题是我在我的 SecurityConfig 类中定义了这种类型的 bean,但是我在 TestApplication 类的测试包中覆盖了这个配置。安全配置在那里定义为静态内部类。我尝试了不同的方法,包括 Spring 配置文件和 @Primary 注释,但似乎没有任何效果,而且 Spring 没有像以前那样选择我的测试配置。只有当我删除了 SecurityConfig 类的非测试版本并且测试版本变成了这种类型的唯一 bean 时才起作用。有人可以告诉我如何覆盖这个原始配置或如何关闭 Spring Security 以进行测试吗?或者也许有一种方法可以强制 Spring 不选择那个非测试 @Configuration bean?安全配置类 @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired AuthenticationEntryPoint authEntryPoint; @Autowired BasicAuthenticationProvider basicAuthProvider; @Autowired PreAuthenticatedUserDetailsService preAuthUserDetailsService; @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/rest/query/id/*/user/*", "/rest/files/**/*").hasAnyRole("CLIENT", "SYSTEM") .antMatchers("/public/api/management/**/*").hasRole("SYSTEM") .antMatchers("/public/api/**/*").hasAnyRole("SYSTEM", "USER") .antMatchers("/rest/**/*").hasRole("SYSTEM") .and() .x509() .userDetailsService(preAuthUserDetailsService) .and() .httpBasic() .authenticationEntryPoint(authEntryPoint) .and() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and().csrf().disable(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(basicAuthProvider); }
1 回答
千万里不及你
TA贡献1784条经验 获得超9个赞
我设法使用@Profileand来完成这项工作@ActiveProfiles。但是我不得不将我的静态内部@Configuration类提取到另一个 java 文件中,然后它自动开始工作。仍然没有找到为什么它在早期版本的 Spring Boot 中工作
添加回答
举报
0/150
提交
取消
