zoukankan      html  css  js  c++  java
  • 过滤器实现登录拦截

    过滤器拦截类

    package com.ssm.filter;
    
    import org.omg.PortableServer.SERVANT_RETENTION_POLICY_ID;
    import javax.servlet.*;
    import javax.servlet.annotation.WebFilter;
    import javax.servlet.http.HttpServletRequest;
    import java.io.IOException;
    
    @WebFilter(urlPatterns = {"/*"})
    public class myFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            HttpServletRequest httpServletRequest=(HttpServletRequest) servletRequest;
            System.out.println(httpServletRequest.getRequestURI());
            if(httpServletRequest.getRequestURI().equals("/user/login")){
                filterChain.doFilter(servletRequest,servletResponse);
                System.out.println("合法请求");
            }
            Object user = httpServletRequest.getSession().getAttribute("user");
            if(user==null){
                System.out.println("非法请求");
                httpServletRequest.getRequestDispatcher("/login.jsp").forward(servletRequest,servletResponse);
            }else{
                filterChain.doFilter(servletRequest,servletResponse);
            }
        }
    
        @Override
        public void destroy() {
    
        }
    }
    View Code

    web.xml文件配置

    <!DOCTYPE web-app PUBLIC
            "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
            "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
      <filter>
        <filter-name>characterEncodingFilter</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>
        <!--强制使用UTF-8编码-->
        <init-param>
          <param-name>forceEncoding</param-name>
          <param-value>true</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/</url-pattern>
      </filter-mapping>
    
      <!--中央调度器-->
      <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:applicationContext.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>
    
    
    </web-app>
    View Code

    login.jsp页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登陆</title>
    </head>
    <body>
        <div>
            <form action="/user/login" method="post">
                用户名:<input type="text" name="usercode"/>
                密码:<input type="password" name="userpassword"/>
                <input type="submit" value="登陆"/>
            </form>
        </div>
    </body>
    </html>
    View Code

    welcome.jsp页面

    <%--
      Created by IntelliJ IDEA.
      User: FLC
      Date: 2019/11/14
      Time: 10:00
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <html>
    <head>
        <title>欢迎</title>
    </head>
    <body>
        欢迎光临:${user.username}
    </body>
    </html>
    View Code

      在没有登录的情况下访问首页,则直接跳转到登录页面,进行登录验证;

      

  • 相关阅读:
    那些年我们写过的T-SQL(下篇)(转)
    好的架构是进化来的,不是设计来的
    dhcpsrv:windows系统的优秀开源免费dhcp serve软件
    PXE(preboot execution environment):【网络】预启动执行环节:安装 debian 9系列:成功
    PXE(preboot execution environment):【网络】预启动执行环节:安装 ubuntu、rehat系列:成功
    问题:UltraISO:这个软件有问题,它制作的iso文件会造成无法正确识别。用PowerISO吧
    自windows8以后,所有版本(专业版、企业版、旗舰版)都支持从 vhd 启动
    SpringCloud Feign
    SpringCloud Netflix Ribbon(负载均衡)
    SpringCloud Netflix Eureka(服务注册/发现)
  • 原文地址:https://www.cnblogs.com/wnwn/p/11858746.html
Copyright © 2011-2022 走看看