1结构
2loginController中的代码:
package com.example.demo.controller.login;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class LoginController {
private Map<String,Object> params=new HashMap<>();
@RequestMapping(value="/api2/v1/account")
public Object account() {
System.out.println("controller中的account方法");
params.put("money", "1000");
return params;
}
}
3自定义拦截器LoginIntercepter
代码:
package com.example.demo.Interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class LoginIntercepter implements HandlerInterceptor {
/**
* 进入controller方法之前
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("LoginIntercepter-------->preHandle");
String token=request.getParameter("token");
/*
* if(token!=null) { response.getWriter().print("token不为空"); }else {
* response.getWriter().print("token 为空"); }
*/
return HandlerInterceptor.super.preHandle(request, response, handler);
}
/**
* 调用controller之后,但是在视图渲染之前
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("LoginIntercepter-------->postHandle");
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
/**
* 整个完成之后,通常用于资源清理
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("LoginIntercepter-------->afterCompletion");
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
4@Configuaration
旧版本 继承WebMvcConfigurerAdapter(springBoot2.x之前的老版本)
package com.example.demo.Interceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* 旧版本的拦截器(springboot2.x之前的拦截器),不建议使用,我的版本是2.3.3,所以这里WebMvcConfigurerAdapter是横线
* @author Administrator
*
*/
@Configuration
public class OldIntercepter extends WebMvcConfigurerAdapter{
/**
* 添加旧版拦截器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginIntercepter()).addPathPatterns("/api/*/**");
super.addInterceptors(registry);
}
}
新版本 springBoot2.x 新版本配置拦截器 implements WebMvcConfigurer
package com.example.demo.Interceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* jdk8之后,可以在接口中写方法,属于jdk的新特性
* springboot2.x之后通过接口的形式引用拦截器
* @author Administrator
*
*/
@Configuration
public class NewIntercepter implements WebMvcConfigurer{
@Override
public void addInterceptors(InterceptorRegistry registry) {
//拦截全部接口为
registry.addInterceptor(new LoginIntercepter()).addPathPatterns("/api2/*/**");
WebMvcConfigurer.super.addInterceptors(registry);
}
}
5run application