HTTP自动跳转至HTTPS

本文介绍了两种实现全站HTTPS的方法:一是通过修改web.xml来强制所有请求使用HTTPS;二是利用Acegi安全框架,在用户登录时自动跳转至HTTPS。同时讨论了这两种方法的优缺点,并给出了具体的配置代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一:在应用项目中修改web.xml,增加授权区。
web.xml:
<!-- 所有链接自动跳转到https -->
<security-constraint>  
<web-resource-collection>
<web-resource-name>sslapp</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>


以上这种方式,会导致在IE中无法下载xls\swf等文件,但在firefox中没有问题


方法二:是在用户登录时,用acegi配置处理,强制使用https
项目中的welcome file:index.jsp跳转至一个不允许匿名访问的地址,index.jsp如下列:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<script>
function redirect() {
self.location='menu/homepage.jsp';
}
redirect();
</script>
</head>
</html>


而在验证配置中,允许匿名(未登录)访问的只有login.jsp
  <bean id="filterInvocationInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="accessDecisionManager">
<bean class="org.acegisecurity.vote.UnanimousBased">
<property name="allowIfAllAbstainDecisions" value="true"/>
<property name="decisionVoters">
<list>
<ref local="authService"/>
<bean class="org.acegisecurity.vote.AuthenticatedVoter"/>
</list>
</property>
</bean>
</property>
<property name="objectDefinitionSource">
<value>
<!-- CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON-->
PATTERN_TYPE_APACHE_ANT
/login.jsp=IS_AUTHENTICATED_ANONYMOUSLY
/**=IS_AUTHENTICATED_REMEMBERED
</value>
</property>
</bean>


所以用户在未登录情况下所进行的连接均会经过exceptionTranslationFilter
 <bean id="exceptionTranslationFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter">
<property name="authenticationEntryPoint">
<bean class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
<property name="loginFormUrl" value="/login.jsp"/>
<property name="forceHttps" value="true"/>
</bean>
</property>
<property name="accessDeniedHandler">
<bean class="org.acegisecurity.ui.AccessDeniedHandlerImpl">
<property name="errorPage" value="/403.jsp"/>
</bean>
</property>
</bean>

因为[color=red]forceHttps=true[/color],所以会直接连接到https的登录界面,登录后将会一直使用的是http连接

此种情况只有在用户登录后,又手动输入http连接的情况下无法跳转至https

此种方式在用户登录后的页面中如果存在非https的链接,IE可能会提示下列信息:

“是否只查看安全传送的网页内容?网页包含的内容将不使用安全的https连接传送,可能危及到整个网页的安全”

遇到上述IE的提示时,对IE进行以下设置即不会再出现提示:

在IE中选择“工具”-->"Internet选项"-->“安全”-->“自定义安全级别”-->在“其他”分类里找到“显示混合内容”选“启用”即可。(可参见附件)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值