07-08
12
一个关于后台保护的过滤和记录程序
作者:Java伴侣 日期:2007-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() {
}
}
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 {
}
}
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 {
}
}
评论: 0 | 引用: 0 | 查看次数: 619
发表评论