I am now using spring cloud gateway, from the spring could gateway filter I could get the ServerHttpRequest which come from org.springframework.http.server like this:
ServerHttpRequest request = (ServerHttpRequest) exchange.getRequest();
but the public library function just only accept HttpServletRequest which was in the package javax.servlet.http, is it possible to translate the two type of object? or must I write a overload function with different type of parameter? should I implement twice with the same function? BTW, this is my public function:
public static void handleLoginCheck(HttpServletRequest httpServletRequest) {
AutoHeaderInfoRequest autoHeaderInfoRequest = AuthUtil.getAutoHeaderInfoRequest(httpServletRequest);
if (autoHeaderInfoRequest.getAccessToken() == null) {
throw ServiceException.NOT_LOGGED_IN_EXCEPTION;
}
if (!AuthUtil.verifyAccessToken(autoHeaderInfoRequest.getAccessToken())) {
throw GlobalException.ACCESS_TOKEN_INVALID_EXCEPTION;
}
setRequestGlobalHeader(autoHeaderInfoRequest);
}
this function and all invoked function use HttpServletRequest.
According to Spring Docs:
ServerHttpRequestinterface implementation is based onHttpServletRequestinterface.
Two points to be noted before you proceed:
The ServerWebExchange.getRequest() returns org.springframework.http.server.reactive.ServerHttpRequest and not org.springframework.http.server.ServerHttpRequest. Otherwise, you will get java.lang.ClassCastException. So, modify your code.
It is rather impossible to return HttpServletRequest from org.springframework.http.server.reactive.ServerHttpRequest and also org.springframework.http.server.reactive.ServletServerHttpServlet is a private class and can't be helpful in this case. Previously, considering the approach with org.springframework.http.server.ServletServerHttpServlet was my bad.
Spring team also doesn't recommend the use of servlets in webflux application.
You have to overload the public method and also modify the method calls associated with this :
public static void handleLoginCheck(ServerHttpRequest request) {
HttpHeaders headers = request.getHeaders();
if (headers.get("access_token") == null) {
throw ServiceException.NOT_LOGGED_IN_EXCEPTION;
}
if (!AuthUtil.verifyAccessToken(headers.get("access_token"))) {
throw GlobalException.ACCESS_TOKEN_INVALID_EXCEPTION;
}
setRequestGlobalHeader(headers);
}
Refer docs for more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With