首页 > 其他分享 >spring boot——请求与参数校验——重要概念——拦截器

spring boot——请求与参数校验——重要概念——拦截器

时间:2023-01-18 22:55:26浏览次数:50  
标签:web 拦截器 http spring boot springframework org import public

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

============================================================================

 

 

 

 

 

 

 

 

 

 

拦截器示例:

控制器:

package org.example.controller;

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


@Controller
public class HelloController
{
    //  http://localhost:8080/hello
    @ResponseBody
    @RequestMapping("/hello")
    public String hello()
    {

        return "Hello World!____________china";
    }
    
    //    http://localhost:8080/123
    @ResponseBody
    @RequestMapping("/123")
    public String home123()
    {
        return "中国您好";
    }



    //   http://localhost:8080/1234
    @RequestMapping(value = "/1234",method = RequestMethod.GET)
    @ResponseBody
    public String home1234()
    {
        return "中国您好_123456";
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

定义一个拦截器:

package org.example.controller.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 UseInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if ("b".equals(request.getParameter("a"))) {

            System.out.println("请求不成功");

            return false;
        } else {

            System.out.println("该方法在控制器处理请求方法前执行,其返回值表示是否中断后续操作,返回 true 表示继续向下执行,返回 false 表示中断后续操作。");
            //放行
            return true;
        }
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        System.out.println("该方法在控制器处理请求方法调用之后、解析视图之前执行,可以通过此方法对请求域中的模型和视图做进一步修改。");


    }



}

 

 

 

 

 

 

 

 

 

注册拦截器:

package org.example.config;

import org.example.controller.Interceptor.UseInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {


        System.out.println("注册拦截器");

        registry.addInterceptor(new UseInterceptor()).addPathPatterns("/**") //拦截所有请求,包括静态资源文件
                .excludePathPatterns("/", "/login", "/index.html", "/user/login", "/css/**", "/images/**", "/js/**", "/fonts/**"); //放行登录页,登陆操作,静态资源
    }
}

 

 

 

 

 

 

 

请求:http://localhost:8080/hello?a=b

 

 

 

 

 

 

 

 

 

 

 

请求:http://localhost:8080/hello?a=c

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

PS:

 

 

 

标签:web,拦截器,http,spring,boot,springframework,org,import,public
From: https://www.cnblogs.com/xiaobaibailongma/p/17060812.html

相关文章

  • SpringCloud Bus消息总线
    1、介绍①概念人话:不用一个一个服务去post。一次post后,各个服务通过bus都能获取post的信息去更新配置。即将N次向client的post,改为了一次post(向client或center)在微服......
  • Spring Remoting
    Spring框架使远程支持服务的开发变得容易。通过提供自己的API,它节省了大量代码。SpringRemoting的优势程序员只需要专注于业务逻辑,而不必研究诸如启动和停止服务器之类......
  • Spring Cloud Gateway 网关限流
    可用性和可靠性对于所有web应用程序和API来说都是至关重要的。当系统流量突然增加时,会影响应用程序的服务质量,甚至可能导致所有用户的服务中断。一种解决方案是为基础......
  • 学习笔记——Spring声明式事务管理;Spring中支持事务管理;使用声明式事务管理;Spring声明
    2023-01-18一、Spring声明式事务管理1、事务四大特征(ACID)(1)原子性(2)一致性(3)隔离性(4)持久性2、事务三种行为(1)开启事务:connection.setAutoCommit(False)(2)提交事务:conne......
  • 浅谈Netty中ServerBootstrap服务端源码(含bind全流程)
    文章目录​​一、梳理Java中NIO代码​​​​二、Netty服务端代码​​​​1、newNioEventLoopGroup()​​​​2、group​​​​3、channel​​​​4、NioServerSocketChanne......
  • Spring MVC Tiles示例
    Spring提供了与apachetile框架的集成支持。因此,我们可以借助SpringTile支持简单地管理SpringMVC应用程序的布局。SpringMVC支持Tiles的优势SpringMVCTiles示例1、......
  • Spring MVC自定义验证
    SpringMVC框架允许我们执行自定义验证。在这种情况下,我们声明自己的注释。我们可以根据自己的业务逻辑执行验证。SpringMVC自定义验证示例在此示例中,我们同时使用预......
  • Spring MVC编号验证
    在SpringMVC验证中,我们可以在数字范围内验证用户的输入。以下注释用于实现数字验证:@Min注解-必须传递带有@Min批注的整数值。用户输入必须等于或大于此值。@Max注解......
  • Spring MVC验证
    SpringMVC验证用于限制用户提供的输入。为了验证用户的输入,Spring4或更高版本支持并使用Bean验证API。它可以同时验证服务器端和客户端应用程序。 Bean验......
  • Spring MVC正则表达式验证
    SpringMVC验证使我们可以按特定顺序(即正则表达式)验证用户输入。 @Pattern 批注用于实现正则表达式验证。在这里,我们可以为 regexp 属性提供所需的正则表达式,并将其......