今天我將我的項目從 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個贊
我設法使用@Profile
and來完成這項工作@ActiveProfiles
。但是我不得不將我的靜態內部@Configuration
類提取到另一個 java 文件中,然后它自動開始工作。仍然沒有找到為什么它在早期版本的 Spring Boot 中工作
添加回答
舉報
0/150
提交
取消