Require HTTPS with Spring Security behind a reverse proxy

后端 未结 3 1896
你的背包
你的背包 2020-12-29 09:18

I have a Spring MVC application secured with Spring Security. The majority of the application uses simple HTTP to save resources, but a small part processes more confidentia

3条回答
  •  遥遥无期
    2020-12-29 09:41

    Kind of a followup to NeilMcGuigan's answer that showed that the solution was servlet container side.

    Tomcat is even better. There is a valve dedicated to masking the side effects of a reverse proxy. Extract from Tomcat documentation for Remote IP Valve:

    Another feature of this valve is to replace the apparent scheme (http/https), server port and request.secure with the scheme presented by a proxy or a load balancer via a request header (e.g. "X-Forwarded-Proto").

    Example of the valve configuration :

    
    

    That way with no other configuration of the application itself, the call to Request.isSecure() will return true if the request contains a header field of X-Forwarded-Proto=https.

    I had thought of two other possibilities, but definitively prefere that one :

    • use a filter active before Spring Security ChannelProcessingFilter to wrap the request with a HttpServletRequestWrapper overriding isSecure() to process a X-Forwarded-Proto header - need writing and testing the filter and the wrapper
    • use a Spring BeanPostProcessor to look for a ChannelProcessingFilter and manually inject a ChannelDecisionManager able to consider the X-Forwarded-Proto header - really too low level

提交回复
热议问题