当前位置: 代码迷 >> JavaScript >> ibatis对JSP过滤,在用户未登录的情况上
  详细解决方案

ibatis对JSP过滤,在用户未登录的情况上

热度:98   发布时间:2012-09-05 15:19:35.0
ibatis对JSP过滤,在用户未登录的情况下

1、JAVA代码

package com.yihaodian.pis.util;

import java.io.IOException;   
import java.io.PrintWriter;   
  
import javax.servlet.Filter;   
import javax.servlet.FilterChain;   
import javax.servlet.FilterConfig;   
import javax.servlet.ServletException;   
import javax.servlet.ServletRequest;   
import javax.servlet.ServletResponse;   
import javax.servlet.http.HttpServletRequest;   
import javax.servlet.http.HttpServletResponse;   
import javax.servlet.http.HttpSession;   
  
public class LoginFilter implements Filter {   
  
 private static final String LOGON_URI = "LOGON_URI";   
  
 private static final String HOME_URI = "HOME_URI";   
  
 private String logon_page;   
  
 private String home_page;   
  
 public void destroy() {   
 }   
  
 public void doFilter(ServletRequest request, ServletResponse response,   
   FilterChain chain) throws IOException, ServletException {   
  HttpServletRequest req = (HttpServletRequest) request;   
  HttpServletResponse resp = (HttpServletResponse) response;   
  resp.setContentType("text/html;");   
  resp.setCharacterEncoding("utf-8");   
  HttpSession session = req.getSession();   
  PrintWriter out = resp.getWriter();   
  // 得到用户请求的URI   
  String request_uri = req.getRequestURI();   
  // 得到web应用程序的上下文路径   
  String ctxPath = req.getContextPath();   
  // 去除上下文路径,得到剩余部分的路径   
  String uri = request_uri.substring(ctxPath.length());   
  // 判断用户访问的是否是登录页面   
  if (uri.equals(logon_page) || uri.equals(home_page)) {   
   chain.doFilter(request, response);   
   return;   
  } else {   
   // 如果访问的不是登录页面,则判断用户是否已经登录   
   if (null != session.getAttribute("admin")   
     && "" != session.getAttribute("admin"))    
  
{   
    chain.doFilter(request, response);   
    return;   
   } else {   
    out.println("<script language=\"javaScript\">"  
      + "parent.location.href='" +    
  
ctxPath + logon_page + "'"  
      + "</script>");   
    return;   
   }   
  }   
 }   
  
 public void init(FilterConfig config) throws ServletException {   
  // TODO Auto-generated method stub   
  // 从部署描述符中获取登录页面和首页的URI   
  logon_page = config.getInitParameter(LOGON_URI);   
  home_page = config.getInitParameter(HOME_URI);   
  // System.out.println(logon_page);   
  if (null == logon_page || null == home_page) {   
   throw new ServletException("没有找到登录页面或主页");   
  }   
 }   
  
}   

?

2、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
	<display-name>pcs</display-name>
	<welcome-file-list>
		<welcome-file>webpage/login/login.jsp</welcome-file>
	</welcome-file-list>


	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:config/spring/spring-*.xml</param-value>
	</context-param>

	<!-- log4j在spring中的配置 -->
	<!--然后加上这个Spring的Log4j侦听类,注意在JBOSS里面,不需要这个类,注释掉,不然会冲突, -->
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	<!--webAppRootKey 属性为webApp.root 代表webApp的根目录,这样就能在properties文件中定义 -->
	<!-- 这里的webApp是http://localhost:8080/webApp/ -->
	<context-param>
		<param-name>webAppRootKey</param-name>
		<param-value>pcs.WebContent</param-value>
	</context-param>
	<!--log4jConfigLocation 属性代表log4j.properties文件的地址 -->
	<!-- log4j.appender.file.File=${webApp.root}/WEB-INF/logs/subject.log -->
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/classes/config/log4j/log4j.properties</param-value>
	</context-param>

	<!-- spring2全局上下文监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 应删除这段,不然汇报重复定义ContextLoader错误 -->
	<!-- <servlet> <servlet-name>context</servlet-name> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> 
		<load-on-startup>1</load-on-startup> </servlet> -->
	<!-- spring 的httpinvoker -->
	
	<!-- 
	<servlet>
        <servlet-name>editweboffice</servlet-name>
        <servlet-class>com.jspsmart.web.action.EditWebOfficeServlet</servlet-class>
    </servlet>		
    <servlet-mapping>
        <servlet-name>editweboffice</servlet-name>
        <url-pattern>/editweboffice.goto</url-pattern>
    </servlet-mapping>	     	
	 -->
	<!-- 
	<servlet>
		<description>HTTP Exporters</description>
		<servlet-name>remote</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:config/spring/remote-servlet.xml</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>remote</servlet-name>
		<url-pattern>/remote/*</url-pattern>
	</servlet-mapping>
	 -->
	<session-config>
       <session-timeout> 1 </session-timeout >
    </session-config> 
	<!-- spring 全局字符编码器-->
	
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping> 


	<!-- Struts2配置 -->
	
	<filter>
	
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>struts-default.xml,struts-plugin.xml,/config/struts/struts.xml </param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>*.jsp</url-pattern>
	</filter-mapping>
	
	<!-- session 拦截器 -->
	    <filter>  
       <filter-name>loginFilter</filter-name>  
       <filter-class>com.yihaodian.pis.util.LoginFilter</filter-class>  
       <init-param>  
             <param-name>LOGON_URI</param-name>  
             <param-value>/</param-value>  
       </init-param>  
       <init-param>  
             <param-name>HOME_URI</param-name>  
             <param-value>/index.jsp</param-value>  
       </init-param>  
     </filter>  
     <filter-mapping>  
      <filter-name>loginFilter</filter-name>  
      <url-pattern>*.jsp</url-pattern>  
     </filter-mapping>  

	
	<!-- session 拦截器 -->
	<!-- 
     <filter>  
        <filter-name>SessionInvalidate</filter-name>  
        <filter-class>com.yihaodian.pcs.session.SessionCheckFilter</filter-class>  
        <init-param>  
            <param-name>checkSessionKey</param-name>  
            <param-value>APP_SESSION_TOKEN</param-value>  
        </init-param>  
        <init-param>  
            <param-name>redirectURL</param-name>  
            <param-value>/sessionInvalidate.jsp</param-value>  
        </init-param>  
        <init-param>  
            <param-name>notCheckURLList</param-name>  
            <param-value>/webpage/login/login.jsp,/logon.do,/logout.jsp,/sessionInvalidate.jsp</param-value>  
        </init-param>  
     </filter>  
     <filter-mapping>  
        <filter-name>SessionInvalidate</filter-name>  
        <url-pattern>*.do</url-pattern>       
     </filter-mapping>  
    <filter-mapping>  
        <filter-name>SessionInvalidate</filter-name>  
        <url-pattern>*.jsp</url-pattern>      
     </filter-mapping>  	
	 -->

</web-app>
?
  相关解决方案