英文:
Spring Boot Reactive WebFlux application reuse inbout call's JWT to outbound calls
问题
我有一个Spring Boot响应式应用程序,使用JwtIssuerReactiveAuthenticationManagerResolver(多个发行者)验证入站调用 - 这部分正常工作。但接下来我必须通过WebClient调用其他已配置了不同身份验证的OAuth2注册服务(通过application.properties)来检索结果。但出站调用重用入站JWT令牌进行这些调用。
出站端点通过openapi-generator-maven-plugin导入到我的项目中(使用配置:生成器java,库webclient)。
我曾尝试搜索正确的WebClient配置来初始化ApiClient,但没有一个有效。
英文:
I have spring boot reactive application that validate inbound calls with JwtIssuerReactiveAuthenticationManagerResolver (multiple issuer) - this part working fine. But than I have to call other services via WebClient with already configured OAuth2 registrations (via application.properties) with different authentications to retrieve results. But outbound calls reuse inbound JWT token to this calls.
Outbound endpoints are imported to my project via openapi-generator-maven-plugin (with configuration: generator java, library webclient)
I had try to search for correct configuration for WebClient to initialize ApiClient but non of them works.
答案1
得分: 0
以下是翻译好的部分:
"An option is to retrieve the Bearer
string from the authentication in the security context and manually setting an Authorization
header to WebClient
requests (provided that the configured Authentication
implementation stores the original token string, of course).
If you have configured a JwtIssuerReactiveAuthenticationManagerResolver
, you most probably have a JwtAuthenticationToken
in the security context of authorized requests.
@RestController
@RequiredArgsConstructor
public class SampleController {
private final WebClient otherResourceServer;
@GetMapping("/something")
@PreAuthorize("isAuthenticated()")
public Mono<Object> getSomething(JwtAuthenticationToken auth) throws URISyntaxException {
return otherResourceServer
.get()
.uri("https://some.host/getSomethingElse")
.header(HttpHeaders.AUTHORIZATION, "Bearer %s".formatted(auth.getToken().getTokenValue()))
.exchangeToMono(resp -> resp.bodyToMono(Object.class));
}
}
英文:
An option is to retrieve the Bearer
string from the authentication in the security context and manually setting an Authorization
header to WebClient
requests (provided that the configured Authentication
implementation stores the original token string, of course).
If you have configured a JwtIssuerReactiveAuthenticationManagerResolver
, you most probably have a JwtAuthenticationToken
in the security context of authorized requests.
@RestController
@RequiredArgsConstructor
public class SampleController {
private final WebClient otherResourceServer;
@GetMapping("/something")
@PreAuthorize("isAuthenticated()")
public Mono<Object> getSomething(JwtAuthenticationToken auth) throws URISyntaxException {
return otherResourceServer
.get()
.uri("https://some.host/getSomethingElse")
.header(HttpHeaders.AUTHORIZATION, "Bearer %s".formatted(auth.getToken().getTokenValue()))
.exchangeToMono(resp -> resp.bodyToMono(Object.class));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论