[英]Spring security change redirect URL to use HTTPS instead of HTTP
我正在使用受Spring Security SSO登錄保護的Spring微服務(使用Cloudfoundry UAA)。
部署在雲上的微服務可通過HTTPS
URL訪問。 由於HTTPS URL是ELB(負載均衡器/ Web服務器),因此對ELB的微服務的實際請求來自HTTP
。 因此Spring將用戶重定向到登錄頁面時會在302 Location
標題中生成HTTP URL而不是HTTPS URL。
以下是流程
Browser
->(https://mymicroservice.com) Unauthenticated request (Load balancer)
->(http://internal_lan_ip:someport) Microservice
-> 302 Location http://mymicroservice.com/login
-> Browser http://mymicroservice.com/login (failed)
In short it goes from HTTPS -> HTTP -> 302 HTTP (failed as ELB doesn't serve on HTTP)
以下是我的嘗試
的x轉發-原
由於負載均衡器也沒有正確地將x-forwarded-proto
填充到HTTPS
,而是它給了我HTTP
,我不能使用Spring的支持。
需要渠道HTTPS
它也不起作用,因為它導致Spring無限重定向,因為Spring從未收到來自ELB
的HTTPS請求,盡管正確生成了HTTPS重定向URL。
攔截器/過濾器
使用ServletFilter
檢查響應頭Location
,如果存在,則用https://
替換http://
https://
。
坦率地說,最后一個選項是我的最終選項,因為我不控制ELB配置。
現在的問題是,我無法在Spring重定向到/login
URL后攔截響應,而后者又應該重定向到SSO URL。
我嘗試過攔截器的各種組合(postHandle,afterCompletion),使用Spring安全性將其注入過濾器鏈中的不同位置,最后將過濾器順序設置為最低。 這些都不會在重定向后攔截未經身份驗證的請求。
@Component
@Order(Ordered.LOWEST_PRECEDENCE)
class RedirectUrlProtocolUpdaterFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String locationHeader = response.getHeader("Location");
System.out.println("############ inside interceptor");
for(String name: response.getHeaderNames()) {
System.out.println(name + " : " + response.getHeader(name));
}
if(locationHeader != null && locationHeader.startsWith("http://")) {
System.out.println("###################### setting location header");
locationHeader = locationHeader.replaceAll("http://", "https://");
response.setHeader("Location", locationHeader);
}
filterChain.doFilter(request, response);
}
}
如何在過濾器/攔截器中正確攔截Spring Security的/login
重定向並更新Location頭以包含正確的協議?
任何提示都表示贊賞。
如果要更新Location頭信息,可以嘗試使用HttpResponseInterceptor。
這是使用Google HttpResponseInterceptor的一個示例: https : //developers.google.com/api-client-library/java/google-http-java-client/reference/1.20.0/com/google/api/client/ HTTP / HttpResponseInterceptor
其他選項來自Apache: https : //hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/class-use/HttpResponseInterceptor.html
SpringFramework提供了HandlerInterceptor。 這將攔截所有http請求,但可用於不斷檢查身份驗證和授權。 您必須提供3種方法的實現(如果您不使用它們,只需實現一個空方法)。 然后,您可以將代碼放在preHandle方法中。
public class AuthenticationInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.