为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 246 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号