我正在使用Juul过滤器。要从Juul过滤器调用控制器,我正在使用Feign客户端。
我的Juul过滤器代码是:
public class PreFilter extends ZuulFilter{
@Autowired
private SecurityServiceClient securityService;
@Autowired
private ValidationBean validationBean;
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
ctx.setSendZuulResponse(false);
return null;
}
@Override
public boolean shouldFilter() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String userName = request.getParameter("userName");
String password = request.getParameter("pwd");
String grantType = request.getParameter("grant_type");
String refreshToken = request.getParameter("refresh_token");
String accessToken = request.getParameter("access_token");
String clientIdAndClientSecret = request.getParameter("authorization");
String reqestedUrl = request.getRequestURI();
String result = securityService.authenticate(request);
JsonParser parser = new JsonParser();
JsonObject object = parser.parse(result).getAsJsonObject();
if(object.get("access_token") != null){
return false;
}else{
ctx.setResponseBody(result);
}
return true;
}
@Override
public int filterOrder() {
return 1;
}
@Override
public String filterType() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletResponse response = ctx.getResponse();
return "pre";
}
}
Feign客户端代码是:
@FeignClient("security-service")
public interface SecurityServiceClient {
@RequestMapping(method = RequestMethod.GET, value = "/api/security/authenticate", consumes = "application/json")
String authenticate(HttpServletRequest request);
}
和控制器是:
@RestController
@RequestMapping("/security")
public class AuthenticationController {
@Autowired
private Auth2Client auth2Client;
@RequestMapping(value="/authenticate", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public String authoriseRequest(HttpServletRequest request) {
String oauth2Response = "";
String user = request.getParameter("userName");
String password = request.getParameter("pwd");
String grantType = request.getParameter("grant_type");
String refreshToken = request.getParameter("refresh_token");
String accessToken = request.getParameter("access_token");
String clientIdAndClientSecret = request.getHeader("Authorization");
String reqestedUri = request.getParameter("url");
String clientid = "clientid";
String clientSecret = "secret";
// Needs to put in the request header with key "Authorization" and not here
clientIdAndClientSecret = "Basic " + new String(Base64Utils.encode(("" + clientid + ":" + clientSecret + "").getBytes()));
try{
if(null != grantType && grantType.equalsIgnoreCase("password")){
oauth2Response = auth2Client.authenticateUserPassword(clientIdAndClientSecret, user, password, grantType, refreshToken);
}else if(null != grantType && grantType.equalsIgnoreCase("refresh_token")){
oauth2Response = auth2Client.getNewAccessTokenUsingRefreshToken(clientIdAndClientSecret, refreshToken, grantType);
}else{
clientIdAndClientSecret = "Bearer " + accessToken;
oauth2Response = auth2Client.authenticateUserUsingAccessToken(clientIdAndClientSecret, reqestedUri);
}
}catch(FeignException fe){
// Get the error message and send back to client
oauth2Response = fe.toString().substring(fe.toString().lastIndexOf("content:") + 8);
}
return oauth2Response;
}
}
上面的代码不起作用,我得到例外:
com.netflix.zuul.exception.ZuulException: Filter threw Exception
at com.netflix.zuul.FilterProcessor.processZuulFilter(FilterProcessor.java:231) ~[zuul-core-1.0.28.jar:?]
at com.netflix.zuul.FilterProcessor.runFilters(FilterProcessor.java:161) ~[zuul-core-1.0.28.jar:?]
at com.netflix.zuul.FilterProcessor.preRoute(FilterProcessor.java:136) ~[zuul-core-1.0.28.jar:?]
at com.netflix.zuul.ZuulRunner.preRoute(ZuulRunner.java:93) ~[zuul-core-1.0.28.jar:?]
at com.netflix.zuul.http.ZuulServlet.preRoute(ZuulServlet.java:120) [zuul-core-1.0.28.jar:?]
at com.netflix.zuul.http.ZuulServlet.service(ZuulServlet.java:70) [zuul-core-1.0.28.jar:?]
at org.springframework.web.servlet.mvc.ServletWrappingController.handleRequestInternal(ServletWrappingController.java:158) [spring-webmvc-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.cloud.netflix.zuul.web.ZuulController.handleRequestInternal(ZuulController.java:43) [spring-cloud-netflix-core-1.0.7.RELEASE.jar:1.0.7.RELEASE]
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:146) [spring-webmvc-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:50) [spring-webmvc-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) [spring-webmvc-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) [spring-webmvc-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965) [spring-webmvc-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:856) [spring-webmvc-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:841) [spring-webmvc-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:300) [spring-boot-actuator-1.2.8.RELEASE.jar:1.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:103) [spring-boot-actuator-1.2.8.RELEASE.jar:1.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) [spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) [spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:77) [spring-boot-actuator-1.2.8.RELEASE.jar:1.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) [tomcat-embed-core-8.0.30.jar:8.0.30]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_92]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_92]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.30.jar:8.0.30]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_92]
Caused by: feign.codec.EncodeException: Could not write content: (was java.lang.NullPointerException) (through reference chain: org.springframework.cloud.netflix.zuul.filters.pre.Servlet30RequestWrapper["contentData"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: org.springframework.cloud.netflix.zuul.filters.pre.Servlet30RequestWrapper["contentData"])
at feign.ReflectiveFeign$BuildEncodedTemplateFromArgs.resolve(ReflectiveFeign.java:256) ~[feign-core-8.1.1.jar:8.1.1]
at feign.ReflectiveFeign$BuildTemplateByResolvingArgs.create(ReflectiveFeign.java:199) ~[feign-core-8.1.1.jar:8.1.1]
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:67) ~[feign-core-8.1.1.jar:8.1.1]
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:94) ~[feign-core-8.1.1.jar:8.1.1]
at com.sun.proxy.$Proxy93.authenticate(Unknown Source) ~[?:?]
at com.openrisk.openmargin.techservices.gateway.filters.PreFilter.shouldFilter(PreFilter.java:81) ~[classes/:?]
at com.netflix.zuul.ZuulFilter.runFilter(ZuulFilter.java:109) ~[zuul-core-1.0.28.jar:?]
at com.netflix.zuul.FilterProcessor.processZuulFilter(FilterProcessor.java:197) ~[zuul-core-1.0.28.jar:?]
... 56 more
Caused by: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: (was java.lang.NullPointerException) (through reference chain: org.springframework.cloud.netflix.zuul.filters.pre.Servlet30RequestWrapper["contentData"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: org.springframework.cloud.netflix.zuul.filters.pre.Servlet30RequestWrapper["contentData"])
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:238) ~[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:208) ~[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.cloud.netflix.feign.support.SpringEncoder.encode(SpringEncoder.java:87) ~[spring-cloud-netflix-core-1.0.7.RELEASE.jar:1.0.7.RELEASE]
at feign.ReflectiveFeign$BuildEncodedTemplateFromArgs.resolve(ReflectiveFeign.java:252) ~[feign-core-8.1.1.jar:8.1.1]
at feign.ReflectiveFeign$BuildTemplateByResolvingArgs.create(ReflectiveFeign.java:199) ~[feign-core-8.1.1.jar:8.1.1]
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:67) ~[feign-core-8.1.1.jar:8.1.1]
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:94) ~[feign-core-8.1.1.jar:8.1.1]
at com.sun.proxy.$Proxy93.authenticate(Unknown Source) ~[?:?]
at com.openrisk.openmargin.techservices.gateway.filters.PreFilter.shouldFilter(PreFilter.java:81) ~[classes/:?]
at com.netflix.zuul.ZuulFilter.runFilter(ZuulFilter.java:109) ~[zuul-core-1.0.28.jar:?]
at com.netflix.zuul.FilterProcessor.processZuulFilter(FilterProcessor.java:197) ~[zuul-core-1.0.28.jar:?]
... 56 more
Caused by: com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: org.springframework.cloud.netflix.zuul.filters.pre.Servlet30RequestWrapper["contentData"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210) ~[jackson-databind-2.4.6.jar:2.4.6]
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:177) ~[jackson-databind-2.4.6.jar:2.4.6]
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:187) ~[jackson-databind-2.4.6.jar:2.4.6]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:647) ~[jackson-databind-2.4.6.jar:2.4.6]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152) ~[jackson-databind-2.4.6.jar:2.4.6]
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:128) ~[jackson-databind-2.4.6.jar:2.4.6]
at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1902) ~[jackson-databind-2.4.6.jar:2.4.6]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:231) ~[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:208) ~[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.cloud.netflix.feign.support.SpringEncoder.encode(SpringEncoder.java:87) ~[spring-cloud-netflix-core-1.0.7.RELEASE.jar:1.0.7.RELEASE]
at feign.ReflectiveFeign$BuildEncodedTemplateFromArgs.resolve(ReflectiveFeign.java:252) ~[feign-core-8.1.1.jar:8.1.1]
at feign.ReflectiveFeign$BuildTemplateByResolvingArgs.create(ReflectiveFeign.java:199) ~[feign-core-8.1.1.jar:8.1.1]
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:67) ~[feign-core-8.1.1.jar:8.1.1]
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:94) ~[feign-core-8.1.1.jar:8.1.1]
at com.sun.proxy.$Proxy93.authenticate(Unknown Source) ~[?:?]
at com.openrisk.openmargin.techservices.gateway.filters.PreFilter.shouldFilter(PreFilter.java:81) ~[classes/:?]
at com.netflix.zuul.ZuulFilter.runFilter(ZuulFilter.java:109) ~[zuul-core-1.0.28.jar:?]
at com.netflix.zuul.FilterProcessor.processZuulFilter(FilterProcessor.java:197) ~[zuul-core-1.0.28.jar:?]
... 56 more
Caused by: java.lang.NullPointerException
at com.netflix.zuul.http.HttpServletRequestWrapper.getContentData(HttpServletRequestWrapper.java:106) ~[zuul-core-1.0.28.jar:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_92]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_92]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_92]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_92]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:466) ~[jackson-databind-2.4.6.jar:2.4.6]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:639) ~[jackson-databind-2.4.6.jar:2.4.6]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152) ~[jackson-databind-2.4.6.jar:2.4.6]
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:128) ~[jackson-databind-2.4.6.jar:2.4.6]
at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1902) ~[jackson-databind-2.4.6.jar:2.4.6]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:231) ~[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:208) ~[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.cloud.netflix.feign.support.SpringEncoder.encode(SpringEncoder.java:87) ~[spring-cloud-netflix-core-1.0.7.RELEASE.jar:1.0.7.RELEASE]
at feign.ReflectiveFeign$BuildEncodedTemplateFromArgs.resolve(ReflectiveFeign.java:252) ~[feign-core-8.1.1.jar:8.1.1]
at feign.ReflectiveFeign$BuildTemplateByResolvingArgs.create(ReflectiveFeign.java:199) ~[feign-core-8.1.1.jar:8.1.1]
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:67) ~[feign-core-8.1.1.jar:8.1.1]
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:94) ~[feign-core-8.1.1.jar:8.1.1]
at com.sun.proxy.$Proxy93.authenticate(Unknown Source) ~[?:?]
at com.openrisk.openmargin.techservices.gateway.filters.PreFilter.shouldFilter(PreFilter.java:81) ~[classes/:?]
at com.netflix.zuul.ZuulFilter.runFilter(ZuulFilter.java:109) ~[zuul-core-1.0.28.jar:?]
at com.netflix.zuul.FilterProcessor.processZuulFilter(FilterProcessor.java:197) ~[zuul-core-1.0.28.jar:?]
... 56 more