springboot 拦截器

1.编写一个拦截器

import java.io.PrintWriter;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.zl.fund.pojo.User;

public class LoginInterceptor extends HandlerInterceptorAdapter{

	//在控制器执行前调用
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		User user = (User) request.getSession().getAttribute("loginUser");
		String operUrl = request.getServletPath().toString();
		if(user == null&&!operUrl.equals("/user/toLogin.do")&&!operUrl.equals("/user/login.do")&&!operUrl.equals("/scripts/login.js")) {
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html; charset=utf-8");
			PrintWriter out = response.getWriter();
		    out.println("");    
		    out.println("");    
		    out.println("");
			return false;
		}
		return true;
	}
	//在后端控制器执行后调用
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}
	//整个请求执行完成后调用
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}
}
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.zl.fund.pojo.User;
/**
 * 去设置交易密码页面的拦截
 * @author THINK
 *
 */
public class SetTransCodeInterceptor extends HandlerInterceptorAdapter {
	//在控制器执行前调用
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		User user = (User) request.getSession().getAttribute("loginUser");
		//String operUrl = request.getServletPath().toString();
		if(user.getIdCard() == null) {
			response.sendRedirect(request.getContextPath() + "/security/toIdentityVerify.do");
			return false;
		}
		return true;
	}
	//在后端控制器执行后调用
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}
	//整个请求执行完成后调用
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}
}

2.编写一个管理器 实现WebMvcConfigurer

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登录拦截的管理器
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());     //拦截的对象会进入这个类中进行判断
        registration.addPathPatterns("/**");                    //所有路径都被拦截
        registration.excludePathPatterns("/","/scripts/**","/webjars/**","/error",
        		"/images/**","/asserts/**","/bootstrap-3.3.7-dist/**","/question",
        		"/toDetails/**","/fundMarket","/index","/user/**","/pagingByAjax");       //添加不拦截路径
        registration.order(1);
        //拦截访问设置交易密码的请求
        InterceptorRegistration registrationTransCode = registry.addInterceptor(new SetTransCodeInterceptor());
        registrationTransCode.addPathPatterns("/security/toSetTransCode.do");
        registrationTransCode.order(2);
    }
}

WebMvcConfigurationSupport和WebMvcConfigurer的区别

你可能感兴趣的:(框架笔记,springboot)