• zbb20170926 SpringMVC javaweb 登录地址过滤器 没有登录跳转到登录页面


    AuthFilter 
    package com.zbb.filter;
    
    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.annotation.WebFilter;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * Servlet Filter implementation class AuthFilter
     */
    @WebFilter("/AuthFilter")
    public class AuthFilter implements Filter {
    
        /**
         * Default constructor.
         */
        public AuthFilter() {
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see Filter#destroy()
         */
        public void destroy() {
            // TODO Auto-generated method stub
        }
    
        /**
         * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
         */
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
                throws IOException, ServletException {
            // TODO Auto-generated method stub
            // place your code here
            HttpServletRequest request = (HttpServletRequest) servletRequest;
            HttpServletResponse response = (HttpServletResponse) servletResponse;
            // 获取根目录所对应的绝对路径 /jumper/index.jsp
            String currentURL = request.getRequestURI();
    
            // 截取到当前文件名用于比较 /index.jsp
            String targetURL = currentURL.substring(currentURL.indexOf("/", 1), currentURL.length());
            // System.out.println(targetURL);
            // 如果session不为空就返回该session,如果为空就返回null
            HttpSession session = request.getSession(false);
            boolean canLogin = true;
            // 登录地址不过滤
            if (currentURL.contains("jumper/jsp/admin/login/login.jsp")) {
                canLogin = false;
            }
            if (currentURL.contains("jumper/admin/login/validateLogin")) {
                canLogin = false;
            }
            // 静态文件不过滤
            if (currentURL.contains("jumper/static")) {
                canLogin = false;
            }
            // ui页面不过滤
            if (currentURL.contains("jumper/jsp/ui/") || currentURL.contains("jumper/index.jsp")) {
                canLogin = false;
            }
    
            if (canLogin) {
                // 判断当前页面是否是重顶次昂后的登陆页面页面,如果是就不做session的判断,防止死循环
                if (session == null || session.getAttribute("user") == null) {
                    // 如果session为空表示用户没有登陆就重定向到login.jsp页面
                    // System.out.println("request.getContextPath()=" +
                    // request.getContextPath());
                    response.sendRedirect(request.getContextPath() + "/jsp/admin/login/login.jsp");
                    return;
                }
            }
            // 继续向下执行
            chain.doFilter(request, response);
        }
    
        /**
         * @see Filter#init(FilterConfig)
         */
        public void init(FilterConfig fConfig) throws ServletException {
            // TODO Auto-generated method stub
        }
    
    }

    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"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:/springmvc-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <filter>
            <filter-name>AuthFilter</filter-name>
            <filter-class>com.zbb.filter.AuthFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>AuthFilter</filter-name>
            <url-pattern>*</url-pattern>
        </filter-mapping>
    </web-app>
  • 相关阅读:
    MySQL 和 Oracle 在 MyBatis 使用中的区别
    nodeppt:网页版 PPT
    在 sql 语句出现 warning 之后,立刻执行 `show warnings;` 就可以看到 warning 提示信息
    MySQL 列,可选择的数据类型(通过sql命令查看:`help create table;`)
    create table 推荐规则
    MySQL 中的变量:系统变量(包括:会话变量、全局变量)、用户变量(包括:局部变量、用户变量)
    MySQL 有用的查询语句
    MySQL 遇到错误集锦
    VARCHAR(N)类型,utf8编码,则N最大值为多少,n表示什么?
    MySQL 的严格模式
  • 原文地址:https://www.cnblogs.com/super-admin/p/7594979.html
Copyright © 2020-2023  润新知