原生的Spring AOP实现统一拦截有两大难点,1.定义拦截的规则难写;2.在切面类中拿到HttpSession比较难。为此Spring官方提供了拦截器帮我们解决了上面痛点。
//验证登入信息的拦截器
public class LoginInterceptor implements HandlerInterceptor {
//此方法返回一个boolean,如果为true表示验证成功,继续执行后续流程
//如果是false表示验证失败,后面流程不能执行
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession(false);
if(session != null && session.getAttribute("userinfo") != null) {
return true;
}
//身份验证失败,重定向到登入页面
response.sendRedirect("/login.html");
return false;
}
}
@Configuration
public class AppConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**") //**表示拦截所有请求
.excludePathPatterns("/user/login") //不拦截的url地址
.excludePathPatterns("/user/reg")
.excludePathPatterns("/login.html");//登入页面不拦截,否则会报重定向多次的错误
}
}
Spring中的拦截器是通过动态代理和环绕通知的思想实现的
之所以要配置统一的前缀是因为在一个服务器中有多个程序,却只有一个域名,就需要在每个程序添加url前缀才知道访问哪个程序
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.addPathPrefix("/zhangsan", c ->true);//设置true启动前缀
}
server.servlet.context-path=/lisi
@ControllerAdvice
@ResponseBody
public class MyExhandler {
}
/*
拦截所有空指针异常
*/
@ExceptionHandler(NullPointerException.class)
public HashMap<String, Object> nullException(NullPointerException e) {
HashMap<String, Object> result = new HashMap<>();
result.put("code", -1);
result.put("msg", "空指针异常:" + e.getMessage());
result.put("data", null);
return result;
}
@ExceptionHandler(Exception.class)
public HashMap<String, Object> exception(Exception e) {
HashMap<String, Object> result = new HashMap<>();
result.put("code", -1);
result.put("msg", "异常:" + e.getMessage());
result.put("data", null);
return result;
}
@ControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice {
@Autowired
private ObjectMapper objectMapper;
/*
返回true则执行beforeBodyWrite方法,反之不执行
*/
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
HashMap<String, Object> result = new HashMap<>();
result.put("code", 200);
result.put("msg", "");
result.put("data", body);
//body是String时转换时会报错,需要特殊处理
if(body instanceof String) {
try {
return objectMapper.writeValueAsString(result);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
return result;
}
}