Skip to content

Commit

Permalink
Added support for remote host and port to jersey and security. (#2368)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
  • Loading branch information
tomas-langer committed Sep 17, 2020
1 parent c0fc3b9 commit c93c5cb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Expand Up @@ -76,10 +76,6 @@ protected void doFilter(ContainerRequestContext request, SecurityContext securit
return;
}

// The following two lines are not possible in JAX-RS or Jersey - we would have to touch
// underlying web server's request...
//.addAttribute("userIp", req.remoteAddress())
//.addAttribute("userPort", req.remotePort())
URI requestUri = request.getUriInfo().getRequestUri();
String query = requestUri.getQuery();
String origRequest;
Expand All @@ -91,13 +87,25 @@ protected void doFilter(ContainerRequestContext request, SecurityContext securit
Map<String, List<String>> allHeaders = new HashMap<>(filterContext.getHeaders());
allHeaders.put(Security.HEADER_ORIG_URI, List.of(origRequest));

SecurityEnvironment env = SecurityEnvironment.builder(security.serverTime())
SecurityEnvironment.Builder envBuilder = SecurityEnvironment.builder(security.serverTime())
.path(filterContext.getResourcePath())
.targetUri(filterContext.getTargetUri())
.method(filterContext.getMethod())
.headers(allHeaders)
.addAttribute("resourceType", filterContext.getResourceName())
.build();
.addAttribute("resourceType", filterContext.getResourceName());

// The following two lines are not possible in JAX-RS or Jersey - we would have to touch
// underlying web server's request...
String remoteHost = (String) request.getProperty("io.helidon.jaxrs.remote-host");
Integer remotePort = (Integer) request.getProperty("io.helidon.jaxrs.remote-port");
if (remoteHost != null) {
envBuilder.addAttribute("userIp", remoteHost);
}
if (remotePort != null) {
envBuilder.addAttribute("userPort", remotePort);
}

SecurityEnvironment env = envBuilder.build();

EndpointConfig ec = EndpointConfig.builder()
.securityLevels(filterContext.getMethodSecurity().getSecurityLevels())
Expand Down
Expand Up @@ -258,10 +258,18 @@ private void doAccept(ServerRequest req, ServerResponse res) {
requestUri(req),
req.method().name(),
new WebServerSecurityContext(),
new WebServerPropertiesDelegate(req));
new WebServerPropertiesDelegate(req),
null);
// set headers
req.headers().toMap().forEach(requestContext::headers);

// set remote address
String remoteHost = req.remoteAddress();
int remotePort = req.remotePort();

requestContext.setProperty("io.helidon.jaxrs.remote-host", remoteHost);
requestContext.setProperty("io.helidon.jaxrs.remote-port", remotePort);

requestContext.setWriter(responseWriter);

req.content()
Expand Down

0 comments on commit c93c5cb

Please sign in to comment.