자바 웹 응용 URL 필터

11333 단어 필터url자바web
전환 하 다http://www.yihaomen.com/article/java/117.htm노트
import java.io.IOException;
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 SecurityFilter implements Filter {

    public void destroy() {
        // TODO Auto-generated method stub

    }
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) arg0;
        HttpServletResponse response = (HttpServletResponse) arg1;
        HttpSession session = request.getSession(true);
        Object obj = (Object)session.getAttribute("CURRENT_USER");
        String url = request.getRequestURI();
        System.out.println(">>>>RequestURI:"+url);
        System.out.println(">>>RequestContext:"+request.getContextPath());
        if(obj==null){
            //                              
            if (url != null && !url.equals("")) {
                response.sendRedirect(request.getContextPath() + "/login.jsp");
                return;
            }
        }
        arg2.doFilter(arg0, arg1);
        return;
    }

    public void init(FilterConfig arg0) throws ServletException {
    }
}

웹.xml 설정
    <filter>  
        <filter-name>SecurityFilterfilter-name>  
        <filter-class>com.gjp.o2o.web.filter.SecurityFilterfilter-class>  
    filter>  
    <filter-mapping>  
        <filter-name>SecurityFilterfilter-name>  
        <url-pattern>/admin/*url-pattern>  
    filter-mapping>
    <filter-mapping>  
        <filter-name>SecurityFilterfilter-name>  
        <url-pattern>/facilitator/*url-pattern>  
    filter-mapping> 

주의:잘못된 몇 가지 쓰기
<filter-mapping>  
    <filter-name>authorityfilter-name>  
    <url-pattern>/pages/cmm/*;/pages/genbill/*url-pattern>  
filter-mapping> 

그리고 이렇게 쓰 는 건 잘못된 거 예요.
<url-pattern>/direcotry/*.jspurl-pattern>

spring 차단기 의 servlet 필터 방식
일단 차단기.
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;
//import org.apache.struts.action.ActionMapping;

public class LoginInterceptor implements MethodInterceptor {
    private static final Logger log = Logger.getLogger(LoginInterceptor.class);

    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("    !");
        Object[] args = invocation.getArguments();  
        HttpServletRequest request = null;
        HttpServletResponse response = null;
        for (int i = 0 ; i < args.length ; i++ )    {
            if (args[i] instanceof HttpServletRequest) request = (HttpServletRequest)args[i];   
            if (args[i] instanceof HttpServletResponse) response = (HttpServletResponse)args[i];

        }
        if (request != null ) {
            String url=request.getRequestURI();
            String operation=request.getParameter("operation");
            HttpSession session = request.getSession(true);     
            Object obj = (Object)session.getAttribute("CURRENT_USER");
            System.out.println(">>>>RequestURI:"+url);
            System.out.println(">>>RequestContext:"+request.getContextPath());
            System.out.println(">>>Operation:"+operation);
            if (obj == null&&(!"login".equals(operation))) {
                response.sendRedirect(request.getContextPath() + "/login.jsp");
                return false;
            }
            else {
                /*do something*/
                return invocation.proceed();
            }
        }
        else {
            return invocation.proceed();
        }
    }

}

그리고 spring 프로필 에 설정:

<bean id="loginInterceptor" class="com.gjp.o2o.web.interceptor.LoginInterceptor" >bean>

<bean id="autoPorxyFactoryBean" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="interceptorNames">
            <list>
                <value>loginInterceptorvalue>
            list>
        property>
        <property name="beanNames" >
            <list>
                <value>*Servletvalue>
            list>
        property>
    bean>

좋은 웹페이지 즐겨찾기