Spring-Boot框架拦截器对请求进行拦截处理------Spring-Boot框架

发布于:2023-10-08 ⋅ 阅读:(144) ⋅ 点赞:(0)
package com.powernode.springbootvue.controller;

import com.powernode.springbootvue.entity.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class ParamsController
{
    @RequestMapping("/upload")
    public String up(String nickname, MultipartFile photo, HttpServletRequest request)
    {
        System.out.println(nickname);
        System.out.println(photo.getOriginalFilename());
        System.out.println(photo.getContentType());
        System.out.println(System.getProperty("user.dir"));
        String path = request.getServletContext().getRealPath("/upload/");
        System.out.println(path);
        try {
            saveFile(photo,path);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "上传成功";
    }
    public void saveFile(MultipartFile photo,String path) throws IOException {
        File dir = new File(path);
        if(!dir .exists())
        {
            dir.mkdir();
        }
        File file = new File(path + photo.getOriginalFilename());
        photo.transferTo(file);
    }
    @RequestMapping("/user/helloGet")
    public String helloGet(@RequestParam(value = "nickname",required = false) String name)
    {
        return name;
    }
    @RequestMapping("/user/helloPost")
    public String helloPost(@RequestParam(value = "nickname",required = false) String name,String password)
    {
        System.out.println(name + password);
        return name;
    }
    @RequestMapping(value = "/PostTest",method = RequestMethod.POST)
    public void PostTest(User user)
    {
        System.out.println(user.toString());
    }
    @RequestMapping(value = "/PostTest1",method = RequestMethod.POST)
    //如果前端回传的json类型的数据,需要添加上@RequestBody这个注解,否则无法接收
    public void PostTest1(@RequestBody User user)
    {
        System.out.println(user.toString());
    }
    @RequestMapping("/user/**")
    public String test()
    {
        return "通配符请求";
    }
}
package com.powernode.springbootvue.controller;

import com.powernode.springbootvue.entity.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class ParamsController
{
    @RequestMapping("/upload")
    public String up(String nickname, MultipartFile photo, HttpServletRequest request)
    {
        System.out.println(nickname);
        System.out.println(photo.getOriginalFilename());
        System.out.println(photo.getContentType());
        System.out.println(System.getProperty("user.dir"));
        String path = request.getServletContext().getRealPath("/upload/");
        System.out.println(path);
        try {
            saveFile(photo,path);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "上传成功";
    }
    public void saveFile(MultipartFile photo,String path) throws IOException {
        File dir = new File(path);
        if(!dir .exists())
        {
            dir.mkdir();
        }
        File file = new File(path + photo.getOriginalFilename());
        photo.transferTo(file);
    }
    @RequestMapping("/user/helloGet")
    public String helloGet(@RequestParam(value = "nickname",required = false) String name)
    {
        return name;
    }
    @RequestMapping("/user/helloPost")
    public String helloPost(@RequestParam(value = "nickname",required = false) String name,String password)
    {
        System.out.println(name + password);
        return name;
    }
    @RequestMapping(value = "/PostTest",method = RequestMethod.POST)
    public void PostTest(User user)
    {
        System.out.println(user.toString());
    }
    @RequestMapping(value = "/PostTest1",method = RequestMethod.POST)
    //如果前端回传的json类型的数据,需要添加上@RequestBody这个注解,否则无法接收
    public void PostTest1(@RequestBody User user)
    {
        System.out.println(user.toString());
    }
    @RequestMapping("/user/**")
    public String test()
    {
        return "通配符请求";
    }
}
package com.powernode.springbootvue.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

public class LoginInterceptor implements HandlerInterceptor
{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("LoginInterceptor");
        return false;
    }
}

 

package com.powernode.springbootvue.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

public class LoginInterceptor implements HandlerInterceptor
{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("LoginInterceptor");
        return false;
    }
}
package com.powernode.springbootvue.config;

import com.powernode.springbootvue.interceptor.LoginInterceptor;
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 WebConfig implements WebMvcConfigurer
{
    //增加拦截器方法
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/user/**");
    }
}

 

package com.powernode.springbootvue.config;

import com.powernode.springbootvue.interceptor.LoginInterceptor;
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 WebConfig implements WebMvcConfigurer
{
    //增加拦截器方法
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/user/**");
    }
}
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.web.resources.static-locations=/upload/
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.web.resources.static-locations=/upload/
本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到