亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring @Configuration 未在測試上下文中覆蓋

Spring @Configuration 未在測試上下文中覆蓋

白板的微信 2022-03-10 22:09:13
今天我將我的項目從 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 中工作


查看完整回答
反對 回復 2022-03-10
  • 1 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號