使用 https 安全协议方法

1 使用java拦截器方法实现 协议跳转(目前可以不用,使用下面js方法即可,这个是提供的另外一种方法)
//该类实现了 Filter 接口,在 web.xml需要配置拦截器


    public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain)
        throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)request;
        String contextPath = httpServletRequest.getRequestURI();
        //获取需要转换为 https的请求路径,这个配置在 xml文件里
        List<String> resultList = getList()
        //isSecure()用来检测是否是 https 的请求
        if (httpServletRequest.isSecure() && resultList.contains(contextPath)) {
            //此处用来做页面重定向处理,如果路径需要转为 https,则 重定向
            response.sendRedirect(........);
        }
        else {
            chain.doFilter(request, response);
        }
        return;
    }




    <filter>
        <filter-name>Redirect</filter-name>
        <filter-class>com.abc.def.MyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Redirect</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
2 js方法
if (window.location.protocol == "http:") {
    if (window.location.replace) {
        window.location.replace("https" + window.location.href.substr(4));
    } else {
        window.location.href = "https" + window.location.href.substr(4)
    }
}
3 tomcat 主要配置:
    <Connector connectionTimeout="20000" port="80" protocol="HTTP/1.1" redirectPort="443"/>
    <!--配置了 443端口,keystore 路径, 和密码-->
    <Connector port="443" SSLEnabled="true" clientAuth="false" keystoreFile="d:/a/b/tomcat.keystore" keystorePass="222555" maxThreads="150" protocol="HTTP/1.1" scheme="https" secure="true" sslProtocol="TLS"/>
    <Connector port="8009" protocol="AJP/1.3" redirectPort="443"/>    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静山晚风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值