Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for remote host and port to jersey and security 2.x #2368

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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