【发布时间】:2011-08-08 16:20:21
【问题描述】:
有一堆链接访问我的 servlet 没有 https
由于 servlet 是一种通用形式,并且 http url 是使用随机 id 生成的,因此很难使用 modrewrite 或类似的东西。
因此我用这样的代码修改了我的 servlet:
//redirect to https
String sec = servletRequest.getParameter("Sec");
String qString = servletRequest.getQueryString();
if (StringUtils.isEmpty(sec)){
try {
HttpServletResponse rsp = request.getServletResponse(true);
String PORTAL_URL = l_aliasHelper.getPath(request);
rsp.sendRedirect("https://"+servletRequest.getServerName() +PORTAL_URL+"?" +qString+"&Sec=yes");
} catch (Exception e) {
e.printStackTrace();
}
}
现在可以正常使用了!
但是,如果我想回到 http 怎么办,因为我想避免在其他页面上出现关于不安全元素的唠叨警告。
那么在用户提交表单后如何再次重定向到http呢?
如果一切正常,用户会在他开始的同一个 URL 下显示一条成功消息的响应。
So the cycle goes like this:
http://<somedomain>/<anypath>?<anyid>
https://<somedomain>/<anypath>?<anyid>&Sec=yes
and now it should go back maybe with a step inbetween to
http://<somedomain>/<anypath>?<anyid> <- the success message should be
displayed here
消息显示前的最后一个方法是
sucessmessage.render(请求,响应)
request 和 response 都是 appserver 组件对所有请求/响应相关事务的特定视图。他们有这样的方法:
getServletResponse
public HttpServletResponse getServletResponse(boolean answering)
Gets the original servlet response. Note: This should be accessed仅在特殊情况下。如果参数设置为true all 将跳过运行时的进一步内容处理。这是 仅在请求是从基于 servlet 的情况下才可用 连接。
那么如何以安全提交表单的方式处理响应,但之后用户可以在网站的其余部分继续使用 http。
【问题讨论】: