Spring security spring安全响应应用程序中的模拟用户

Spring security spring安全响应应用程序中的模拟用户,spring-security,mockito,spring-webflux,spring-test,jwt-auth,Spring Security,Mockito,Spring Webflux,Spring Test,Jwt Auth,我有一个spring应用程序,它使用以下依赖项: <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.3.RELEASE</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId>

我有一个spring应用程序,它使用以下依赖项:

<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.3.RELEASE</version>    

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-oauth2-jose</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-tools</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
但所有测试都通过了,不需要经过身份验证的用户,也不需要授权
我想使用
@MockUser
测试角色。

编辑以获取有关测试的更多信息

这是考试班

@ExtendWith(SpringExtension.class)
@WebFluxTest(controllers = MyController.class)
@Import({MyRepository.class, ValidationConfiguration.class})
public class MyControllerTest extends BaseControllerTest {

    @Autowired
    MyController controller;

    @Test
    public void test() {}

}

您的TestClass是否用@SpringBootTest或@WebFluxTest注释?我不使用SpringBootTest,但我使用WebFluxTest,我使用
@SpringBootTest
@WebFluxTest
一起使用
我发现了此错误
发现@BootstrapWith
的多个声明,SpringBootTest配置为加载/bootstrap整个配置。仅限控制器的WebFluxTest配置。应模拟所有其他类或配置。如果还想测试SecurityConfiguration,请使用
@WebFluxTest(controllers=MyController.class)@Import({SecurityConfiguration.class})
不要同时使用这两种方法。我尝试过,现在出现了以下错误:无法加载ApplicationContext
创建名为“securityWebFilterChain”的bean时出错
无此bean定义异常:没有类型为“org.springframework.security.oauth2.jwt.ReactiveJwtDecoder”的合格bean可用
您的测试类是否用@SpringBootTest Order@WebFluxTest?我没有使用SpringBootTest,但我使用WebFluxTest,我使用
@SpringBootTest
@WebFluxTest
一起使用,我发现了这个错误
发现@BootstrapWith
的多个声明SpringBootTest配置为加载/bootstrap整个配置。仅限控制器的WebFluxTest配置。应模拟所有其他类或配置。如果还想测试SecurityConfiguration,请使用
@WebFluxTest(controllers=MyController.class)@Import({SecurityConfiguration.class})
不要将两者合并。我尝试过,但现在出现了以下错误:无法加载ApplicationContext
创建名为“securityWebFilterChain”的bean时出错
无此bean定义异常:没有“org.springframework.security.oauth2.jwt.ReactiveJwtDecoder”类型的合格bean可用
    protected WebTestClient webTestClient(Object controller) {
    return WebTestClient.bindToController(controller)
            .apply(springSecurity())
            .configureClient()
            .build()
            .mutateWith(mockJwt().authorities(new SecurityConfiguration.GrantedAuthoritiesExtractor()));
}
@ExtendWith(SpringExtension.class)
@WebFluxTest(controllers = MyController.class)
@Import({MyRepository.class, ValidationConfiguration.class})
public class MyControllerTest extends BaseControllerTest {

    @Autowired
    MyController controller;

    @Test
    public void test() {}

}