您可以定义一个过滤器来验证连接是否安全,通过ServletRequest.isSecure
. 如果不安全,并且上下文中的应用程序需要安全连接,则重定向到您的“https”端点。否则,继续。
伪代码:
public class SecureConnectionFilter implements Filter {
private boolean requireSecure;
@Override
public void destroy() {
return;
}
@Override
public void doFilter(final ServletRequest request,
final ServletResponse response, final FilterChain filterChain)
throws IOException, ServletException {
if(requireSecure && ! (request.isSecure())) {
// Redirect to secure endpoint
} else {
filterChain.doFilter(request, response);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// Determine wether the application in context is required
// to be secure or not. If this information is not available
// at startup time then defer this logic to `doFilter`
}
}