{"action":"created","issue":{"url":"https://api.github.com/repos/spring-cloud/spring-cloud-netflix/issues/1392","repository_url":"https://api.github.com/repos/spring-cloud/spring-cloud-netflix","labels_url":"https://api.github.com/repos/spring-cloud/spring-cloud-netflix/issues/1392/labels{/name}","comments_url":"https://api.github.com/repos/spring-cloud/spring-cloud-netflix/issues/1392/comments","events_url":"https://api.github.com/repos/spring-cloud/spring-cloud-netflix/issues/1392/events","html_url":"https://github.com/spring-cloud/spring-cloud-netflix/issues/1392","id":182471494,"number":1392,"title":"How to modify authorization header of a request before forwarding to microservice from gateway.","user":{"login":"grovermanas","id":20453088,"avatar_url":"https://avatars.githubusercontent.com/u/20453088?v=3","gravatar_id":"","url":"https://api.github.com/users/grovermanas","html_url":"https://github.com/grovermanas","followers_url":"https://api.github.com/users/grovermanas/followers","following_url":"https://api.github.com/users/grovermanas/following{/other_user}","gists_url":"https://api.github.com/users/grovermanas/gists{/gist_id}","starred_url":"https://api.github.com/users/grovermanas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/grovermanas/subscriptions","organizations_url":"https://api.github.com/users/grovermanas/orgs","repos_url":"https://api.github.com/users/grovermanas/repos","events_url":"https://api.github.com/users/grovermanas/events{/privacy}","received_events_url":"https://api.github.com/users/grovermanas/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-10-12T09:00:23Z","updated_at":"2016-10-12T16:38:29Z","closed_at":null,"body":"In my application, I am enabling security using authentication server. Once oauth2 token is validated , I generate a jwt token which is sent back to Gateway by Auth server. Now , before forwarding the actual request to respective microservice , I need to add this jwt token in as authorization bearer token to my original request . I tried using HttpServletRequestWrapper by overriding the getHeader(String) method but get null whenever I do getHeader() in the microservice. Looking for a way to achieve this."},"comment":{"url":"https://api.github.com/repos/spring-cloud/spring-cloud-netflix/issues/comments/253267241","html_url":"https://github.com/spring-cloud/spring-cloud-netflix/issues/1392#issuecomment-253267241","issue_url":"https://api.github.com/repos/spring-cloud/spring-cloud-netflix/issues/1392","id":253267241,"user":{"login":"grovermanas","id":20453088,"avatar_url":"https://avatars.githubusercontent.com/u/20453088?v=3","gravatar_id":"","url":"https://api.github.com/users/grovermanas","html_url":"https://github.com/grovermanas","followers_url":"https://api.github.com/users/grovermanas/followers","following_url":"https://api.github.com/users/grovermanas/following{/other_user}","gists_url":"https://api.github.com/users/grovermanas/gists{/gist_id}","starred_url":"https://api.github.com/users/grovermanas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/grovermanas/subscriptions","organizations_url":"https://api.github.com/users/grovermanas/orgs","repos_url":"https://api.github.com/users/grovermanas/repos","events_url":"https://api.github.com/users/grovermanas/events{/privacy}","received_events_url":"https://api.github.com/users/grovermanas/received_events","type":"User","site_admin":false},"created_at":"2016-10-12T16:38:29Z","updated_at":"2016-10-12T16:38:29Z","body":"I have written a ZuulFilter to pass on the oauth2 token to auth service check_token endpoint for validation, and on successful validation generate a JWT at auth service which contains the permission details for the user/microservice and returns that back to ZuulFilter in the gateway. Now, I need to set this jwt in the Authentication header as a Bearer token . Below is the Filter code I have written.\r\n\r\nimport javax.servlet.http.HttpServletRequest;\r\n\r\nimport org.slf4j.Logger;\r\nimport org.slf4j.LoggerFactory;\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.http.ResponseEntity;\r\n\r\nimport com.netflix.zuul.ZuulFilter;\r\nimport com.netflix.zuul.context.RequestContext;\r\nimport com.netflix.zuul.http.HttpServletRequestWrapper;\r\n\r\npublic class AuthorizationFilter extends ZuulFilter {\r\n\tprivate static Logger log = LoggerFactory.getLogger(AuthorizationFilter.class);\r\n\t@Autowired\r\n\tprivate AuthenticationServiceClient authenticationServiceClient;\r\n\r\n\t@Override\r\n\tpublic String filterType() {\r\n\t\treturn \"pre\";\r\n\t}\r\n\r\n\t@Override\r\n\tpublic int filterOrder() {\r\n\t\treturn 0;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic boolean shouldFilter() {\r\n\t\treturn true;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic Object run() {\r\n\t\tRequestContext ctx = RequestContext.getCurrentContext();\r\n\t\tHttpServletRequest request = ctx.getRequest();\r\n\t\t\r\n\t\tString header = request.getHeader(\"Authorization\");\r\n\t\tif (header == null || header.isEmpty() || !header.startsWith(\"Bearer \")) {\r\n\t\t\tctx.setResponseStatusCode(401);\r\n\t\t\tctx.setSendZuulResponse(false);\r\n\t\t} else {\r\n\t\t\theader = header.replace(\"Bearer \", \"\");\r\n\t\t\tlog.info(\"Token is '\" + header + \"'\");\r\n\t\t\tResponseEntity<String> responseToken = authenticationServiceClient.validateToken(header);\r\n\t\t\tString jwtToken = responseToken.getBody();\r\n\t\t\t\r\n\t\t\t//request.setAttribute(\"jwt\", jwtToken);\r\n\t\t\tHttpServletRequestWrapper wrappedRequest = modifyRequest(request, jwtToken);\r\n\t\t\t/*wrappedRequest.setAttribute(\"jwt\", jwtToken);*/\r\n\t\t\tctx.setRequest(wrappedRequest);\r\n\t\t\t\r\n\t\t\tif (responseToken == null) {\r\n\t\t\t\tctx.setResponseStatusCode(500);\r\n\t\t\t\tctx.setResponseBody(\"AuthenticationService Not Available\");\r\n\t\t\t\tctx.setSendZuulResponse(false);\r\n\t\t\t} else {\r\n\t\t\t\tlog.info(responseToken.getStatusCode().name());\r\n\t\t\t\tlog.info(responseToken.getBody().toString());\r\n\t\t\t}\r\n\t\t}\r\n\t\tlog.info(String.format(\"%s request to %s\", request.getMethod(), request.getRequestURL().toString()));\r\n\t\treturn null;\r\n\t}\r\n\t\r\n\tprivate HttpServletRequestWrapper modifyRequest(HttpServletRequest request, String jwt) {\r\n\t\treturn new HttpServletRequestWrapper(request) {\r\n\r\n\t\t\t@Override\r\n\t\t\tpublic String getContentType() {\r\n\t\t\t\treturn \"application/json\";\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t@Override\r\n\t\t\tpublic String getHeader(String name){\r\n\t\t\t\t//if(name.equalsIgnoreCase(\"Authorization\")){\r\n\t\t\t\treturn \"Bearer \" + jwt;\r\n\t\t\t\t//}\r\n\t\t\t\t//return super.getHeader(name);\t\t\t\r\n\t\t\t}\r\n\t\t};\r\n\t}\r\n}\r\n\r\nNow , when the request is passed to the actual microservice I see the bearer token in the x-forwarded-prefix header in the following manner: \"Bearer token/content\" where content is the request path for the microservice but not able to set it in the Authentication header."}}
{"actor":{"display_login":"grovermanas"}}