今天我將我的項(xiàng)目從 Spring Boot 1.5.9 更新到 2.1.1,我的一些測(cè)試停止工作。當(dāng)我開始測(cè)試時(shí),控制臺(tái)上會(huì)彈出錯(cuò)誤:com.example.rest.config.SecurityConfig 中的字段 authEntryPoint 需要找不到類型為“com.example.rest.service.auth.entrypoints.AuthenticationEntryPoint”的 bean。問題是我在我的 SecurityConfig 類中定義了這種類型的 bean,但是我在 TestApplication 類的測(cè)試包中覆蓋了這個(gè)配置。安全配置在那里定義為靜態(tài)內(nèi)部類。我嘗試了不同的方法,包括 Spring 配置文件和 @Primary 注釋,但似乎沒有任何效果,而且 Spring 沒有像以前那樣選擇我的測(cè)試配置。只有當(dāng)我刪除了 SecurityConfig 類的非測(cè)試版本并且測(cè)試版本變成了這種類型的唯一 bean 時(shí)才起作用。有人可以告訴我如何覆蓋這個(gè)原始配置或如何關(guān)閉 Spring Security 以進(jìn)行測(cè)試嗎?或者也許有一種方法可以強(qiáng)制 Spring 不選擇那個(gè)非測(cè)試 @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貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊
我設(shè)法使用@Profile
and來完成這項(xiàng)工作@ActiveProfiles
。但是我不得不將我的靜態(tài)內(nèi)部@Configuration
類提取到另一個(gè) java 文件中,然后它自動(dòng)開始工作。仍然沒有找到為什么它在早期版本的 Spring Boot 中工作
添加回答
舉報(bào)
0/150
提交
取消