AuthFilter.java
上传用户:sxychgz
上传日期:2019-04-21
资源大小:4772k
文件大小:1k
源码类别:

电子政务应用

开发平台:

Java

  1. package com.bjsxt.shopping.util.filter;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.Filter;
  5. import javax.servlet.FilterChain;
  6. import javax.servlet.FilterConfig;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.ServletRequest;
  9. import javax.servlet.ServletResponse;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import javax.servlet.http.HttpSession;
  13. public class AuthFilter implements Filter {
  14. public void destroy() {
  15. System.out.println("authfilter destroy");
  16. }
  17. public void doFilter(ServletRequest req, ServletResponse resp,
  18. FilterChain chain) throws IOException, ServletException {
  19. //System.out.println("authfilter dofilter");
  20. HttpServletRequest request = (HttpServletRequest)req;
  21. HttpServletResponse response = (HttpServletResponse)resp;
  22. HttpSession session = request.getSession(false);
  23. if(session == null || (String)session.getAttribute("admin") == null ||!((String)session.getAttribute("admin")).equals("admin")) {
  24. System.out.println(request.getContextPath());
  25. response.sendRedirect( request.getContextPath() + "/AdminLogin.jsp");
  26. return;
  27. }
  28. chain.doFilter(req, resp);
  29. }
  30. public void init(FilterConfig config) throws ServletException {
  31. System.out.println("authfilter init");
  32. }
  33. }