07-08
12

一个关于后台保护的过滤和记录程序

package order.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.*;

/**
* <p>Title: 商城</p>
*
* <p>Description: Struts+Hibernate</p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author Blurxx
* @version 1.1
*/


public class SignonFilter extends HttpServlet implements Filter {
    Log log = LogFactory.getLog(SignonFilter.class);

    private FilterConfig filterConfig;
    //Handle the passed-in FilterConfig
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    //Process the request/response pair
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain filterChain) {
        String LOGIN_POINTER = "../filterlogin.do";
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpSession session = req.getSession();
        try {
            String isLogin = (String) session.getAttribute("person_info");
            if (isLogin == null) {
                isLogin = "";
            }
            if (isLogin.equals("root")) {
                log.info("通过验证");
                filterChain.doFilter(request, response);
            } else {
                log.info("有人强行试图进入后台");
                resp.sendRedirect(LOGIN_POINTER);
                //req.getRequestDispatcher(LOGIN_POINTER).forward(req,resp);
            }
        } catch (ServletException sx) {
            filterConfig.getServletContext().log(sx.getMessage());
        } catch (IOException iox) {
            filterConfig.getServletContext().log(iox.getMessage());
        }
    }

    //Clean up resources
    public void destroy() {
    }
}


package order.action;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import java.io.*;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.*;
import java.util.*;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.*;

public class FilterToLoginAction extends Action {
    Log log = LogFactory.getLog(FilterToLoginAction.class);
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws
            IOException {
        //写入信息
        this.writeLog(request);
        
        ActionMessages errors = new  ActionMessages();
        errors.add("user.must.login", new ActionMessage("user.must.login"));
        this.saveErrors(request,errors);

        return new ActionForward("/login.jsp");

    }

    private void logout(String message) throws IOException {
        //建立文档记录;
        String dir = servlet.getServletContext().getRealPath(""); //确定根目录
        String filterlog = dir + "/manager/log/filterlog.log";
        File f = new File(filterlog);
        if (!f.exists()) {
            f.createNewFile();
        }
        PrintWriter out = null;
        out=new PrintWriter(new FileOutputStream(f, true));
        out.println(new Date().toLocaleString()+" 信息: "+message);
        out.close();
    }
    
    private void logoutln(String message) throws IOException {
        //建立文档记录;
        String dir = servlet.getServletContext().getRealPath(""); //确定根目录
        String filterlog = dir + "/manager/log/filterlog.log";
        File f = new File(filterlog);
        if (!f.exists()) {
            f.createNewFile();
        }
        PrintWriter out = null;
        out=new PrintWriter(new FileOutputStream(f, true));
        out.println();
        out.close();
    }

    
    public void writeLog(HttpServletRequest request) throws IOException {
        if(request.getAttribute("person_id")!=null)
        {
            logout("为本站注册用户");
            logout("id:" + request.getAttribute("person_id").toString());
            logout("name" + request.getAttribute("person_name").toString());
        }
        logout("ip地址:"+request.getRemoteHost().toString());
        logout("浏览器:"+request.getHeader("User-agent"));
        logout("操作系统"+System.getProperty("os.name"));
        logoutln("");

    }

    public static void main(String args[]) throws FileNotFoundException {
        
    }
}


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 621
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭