Javaweb filter determines whether the user login filter-

Reprinted: https://blog.csdn.net/caodongfang126/article/details/53220080

Write very detailed:

1.首先在web.xml文件添加filter
    <filter>  
        <filter-name>wxloginfilter</filter-name>  
        <filter-class>com.src.mian.filter.WXLoginFilter</filter-class>  
        <async-supported>true</async-supported>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>wxloginfilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>

2.Add class class inherits the Filter 

public class WXLoginFilter the implements the Filter {
    // identifier: The current user is not logged in
    NO_LOGIN_NO = String "NO_LOGIN_NO"; 
    // exclude url 
    // String [] after the url path here need to log in before they have access to the 
    String [] excludeUrls = new String [ ] { "gift / getgift", "gift / userGiftList" , "Gift / giftInfo", "Game / the addComment"}; // 
    
    
    public WXLoginFilter () { 
        // constructor the TODO Auto-Generated Stub 
    } 

    / ** 
     * @see the destroy the Filter # () 
     * / public void the destroy () {
         / / the TODO Auto-Generated Method Stub     } / * * filter 
     * Analyzing url array String [] whether the log excludeUrls access url 
     * @see the doFilter the filter # (the ServletRequest, the ServletResponse, the FilterChain) * / public void
     


    
     
      doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        String uri = request.getRequestURI();
        
        //System.out.println("filter url:"+uri);
        //System.out.println("sessionid:"+session.getId());
        boolean pass = false;
        if(excludeUrls != null){
            for(String url : excludeUrls){
                if (uri.indexOf(url)>-1){
                    pass = true;
                    break;
                }
            }
        }        
        if (!pass) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            if(session.getAttribute("userKey_session") != null){
               // System.out.println("userKey_session:"+session.getAttribute("userKey_session"));
                filterChain.doFilter(request, response);
            }else{
                String requestType = request.getHeader("X-Requested-With"); 
                //判断是否是ajax请求
                if(requestType!=null && "XMLHttpRequest".equals(requestType)){
                    response.getWriter().write(this.NO_LOGIN_NO);
                }else{
                    response.sendRedirect(request.getContextPath()+"/user/login");
                }
                return;
            }
        }
    }

    /**初始化
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

}

 

Guess you like

Origin www.cnblogs.com/gxlaqj/p/11415685.html