apache/tomcat服务器下配置https
apache下配置https:
首先在网站根目录下,找到.htaccess文件(如果没有则新建),apache可以直接调用此文件,对需要由http 强制转换为https和https强制转换为http的页面进行处理。
举例说明:
# http-->https
RewriteCond %{SERVER_PORT} !^443$
RewriteCond%{REQUEST_URI}%{QUERY_STRING}^(/user.php?act=login|/user.php?act=register|/user.php? act=profile)$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
当访问www.xxxx.cn/user.php?act=login的时候,.htaccess文件先判断一下端口号是否不是443(RewriteCond % {SERVER_PORT} !^443$),再判断URL的路径中是否包含了user.php?act=login(RewriteCond% {REQUEST_URI}%{QUERY_STRING}^(/user.php?act=login|/user.php?act=register|/user.php? act=profile)$),如果两个条件都符合,则执行下一步,跳转到 https://www.xxxx.cn/user.phpact=login (RewriteRule ^.*$ https://% {SERVER_NAME}%{REQUEST_URI} [L,R])。(https-->http也是同理) tomcat下配置https:
tomcat下有两种配置方式,一种只需要在web.xml中配置,简单粗暴(如果只是需要由http-->https,并不需要 强制https-->http);另外一种则是通过urlrewritefilter来实现,相对较复杂(需要导入urlrewritefilter.jar的包,在 web.xml中需要配置,同时还需要新建单独的xml文件来进行页面访问的控制,但是,可以满足http-->https和htps--
>http)。
第一种:在web.xml最底部加入代码:
<span style="font-size:18px;"> <security-constraint> <web-resource-collection > <web-resource-name >SSL</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint></span>第二种:1、下载urlrewritefilter.jar(点击下载),导入项目中。
2、在web.xml的顶部加入代码(<display-name></display-name>之后)。
<filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class> org.tuckey.web.filters.urlrewrite.UrlRewriteFilter </filter-class> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>3、在项目目录下创建urlrewrite.xml文件,内容如下。
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" "http://tuckey.org/res/dtds/urlrewrite3.0.dtd"> <urlrewrite> <rule> <condition type="scheme" operator="notequal">https</condition> <condition name="host" operator="equal">localhost</condition> <condition type="port" name="port" operator="equal">8888</condition> <condition type="query-string" operator="equal">^model=.*&ObjectName=.*$</condition> <from>^(/servlet/MobileServlet)$</from> <to type="permanent-redirect">https://%{host}:8443%{request-uri}?%{query-string}</to> </rule> </urlrewrite>