首页 > 其他分享 >【WEEK5】 【DAY1】Interceptor【English Version】

【WEEK5】 【DAY1】Interceptor【English Version】

时间:2024-03-26 15:30:15浏览次数:30  
标签:return Create public English import Interceptor WEEK5 login 9.2

2024.3.25 Monday

Contents

9. Interceptor

9.1. Overview

  1. SpringMVC’s handler interceptors are similar to the filters in Servlet development, used for preprocessing and postprocessing of handlers. Developers can define their own interceptors to implement specific functionalities.
  2. Difference between filters and interceptors: Interceptors are a specific application of AOP (Aspect-Oriented Programming) ideas.
  3. Filters
    • Part of the Servlet specification, usable by any Java web project
    • After configuring with url-pattern as /*, it can intercept all resources that are accessed
  4. Interceptors
    • Interceptors are specific to the SpringMVC framework, only projects using SpringMVC can utilize interceptors
    • Interceptors only intercept visits to controller methods, accessing jsp/html/css/image/js will not be intercepted

9.2. Custom Interceptor

9.2.1. How to Implement an Interceptor

Customize a class and then extend the HandlerInterceptor interface

9.2.2. Create a module

9.2.2.1. Create a springmvc-07-interceptor module

Insert image description here

9.2.2.2. Add web support

The location and content of web.xml are the same as in springmvc-06-ajax
Insert image description here

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--Bind configuration file-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <!--Load on startup-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>    <!--Solve garbled text-->
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>   <!--Filter all requests-->
    </filter-mapping>
</web-app>
9.2.2.3. Add applicationContext.xml, create a controller folder

The location is the same as web.xml in springmvc-06-ajax, the content is only path-modified
Insert image description here

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Automatically scan the specified package, manage all annotated classes in IOC container -->
    <context:component-scan base-package="P27.controller"/>
    <!--Static resource filter-->
    <mvc:default-servlet-handler />
    <mvc:annotation-driven/>

    <!-- View resolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <!-- Prefix -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- Suffix -->
        <property name="suffix" value=".jsp" />
    </bean>

</beans>
9.2.2.4. Add jar packages

Insert image description here

9.2.2.5. Configure Tomcat

Insert image description here

9.2.3. Create TestController.java

Insert image description here

package P27.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @GetMapping("/t1")
    public String test(){
        System.out.println("TestController/test() is running");
        return "done";
    }
}

Verify spring configuration is successful
http://localhost:8080/springmvc_07_interceptor_war_exploded/t1
Insert image description here
Server column output
Insert image description here

9.2.4. Create MyInterceptor.java and MyFilter.java

Insert image description here

9.2.4.1. MyFilter must implement three methods

Insert image description here

9.2.4.2. MyInterceptor does not require it, here to rewrite for experience

Use Ctrl+O (shortcut) to insert three methods
Insert image description here

package P27.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor implements HandlerInterceptor {

    // Before the request handler method is executed
    // If return true then execute the next interceptor
    // If return false then do not execute the next interceptor
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("------------preHandle before processing------------");
        return true;
    }

    // After the request handler method is executed
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("------------postHandle after processing------------");
    }

    // After dispatcherServlet processing is complete, used for cleanup
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("------------afterCompletion cleanup------------");
    }
}

9.2.5. Modify applicationContext.xml (add interceptor configuration)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Automatically scan the specified package, all annotated classes under it are managed by the IOC container -->
    <context:component-scan base-package="P27.controller"/>
    <!--Static resource filtering-->
    <mvc:default-servlet-handler />
    <mvc:annotation-driven/>

    <!-- View resolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <!-- Prefix -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- Suffix -->
        <property name="suffix" value=".jsp" />
    </bean>

    <!--  Interceptor configuration  -->
    <mvc:interceptors>
        <mvc:interceptor>
            <!--"/**" includes all requests under this one, all will be intercepted-->
            <!--The following two lines are examples: "/admin/*" intercepts things like /admin/add etc., /admin/add/user won't be intercepted-->
            <!--"/admin/**" intercepts all under /admin/-->
            <mvc:mapping path="/**"/>
            <!--Define the class to be filtered-->
            <bean class="P27.interceptor.MyInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

</beans>

9.2.6. Modify index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Test1$</title>
  </head>
  <body>
  <a href="${pageContext.request.contextPath}/t1">Interceptor Test</a>
  </body>
</html>

9.2.7. Run

Access the base URL of the project to see the initial page:
http://localhost:8080/springmvc_07_interceptor_war_exploded/
Insert image description here

When accessing the /t1 endpoint:
http://localhost:8080/springmvc_07_interceptor_war_exploded/t1

Insert image description here
Server output:
Insert image description here

9.2.7.1. Modify MyInterceptor

Change the return value of the preHandle function to false to intercept the request, then:
Insert image description here
Server output:
Insert image description here

public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("------------preHandle before processing------------");
//        return true;
        return false;
    }

9.3. Verifying User Login (Authentication)

9.3.1. Implementation Idea

  1. Have a login page and write a controller to access it.
  2. The login page has a form submission action. It needs to be processed in the controller. Check if the username and password are correct. If correct, write user information into the session. Return login success.
  3. Intercept user requests, determine if the user is logged in. If the user is already logged in, proceed; if not, redirect to the login page.

9.3.2. Create the login page login.jsp

Insert image description here

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Login Page</title>
</head>
<body>
<%--Pages or resources under WEB-INF can only be accessed through controllers or servlets--%>
<h1>Login Page</h1>
<form action="${pageContext.request.contextPath}/user/login" method="post">
  Username: <input type="text" name="username">
  Password: <input type="password" name="pwd">
  <input type="submit" value="SUBMIT">
</form>
</body>
</html>

9.3.3. Create LoginController.java to handle requests

Insert image description here

package P27.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class LoginController {

    // Redirect to login page
    @RequestMapping("/jumplogin")
    public String jumpLogin() throws Exception {
        return "login";
    }

    // Redirect to success page
    @RequestMapping("/jumpSuccess")
    public String jumpSuccess() throws Exception {
        return "success";
    }

    // Login submission
    @RequestMapping("/login")
    public String login(HttpSession session, String username, String pwd) throws Exception {
        // Record user identity information in session
        System.out.println("Receiving from frontend ===" + username);
        session.setAttribute("user", username);
        return "success";
    }

    // Logout
    @RequestMapping("logout")
    public String logout(HttpSession session) throws Exception {
        // Invalidate the session
        session.invalidate();
        return "login";
    }
}

9.3.4. Create LoginInterceptor.java

Insert image description here

package P27.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("uri: " + request.getRequestURI());
        // Determine the login situation: If the Session is not null, return true
        // Allow passing
        // 1. When on the login page
        if (request.getRequestURI().contains("login")){
            return true;
        }
        HttpSession session = request.getSession();
        // 2. When already logged in
        if (session.getAttribute("user")!=null){
            return true;
        }
        // Do not allow passing
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
        return false;
    }
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}

9.3.5. Create success.jsp

Insert image description here

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>Login Successful Page</h1>
<hr>
<h1><a href="${pageContext.request.contextPath}/index.jsp">TO HOME PAGE</a></h1>
<h1><a href="${pageContext.request.contextPath}/user/logout">EXIT</a></h1>

</body>
</html>

9.3.6. Modify index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>Verify Information</title>
  </head>
  <body>
  <h1>HOME PAGE</h1>
  <hr>

  <h1><a href="${pageContext.request.contextPath}/user/jumplogin">Login Page</a> </h1>
  <h1><a href="${pageContext.request.contextPath}/user/jumpSuccess">User Page</a> </h1>
  </body>
</html>

9.3.7. Register the Interceptor in the Spring MVC Configuration File

(Very similar to the modification in 9.2.5. applicationContext.xml, only the path is different)
Add the following code to the corresponding position in the diagram

<!--AJAX verification if the user is logged in-->
<mvc:interceptor>
    <mvc:mapping path="/**"/>
    <bean id="loginInterceptor" class="P27.interceptor.LoginInterceptor"/>
</mvc:interceptor>

Insert image description here

9.3.8. Run

http://localhost:8080/springmvc_07_interceptor_war_exploded/
Insert image description here
Clicking on any option will redirect to the following page, prompting the user to log in
http://localhost:8080/springmvc_07_interceptor_war_exploded/user/jumplogin
Insert image description here
Enter user information and click SUBMIT
Insert image description here
Insert image description here
Click TO HOME PAGE to return to the main page, where selecting the User Page does not require re-entering user information
Insert image description here
http://localhost:8080/springmvc_07_interceptor_war_exploded/user/jumpSuccess
Insert image description here
Click EXIT to log out, and you will be automatically redirected to the input information page
Insert image description here
This concludes the functionality demonstration. Operations after logging out are completely the same as when not logged in.

标签:return,Create,public,English,import,Interceptor,WEEK5,login,9.2
From: https://blog.csdn.net/2401_83329143/article/details/137028268

相关文章

  • 【WEEK5】学习目标及总结【SpringMVC+MySQL】【中文版】
    学习目标:彻底完成SpringMVC的学习两周完成MySQL的学习——第一周学习内容:参考视频教程【狂神说Java】SpringMVC最新教程IDEA版通俗易懂拦截器文件的上传和下载学习时间及产出:第五周MON~Fri2024.3.25【WEEK5】【DAY1】拦截器【中文版】【WEEK5】【DAY1】Inter......
  • nestJs中 Guards ,Interceptors ,Pipes ,Controller ,Filters的执行顺序
    执行顺序:Guards(守卫):Guards是最先执行的中间件,用于确定是否允许请求继续处理。Guards在请求被路由到控制器之前执行,通常用于身份验证、角色检查或权限验证。如果Guards返回一个布尔值 false 或者抛出一个异常,请求处理流程将终止,不会执行后续的Pipes、Interceptors或控......
  • 【WEEK4】 【DAY4】AJAX - Part One【English Version】
    2024.3.21ThursdayContents8.AJAX8.1.Introduction8.2.Simulatingajax8.2.1.Createanewmodule:springmvc-06-ajax8.2.2.Addwebsupport,importpomdependencies8.2.2.1.Modifyweb.xml8.2.2.2.Createajspfolder8.2.3.CreateapplicationContext.xml......
  • SpringMVC中的拦截器Interceptor实现
    之前的文章介绍过两个拦截器(分别参考MyBatis功能点之二(2):从责任链设计模式的角度理解插件实现技术和SpringAOP之源码分析)。本文介绍的拦截器实现与它们有何异同呢?在SpringMVC拦截器(Interceptor)使用中已知实现了HandlerInterceptor接口,MVC会自动拦截。如何实现的呢?改造......
  • HandlerInterceptor - 自定义拦截器
    自定义一个类实现HandlerInterceptor接口,加上@Component注解。根据需要重写方法publicinterfaceHandlerInterceptor{defaultbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{returntrue;......
  • C# 12 拦截器 Interceptors
    拦截器Interceptors是一种可以在编译时以声明方式替换原有应用的方法。这种替换是通过让Interceptors声明它拦截的调用的源位置来实现的。您可以使用拦截器作为源生成器的一部分进行修改,而不是向现有源编译添加代码。 演示使用.NET8创建一个控制台应用程序。并在Property......
  • NEW CONCEPT ENGLISH 41 - 50
    NEWCONCEPTENGLISH41-50Lesson41 Penny'sbagKeywordsandexpressionscheese n. 乳酪,干酪bread n. 面包soap n. 肥皂chocolate n. 巧克力sugar n. 糖coffee n. 咖啡tea n. 茶tobacco n. 烟草,烟丝LanguagepointsIsthatbagheavy,Penny......
  • 【WEEK2】 【DAY5】Data Processing and Redirection - Data Processing【English Ver
    2024.3.8FridayFollowingthepreviousarticle【WEEK2】【DAY4】DataProcessingandRedirection-MethodsofResultRedirection【EnglishVersion】Contents5.2.DataProcessing5.2.1.SubmittingProcessedData5.2.1.1.Thesubmittedfieldnamematches......
  • NEW CONCEPT ENGLISH 11-20
    NEWCONCEPTENGLISH11-20Lesson11 Isthisyourshirt?Keywordsandexpressionswhose pron. 谁的perhaps adv. 大概blue adj. 蓝色的white adj. 白色的catch v. 抓住LanguagepointsWhoseshirtisthat? 那是谁的衬衫?还可以这样说,Whoseisthatshi......
  • NEW CONCEPT ENGLISH 1 (1-10)
    NEWCONCEPTENGLISH 1-10Lesson1ExcusemeKeywordsandexpressionsexcuseme 劳驾,请问,对不起pardon n. 原谅,请再说一遍handbag n. 女士手提包thankyou 感谢你(们)verymuch 非常地LanguagepointsExcuseme 在别人身边挤过,和陌生人搭话,打断别......