小编bre*_*Dev的帖子

WireMock 有时表现得很奇怪

在大多数集成测试中,我使用 spring-boot-test(2.1.9.RELEASE) 和 spring-cloud-contract-wiremock(2.0.2.RELEASE)。测试基于 @AutoConfigureWireMock(port = 0) 启动 WireMock 服务器,因此我没有使用任何 WireMockRule 或其他配置设置。

有时验证失败并出现一个非常奇怪的错误:

com.github.tomakehurst.wiremock.client.VerificationException:` com.github.tomakehurst.wiremock.client.VerificationException:com.github.tomakehurst.wiremock.client.VerificationException:没有完全匹配的请求。最相似的请求是:预期:< POST /api/id/delete

但是:< POST /api/id/delete

正如您在上面看到的,预期端点与实际调用完全相同。

你有什么想法 ?或者你以前见过吗?这里有一个未解决的问题: https: //github.com/tomakehurst/wiremock/issues/706,但回复不是很有帮助。

java wiremock spring-boot-test

9
推荐指数
1
解决办法
5045
查看次数

在 CORS spring security + webFlux 中启用通配符

我在使用 spring webFlux 制作的项目中启用了 spring security + CORS。我的问题是我们接受例如来自:http://localhost:4200 的请求。如何让 CORS 接受来自http://*.localhost:4200 的请求, 如http://a.localhost:4200http://b.localhost:4200

我的 CORS 配置如下所示:

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public CorsWebFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);

    config.setAllowedOrigins(corsConfigData.getAllowedOrigins());
    config.setAllowedHeaders(corsConfigData.getAllowedHeaders());
    config.setAllowedMethods(corsConfigData.getAllowedMethods());

    source.registerCorsConfiguration("/**", config);
    return new CorsWebFilter(source);
}
Run Code Online (Sandbox Code Playgroud)

你有什么想法 ???

spring-security spring-boot spring-webflux

4
推荐指数
1
解决办法
4986
查看次数

Reactive-Spring-Security-5.1.3.RELEASE,多重授权

我们有一些端点是安全的,在访问它们之前,我们正在验证 jws 是否正确。为了做到这一点,我们定义了一个 SecurityContext 来实际持久化 Auth pojo 并在下游将其操作到控制器中。SecurityWebFilterChain 配置如下所示:

@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
    return http.csrf().disable()
            .formLogin().disable()
            .logout().disable()
            .httpBasic().disable()
            .securityContextRepository(securityContext)
            .authorizeExchange()
            .anyExchange().authenticated()
            .and()
            .build();
}
Run Code Online (Sandbox Code Playgroud)

调用是内部进行的,我们只是验证了 jws 令牌。

现在一些外部客户端需要与我们集成,我们需要验证一个 jwe 令牌。问题是,我们需要以某种方式告诉 spring-security 验证现有端点 jws 和新端点 jwe。

我尝试指定多个安全匹配器,但失败了:(。你还有其他建议吗?

spring-security spring-boot spring-webflux

2
推荐指数
1
解决办法
1468
查看次数

Spring启动和Gradle多模块项目无法正确加载依赖项

基本上我有一个使用Gradle构建的spring boot项目.该项目有一个根项目,包含另外4个子模块.根项目settings.gradle如下所示:

rootProject.name = 'proj'

include 'proj-app'
include 'proj-integration-tests'
include 'proj-model'
include 'proj-service'
Run Code Online (Sandbox Code Playgroud)

app模块包含spring-boot-gradle-plugin并公开了一些api.

我想要做的是创建仅包含集成测试的proj-integration-tests子模块.问题从这里开始,因为我需要proj-app依赖.

所以在proj-integration-tests中我有build.gradle包含:

dependencies {
  testCompile('org.springframework.boot:spring-boot-starter-web')
  testCompile('org.springframework.boot:spring-boot-starter-test')
  testCompile project(':proj-app')
  testCompile project(':proj-model')
}
Run Code Online (Sandbox Code Playgroud)

自集成测试以来,我需要proj-app依赖:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ProjApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Run Code Online (Sandbox Code Playgroud)

需要启动Spring启动应用程序(ProjApplication.class),它位于proj-app模块中.

我从Gradle得到的错误是:"找不到符号ProjApplication".

为什么Gradle无法正确管理proj-app依赖?提前致谢 ;)

java gradle spring-boot spring-boot-gradle-plugin

1
推荐指数
1
解决办法
2230
查看次数

Spring Reactor 中的模拟服务

让我们来看看这个简单的方法:

public Mono<SuccessResponse> doSomething(){
        return service1.doSomething()
            .then(service2.doSomething2())
            .thenReturn(new SuccessResponse("Awesome")));
}
Run Code Online (Sandbox Code Playgroud)

所以基本上我想在 service1.doSomething() 会抛出错误的情况下测试这个方法:

when(service1.doSomething()).thenReturn(Mono.error(new IllegalStateException("Something bad happened")));
when(service2.doSomething()).thenReturn(Mono.just(new SomeResponse()))

assertThatThrownBy(() -> testedService.doSomething().block())
            .isExactlyInstanceOf(IllegalStateException.class);

verify(service2, never()).doSomething(); //Why this is executed!?
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么 service2.doSomething() 被执行一次?它不应该被执行,因为 service1.doSomething() 在上面抛出一个错误......

java mockito reactor spring-boot spring-webflux

1
推荐指数
1
解决办法
3771
查看次数