我有很多方法使用 Spring 的 WebClient 中的 onStatus API:
@Override
public Mono<Accommodation> createAccommodation(CreateAccommodation create) {
return webClient
.post()
.contentType(APPLICATION_JSON)
.bodyValue(create)
.retrieve()
.onStatus(HttpStatus::isError,
clientResponse -> clientResponse
.bodyToMono(ApiErrorResponse.class)
.flatMap(errorResponse -> Mono.error(new ResponseStatusException(
HttpStatus.valueOf(errorResponse.getStatus()),
errorResponse.getMessage()
))))
.bodyToMono(Accommodation.class);
}
Run Code Online (Sandbox Code Playgroud)
我想做的是避免在每个 WebClient 调用中都使用“onStatus”。
在构建 WebClient 实例时有没有办法设置它?你能举一些例子吗?
这是我的 WebClient 实例:
public AccommodationServiceClientImpl(WebClient.Builder builder) {
this.webClient = builder
.baseUrl("lb://accommodation-service/api/v1/accommodations")
.build();
}
Run Code Online (Sandbox Code Playgroud) 因此,我正在开发一个资源服务器(一个 Spring Boot 应用程序),并且我想利用 Spring Security OAuth2 资源服务器库的优点。
我现在面临的问题是授权服务器(另一个 Spring Boot 应用程序)使用对称密钥对 JWT 进行签名,该密钥很久以前就设置为一个非常短的字符串,并且我无法更改。
我按照 Spring Security 文档尝试了这个:
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests(authorize -> authorize
.anyRequest().permitAll())
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
}
@Bean
public JwtDecoder jwtDecoder(@Value("${jwt.secret-key}") String secretKey) {
return NimbusJwtDecoder
.withSecretKey(new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "HS512"))
.macAlgorithm(MacAlgorithm.HS512)
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
Caused by: com.nimbusds.jose.KeyLengthException: The secret length must be at least 256 bits
at com.nimbusds.jose.crypto.impl.MACProvider.<init>(MACProvider.java:118) ~[nimbus-jose-jwt-9.10.1.jar:9.10.1]
at com.nimbusds.jose.crypto.MACVerifier.<init>(MACVerifier.java:168) ~[nimbus-jose-jwt-9.10.1.jar:9.10.1]
at com.nimbusds.jose.crypto.MACVerifier.<init>(MACVerifier.java:81) ~[nimbus-jose-jwt-9.10.1.jar:9.10.1]
at com.nimbusds.jose.crypto.MACVerifier.<init>(MACVerifier.java:113) …Run Code Online (Sandbox Code Playgroud) 正如标题所说,想知道在使用 Mono/Flux 时,是否有任何等效(或类似)的运算符可以从 Java Stream API 中查看和 ifPresent。