Hello大家好,本章我们添加拦截器功能 。有问题可以联系我mr_beany@163.com。另求各路大神指点,感谢
在web开发的过程中,为了实现登录权限验证,安全验证等,我们往往需要添加一个拦截器在用户的的请求到达controller层的时候实现登录验证,那么SpringBoot如何添加拦截器呢?
打开core→configurer→WebConfigurer
添加如下
/** * TODO 修改为自己的需求 */ private static final String IZATION = "CHUCHEN"; /** * 添加拦截器 请求头拦截 */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor( //注意,HandlerInterceptorAdapter 这里可以修改为自己创建的拦截器 new HandlerInterceptorAdapter() { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String ization = request.getHeader("ization"); if(IZATION.equals(ization)){ return true; }else{ RetResult<Object> result = new RetResult<>(); result.setCode(RetCode.UNAUTHORIZED).setMsg("签名认证失败"); responseResult(response, result); return false; } } } //这里添加的是拦截的路径 /**为全部拦截 ).addPathPatterns("/userInfo/selectAlla"); }private void responseResult(HttpServletResponse response, RetResult<Object> result) { response.setCharacterEncoding("UTF-8"); response.setHeader("Content-type", "application/json;charset=UTF-8"); response.setStatus(200); try { response.getWriter().write(JSON.toJSONString(result, SerializerFeature.WriteMapNullValue)); } catch (IOException ex) { LOGGER.error(ex.getMessage()); } }
输入localhost:8080/userInfo/selectAll
输入localhost:8080/userInfo/selectAlla
在header中添加签名
创建core→interceptor→Interceptor1
package com.example.demo.core.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Interceptor1 implements HandlerInterceptor { /** * 在请求处理之前进行调用(Controller方法调用之前) * @param request * @param response * @param handler * @return * @throws Exception */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println(">>>MyInterceptor1>>>>>>> preHandle"); // 只有返回true才会继续向下执行,返回false取消当前请求 return true; } /** * 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后) * @param request * @param response * @param handler * @param modelAndView * @throws Exception */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println(">>>MyInterceptor1>>>>>>> postHandle"); } /** * 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) * @param request * @param response * @param handler * @param ex * @throws Exception */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println(">>>MyInterceptor1>>>>>>> afterCompletion"); } }
修改addInterceptors 方法如下
@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor( //注意,HandlerInterceptorAdapter 这里可以修改为自己创建的拦截器 new Interceptor1() { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String ization = request.getHeader("ization"); if(IZATION.equals(ization)){ return true; }else{ RetResult<Object> result = new RetResult<>(); result.setCode(RetCode.UNAUTHORIZED).setMsg("签名认证失败"); responseResult(response, result); return false; } } } //这里添加的是拦截的路径 /**为全部拦截 ).addPathPatterns("/userInfo/selectAlla"); }
输入localhost:8080/userInfo/selectAlla
在header中添加签名
成功
gitee.com/beany/mySpr…
写文章不易,如对您有帮助,请帮忙点下star
添加拦截器功能已完成,后续功能接下来陆续更新,有问题可以联系我mr_beany@163.com。另求各路大神指点,感谢大家。