gpt4 book ai didi

java - 如何在webflux ServerHttpRequest中获取原始url路径

转载 作者:太空宇宙 更新时间:2023-11-04 09:18:19 26 4
gpt4 key购买 nike

如何获取RequestMapping中写入的绝对URL?

下面给出的代码:

@RequestMapping("/user/{id}")
公共(public)用户获取(){
字符串 url = "/user/{id}";
};

最佳答案

ServerHttpRequest 请求 = (ServerHttpRequest) Exchange.getRequest();

使用request.getURI().getPath()我能够获取API路径。

 @Component
public class ServerAuthEntryPointJwt implements ServerAuthenticationEntryPoint {

private static final Logger logger = LoggerFactory.getLogger(ServerAuthEntryPointJwt.class);

@Override
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException ex) {
// TODO Auto-generated method stub
logger.error("Unauthorized error: {}", ex.getMessage());

ServerHttpRequest request = (ServerHttpRequest) exchange.getRequest();
ServerHttpResponse response = (ServerHttpResponse) exchange.getResponse();

// response.set(MediaType.APPLICATION_JSON_VALUE);
response.setStatusCode(HttpStatus.UNAUTHORIZED);

final Map<String, Object> body = new HashMap<>();
body.put("status", HttpStatus.UNAUTHORIZED);
body.put("error", "Unauthorized");
body.put("message", ex.getMessage());
body.put("path", request.getURI().getPath());
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json");
JSONObject fadfa = new JSONObject(body);
byte[] bytes = fadfa.toString().getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(bytes);
return exchange.getResponse().writeWith(Flux.just(buffer));

}

}

回应:

{
"path": "/admin-service/conversation-management/conversations",
"error": "Unauthorized",
"message": "Not Authenticated",
"status": "UNAUTHORIZED"
}

关于java - 如何在webflux ServerHttpRequest中获取原始url路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58678562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com