拦截器文件
package com.puzzle.api.AuthorityInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
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;
/**
* @author 陈金泽
* @version V1.0
* @date 2019/7/2 13:46
*/
@Configuration
public class AuthorityInterceptor implements WebMvcConfigurer {
@Autowired
private ApiAuthorityInterceptor apiAuthorityInterceptor;
@Autowired
private AdminAuthorityInterceptor adminAuthorityInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 注册拦截器
InterceptorRegistration loginRegistryApi = registry.addInterceptor(apiAuthorityInterceptor);
InterceptorRegistration loginRegistryAdmin = registry.addInterceptor(adminAuthorityInterceptor);
// 拦截路径
loginRegistryApi.addPathPatterns("/admin/*");
loginRegistryAdmin.addPathPatterns("/api/*");
// 排除路径
loginRegistryApi.excludePathPatterns("/admin/login");
loginRegistryApi.excludePathPatterns("/admin/loginPort");
// 排除资源请求
loginRegistryApi.excludePathPatterns("/**/*.css");
loginRegistryApi.excludePathPatterns("/**/*.js");
loginRegistryApi.excludePathPatterns("/**/*.png");
loginRegistryApi.excludePathPatterns("/**/*.gif");
loginRegistryApi.excludePathPatterns("/**/*.jpg");
loginRegistryApi.excludePathPatterns("/**/*.jpeg");
loginRegistryApi.excludePathPatterns("/**/*.ttf");
loginRegistryApi.excludePathPatterns("/**/*.woff");
}
}
API拦截器
package com.puzzle.api.AuthorityInterceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class ApiAuthorityInterceptor implements HandlerInterceptor {
@Override//在一个请求进入Controller层方法执行前执行这个方法
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println(request.getRequestURI()+"aaaaa");
return true;//方法给予执行,就是允许controller的方法进行执行
//false 不允许,可以在这之前在reponse中编写返回的结果
}
//返回model前
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println(request.getRequestURI()+"bbb");
}
//返回model后
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//在一个请求处理完毕,即将销毁的时候,执行,可以做一些资源释放之类的工作
}
}
ADMIN拦截器
package com.puzzle.api.AuthorityInterceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class AdminAuthorityInterceptor implements HandlerInterceptor {
@Override//在一个请求进入Controller层方法执行前执行这个方法
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println(request.getRequestURI()+"cccc");
return true;//方法给予执行,就是允许controller的方法进行执行
//false 不允许,可以在这之前在reponse中编写返回的结果
}
//返回model前
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println(request.getRequestURI()+"aaaaa");
}
//返回model后
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//在一个请求处理完毕,即将销毁的时候,执行,可以做一些资源释放之类的工作
}
}
网友评论