利用session验证用户是否正常登录

原文地址:http://ejb-wawa.iteye.com/blog/395873

WEB的信息安全隐患之一: 

未授权用户通过直接在IE中输入URL直接登录系统 

解决办法: 

通过配置filter过滤无效用户的连接请求. 

WEB的信息安全隐患之二: 

合法用户"注销"后,在未关闭浏览器的情况下,点击浏览器"后退"按钮,可从与本地页面缓存中读取数据,绕过了服务端filter过滤. 

解决办法: 

在必要的页面(包含敏感信息) 设定页面缓存限制. 

也可以把上面两步组合在一个,通过同一个filter实现.具体如下: 

1.配置filter(web.xml) 

...... 

<filter> 
   <filter-name>Authentication</filter-name> <!-- Authentication过滤器别名 --> 
   <filter-class>com.mycompany.myweb.management.util.AuthenticationFilter</filter-class> <!-- 过滤器Authentication指向的具体类 --> 
   <init-param> 
    <param-name>onError</param-name> <!-- 过滤器初始化参数配置 --> 
    <param-value>/Logon.do</param-value> <!-- 这里指定无效用户跳转方向 --> 
   </init-param> 
</filter> 
<filter-mapping> 
<filter-name>Authentication</filter-name> 
<url-pattern>/management/*</url-pattern> <!-- management/*是要过滤的文件的位置,表示过滤management文件夹下的所内容。 --> 
</filter-mapping> 
<filter-mapping> 
<filter-name>Authentication</filter-name> 
<url-pattern>/Main.do</url-pattern> <!-- Main.do/*是要过滤的请求,表示过滤此请求指定的页面的所内容。 --> 
</filter-mapping> 

...... 

AuthenticationFilter过滤器实现类: 

package com.mycompany.myweb.management.util; 

import java.io.IOException; 
import javax.servlet.ServletException; 
import javax.servlet.http.*; 
import javax.servlet.Filter; 
import javax.servlet.FilterChain; 
import javax.servlet.FilterConfig; 
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 
import org.apache.struts.Globals; 
import org.apache.struts.action.*; 

public class AuthenticationFilter implements Filter {//一定要使用Filter接口 
private FilterConfig filterConfig; 

private String onErrorUrl; 

public void init(FilterConfig config) throws ServletException { 
filterConfig = config; 
nErrorUrl = filterConfig.getInitParameter("onError"); 
if (onErrorUrl == null || "".equals(onErrorUrl)) { 
   nErrorUrl = "onError"; 



public void doFilter(ServletRequest request, ServletResponse response, 
   FilterChain next) throws IOException, ServletException { 
HttpServletRequest httpRequest = (HttpServletRequest) request; 
HttpServletResponse httpResponse = (HttpServletResponse) response; 
HttpSession httpSession = httpRequest.getSession(); 
/** 
* @author justin ray 
* @see 页面缓存设定 
* <br>确保浏览器不缓存页面数据 
*/ 

httpResponse.setHeader("Cache-Control","no-cache"); 
httpResponse.setHeader("Cache-Control","no-store"); 
httpResponse.setDateHeader("Expires", 0); 
httpResponse.setHeader("Pragma","no-cache"); 

/** 
* @author justin ray 
* @see 过滤未登录用户无效请求 
* <br>未登录用户请求跳转到/Logon.do 
*/ 
if (httpSession.getAttribute("ePAccountInfo") == null) { 
   ActionErrors errors = new ActionErrors(); 
   errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("没有登录")); 
   httpRequest.setAttribute(Globals.ERROR_KEY, errors); 
   httpRequest.getRequestDispatcher(onErrorUrl).forward(httpRequest, 
     httpResponse); 
} else 
   next.doFilter(request, response); 


public void destroy() { 


}

转载于:https://www.cnblogs.com/m-xy/archive/2013/03/11/2954107.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值