Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我开始使用方法安全性时,为什么我的AuthenticationManager在测试中被初始化了两次?_Java_Spring_Spring Security_Spring Boot_Auth0 - Fatal编程技术网

Java 当我开始使用方法安全性时,为什么我的AuthenticationManager在测试中被初始化了两次?

Java 当我开始使用方法安全性时,为什么我的AuthenticationManager在测试中被初始化了两次?,java,spring,spring-security,spring-boot,auth0,Java,Spring,Spring Security,Spring Boot,Auth0,我有一个工作的Spring MVC应用程序,具有以下安全配置: package co.masslab.shiba; // imports snipped @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private Auth0AuthenticationEntryPoint auth0EntryPoint; @Au

我有一个工作的Spring MVC应用程序,具有以下安全配置:

package co.masslab.shiba;
// imports snipped

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private Auth0AuthenticationEntryPoint auth0EntryPoint;

    @Autowired
    private Auth0AuthenticationFilter auth0Filter;

    @Autowired
    private Auth0AuthenticationProvider auth0AuthenticationProvider;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests().anyRequest().hasRole("USER")
            .and()
            .exceptionHandling().authenticationEntryPoint(auth0EntryPoint)
            .and()
            .addFilterAfter(auth0Filter, SecurityContextPersistenceFilter.class)
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .csrf().disable();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(auth0AuthenticationProvider);
    }
}
当我将注释
@EnableGlobalMethodSecurity(preprestenabled=true)
添加到WebSecurityConfig类中,并将
@PreAuthorize
注释添加到控制器类中的方法中时,代码仍然编译干净,运行良好,行为符合预期。但是,我的测试开始为
AuthenticationManager
抛出一个
AlreadyBuiltException
。我得到的堆栈跟踪是解释Fermat对于这个文本框来说太大了,但是最外面的线程给出了以下原因(添加了换行符):

由于初始化只在测试时失败,我假设问题在于我的测试类,但上面的错误是由以下空洞的测试类生成的:

package co.masslab.shiba;
// imports snipped

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=ShibaApplication.class)
@WebAppConfiguration
public class ShibaApplicationTests {

    @Test
    public void contextLoads() {
    }
}

方法安全性和此测试的结合会导致Spring再次尝试构建
AuthenticationManager

这是上面的测试类与其他测试类的注释方式不同吗?@holmis83:所有测试类的注释方式都相同,并且它们都抛出了相同的错误。这是上面的测试类与其他测试类的注释方式不同吗?@holmis83:测试类的注释方式都相同,他们都犯了同样的错误。
package co.masslab.shiba;
// imports snipped

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=ShibaApplication.class)
@WebAppConfiguration
public class ShibaApplicationTests {

    @Test
    public void contextLoads() {
    }
}