我使用Spring Cloud配置来帮助构建Rest服务,然后选择GitHub来维护我的配置文件。当我在GitHub
上更改某个配置时,需要WebHooks将调用API
,其中提供的Spring Cloud Config用于监控配置更改并通知配置服务。
为了验证请求是否是GitHub
请求。我将添加一个过滤器来检查签名,这不起作用:
该请求甚至没有通过过滤器并获得http 200 ok响应。我还测试了扩展WebMvcConfigurerAdapter的方法。但仍然是一样的。我想在我的过滤器可以处理请求之前是spring spring config吗?
public class WebhookSignatureFilter implements Filter
{
@Autowired private WebhooksAuthService webhooksAuthService;
@Override public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain filterChain) throws IOException,
ServletException
{
HttpServletRequest httpServletRequest=(HttpServletRequest)servletRequest;
String githubSignature=httpServletRequest.getHeader("HTTP_X_HUB_SIGNATURE");
String payload=getPayLoad(httpServletRequest);
try
{
if(!webhooksAuthService.isValidWebhookSignature(githubSignature, payload))
{
throw new GeneralSecurityException();
}
}
catch(GeneralSecurityException e)
{
throw new ServletException("verify signature failed!");
}
filterChain.doFilter(servletRequest, servletResponse);
}
private String getPayLoad(HttpServletRequest httpServletRequest) throws UnsupportedEncodingException
{
httpServletRequest.setCharacterEncoding("UTF-8");
BufferedReader bufferedReader=null;
StringBuffer payload=new StringBuffer();
String line;
try
{
InputStream inputStream=httpServletRequest.getInputStream();
bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
while((line=bufferedReader.readLine()) != null)
{
payload.append(line);
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
if(bufferedReader!=null)
{
try
{
bufferedReader.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
return payload.toString();
}
@Override public void destroy()
{
}
@Override public void init(FilterConfig filterConfig) throws ServletException
{
}
}
这是我的配置文件:
@Configuration public class FilterConfig
{
@Bean public FilterRegistrationBean webhooksFilter()
{
FilterRegistrationBean registration=new FilterRegistrationBean();
WebhookSignatureFilter webhookSignatureFilter=new WebhookSignatureFilter();
registration.setFilter(webhookSignatureFilter);
registration.addUrlPatterns("/monitor");
registration.setOrder(0);
return registration;
}
}
我做错了什么?
答案 0 :(得分:0)
问题解决了。我发现当我检查application.yml时。我最初配置rabbitmq,github配置混合在同一个yml文件中。所以它可能是由混淆的yml文件引起的,这可能导致某些配置未加载。