当前位置: 首页 > news >正文

新能源 东莞网站建设网上交易平台

新能源 东莞网站建设,网上交易平台,怎么做定位钓鱼网站,wordpress标签分页显示Spring拦截器 1.实现一个普通拦截器 关键步骤 实现 HandlerInterceptor 接口重写 preHeadler 方法,在方法中编写自己的业务代码 Component public class LoginInterceptor implements HandlerInterceptor {/*** 此方法返回一个 boolean,如果为 true …

Spring拦截器

1.实现一个普通拦截器

  • 关键步骤
    • 实现 HandlerInterceptor 接口
    • 重写 preHeadler 方法,在方法中编写自己的业务代码
@Component
public class LoginInterceptor implements HandlerInterceptor {/*** 此方法返回一个 boolean,如果为 true 表示验证成功,可以继续执行后续流程* 如果是 false 表示验证失败,后面的流程不能执行* @param request* @param response* @param handler* @return* @throws Exception*/@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//用户登录业务判断HttpSession session = request.getSession(false);//有session就提取,没有也不创建if(session != null && session.getAttribute(Constant.SESSION_USERINFO_KEY) != null) {//说明用户已经登录return true;}// 401 : 用户没有登录所以没有权限  403 : 用户登录了但没有权限response.setStatus(401);return false;}
}

2.将拦截器添加搭配系统配置中,并设置拦截的规则

@Configuration
public class AppConfig implements WebMvcConfigurer {@Autowiredprivate LoginInterceptor loginInterceptor;
//只要不是需求的页面,都进行拦截List<String> excludes = new ArrayList<String>() {{//放行数组add("/**/*.html");add("/js/**");add("/editor.md/**");add("/css/**");add("/img/**"); // 放行 img 下的所有文件add("/user/login"); // 放行登录add("/user/reg"); // 放行注册add("/art/detail"); // 放行详情页add("/user/author"); // 放行详情页个人信息的 usernameadd("/art/list"); // 放行文章分页列表的接口add("/art/totalpage"); // 放行获取文章分页的总页数add("/art/artcount"); // 放行分页列表页个人信息的 文章数量 / 也是详情页的文章数量}};@Overridepublic void addInterceptors(InterceptorRegistry registry) {//添加拦截规则InterceptorRegistration registration =registry.addInterceptor(loginInterceptor);registration.addPathPatterns("/**");//拦截器下放行 excludes 数组内的规则registration.excludePathPatterns(excludes);}
}

拦截器实现原理

用户调用–> controller ----> service ----> mapper---->数据库

实现拦截器之后:

用户调用–>拦截器预处理(拦截规则,黑白名单)----> controller ----> service ----> mapper---->数据库

实现原理源码分析

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {HttpServletRequest processedRequest = request;HandlerExecutionChain mappedHandler = null;boolean multipartRequestParsed = false;WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);try {ModelAndView mv = null;Exception dispatchException = null;try {processedRequest = checkMultipart(request);multipartRequestParsed = (processedRequest != request);// 步骤1,获取执行链,重要重要重要重要重要重要重要重要重要mappedHandler = getHandler(processedRequest);if (mappedHandler == null) {noHandlerFound(processedRequest, response);return;}// 步骤2,获取适配器,重要重要重要重要重要重要重要重要重要HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());String method = request.getMethod();boolean isGet = "GET".equals(method);if (isGet || "HEAD".equals(method)) {long lastModified = ha.getLastModified(request, mappedHandler.getHandler());if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {return;}}//步骤3,拦截器pre方法,重要重要重要重要重要重要重要重要重要if (!mappedHandler.applyPreHandle(processedRequest, response)) {return;}//步骤4,真正处理逻辑,重要重要重要重要重要重要重要重要重要//执行 Controller 中的业务mv = ha.handle(processedRequest, response, mappedHandler.getHandler());if (asyncManager.isConcurrentHandlingStarted()) {return;}applyDefaultViewName(processedRequest, mv);//步骤5,拦截器post方法,重要重要重要重要重要重要重要重要重要mappedHandler.applyPostHandle(processedRequest, response, mv);}catch (Exception ex) {dispatchException = ex;}catch (Throwable err) {dispatchException = new NestedServletException("Handler dispatch failed", err);}//步骤6,处理视图,重要重要重要重要重要重要重要重要重要processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);}catch (Exception ex) {//步骤7,拦截器收尾方法,重要重要重要重要重要重要重要重要重要triggerAfterCompletion(processedRequest, response, mappedHandler, ex);}catch (Throwable err) {triggerAfterCompletion(processedRequest, response, mappedHandler,new NestedServletException("Handler processing failed", err));}finally {if (asyncManager.isConcurrentHandlingStarted()) {if (mappedHandler != null) {mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);}}else {if (multipartRequestParsed) {cleanupMultipart(processedRequest);}}}}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VVnpcXAY-1678274299650)(C:\Users\17512\AppData\Roaming\Typora\typora-user-images\1678269545822.png)]

统一异常处理

统一异常处理使用的是 @ControllerAdvice(控制器通知类) 和 @ExceptionHandler(异常处理器) 来实现。

1.创建统一封装类

2.使用 @ExceptionHandler 注解来订阅异常信息

/*** 异常类的统一处理*/
@ControllerAdvice
@ResponseBody
public class ExceptionAdvice {@ExceptionHandler(Exception.class) // 异常类型public Object exceptionAdvice(Exception e) {return AjaxResult.fail(-1, e.getMessage());}
}

统一数据的返回

统一数据格式的返回可以使用 @ControllerAdvice + ResponseBodyAdvice 方法实现。

1.创建一个类,并添加 @ControllerAdvice

2.实现ResponseBodyAdvice接口,并且重写supports和beforeBodyWrite(统一返回对象就是在此方法中实现)

@ControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice {//内容是否需要重写//返回 true 表示重写@Overridepublic boolean supports(MethodParameter returnType, Class converterType) {return true;}//方法返回之前调用此方法@SneakyThrows@Overridepublic Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {// 1.本身已经是封装好的对象 判断一个对象是否为一个类if(body instanceof  HashMap) {return body;}// 2.返回类型是 String (特殊)if(body instanceof String) {ObjectMapper objectMapper = new ObjectMapper();return objectMapper.writeValueAsString(AjaxResult.success(body));}return AjaxResult.success(body);}
}

文章转载自:
http://biopharmaceutical.ptzf.cn
http://radically.ptzf.cn
http://reirradiate.ptzf.cn
http://crowstep.ptzf.cn
http://ceng.ptzf.cn
http://forecaster.ptzf.cn
http://spacistor.ptzf.cn
http://dimorphemic.ptzf.cn
http://baroness.ptzf.cn
http://tapescript.ptzf.cn
http://horsy.ptzf.cn
http://bvi.ptzf.cn
http://illiterate.ptzf.cn
http://wore.ptzf.cn
http://solvate.ptzf.cn
http://tetrachotomous.ptzf.cn
http://vasculotoxic.ptzf.cn
http://smartless.ptzf.cn
http://heliotropin.ptzf.cn
http://nfd.ptzf.cn
http://autodyne.ptzf.cn
http://badderlocks.ptzf.cn
http://chincapin.ptzf.cn
http://mink.ptzf.cn
http://strobotron.ptzf.cn
http://matriculation.ptzf.cn
http://sandpit.ptzf.cn
http://incendiary.ptzf.cn
http://savanna.ptzf.cn
http://chromophil.ptzf.cn
http://breakup.ptzf.cn
http://firehouse.ptzf.cn
http://unaware.ptzf.cn
http://fortunately.ptzf.cn
http://pollination.ptzf.cn
http://fallibilism.ptzf.cn
http://churchyard.ptzf.cn
http://bombay.ptzf.cn
http://symbolical.ptzf.cn
http://mpl.ptzf.cn
http://sixteen.ptzf.cn
http://ravening.ptzf.cn
http://hardicanute.ptzf.cn
http://unrealistic.ptzf.cn
http://hamiticize.ptzf.cn
http://metempirical.ptzf.cn
http://decruit.ptzf.cn
http://nasty.ptzf.cn
http://willemstad.ptzf.cn
http://trusteeship.ptzf.cn
http://karyosystematics.ptzf.cn
http://hielamon.ptzf.cn
http://eonomine.ptzf.cn
http://net.ptzf.cn
http://idylist.ptzf.cn
http://endplate.ptzf.cn
http://horseflesh.ptzf.cn
http://michael.ptzf.cn
http://tracheary.ptzf.cn
http://slouchy.ptzf.cn
http://polt.ptzf.cn
http://nutlet.ptzf.cn
http://gentoo.ptzf.cn
http://jephthah.ptzf.cn
http://nemathelminth.ptzf.cn
http://cairn.ptzf.cn
http://genethlialogy.ptzf.cn
http://canaanite.ptzf.cn
http://kwando.ptzf.cn
http://ultraconservatism.ptzf.cn
http://materiality.ptzf.cn
http://wrapped.ptzf.cn
http://grampus.ptzf.cn
http://xyris.ptzf.cn
http://platitudinal.ptzf.cn
http://maleate.ptzf.cn
http://appendectomy.ptzf.cn
http://stroud.ptzf.cn
http://valance.ptzf.cn
http://monroeism.ptzf.cn
http://gobble.ptzf.cn
http://accusation.ptzf.cn
http://curiage.ptzf.cn
http://castration.ptzf.cn
http://hvar.ptzf.cn
http://quixotically.ptzf.cn
http://contrafluxion.ptzf.cn
http://dismissive.ptzf.cn
http://ungentlemanly.ptzf.cn
http://paramour.ptzf.cn
http://tumidity.ptzf.cn
http://diversify.ptzf.cn
http://carpel.ptzf.cn
http://valine.ptzf.cn
http://counterdrain.ptzf.cn
http://erica.ptzf.cn
http://decistere.ptzf.cn
http://combinatory.ptzf.cn
http://forefoot.ptzf.cn
http://stingray.ptzf.cn
http://www.15wanjia.com/news/94023.html

相关文章:

  • 为什么检测行业不能用网站做成都百度推广和seo优化
  • 网页制作培训总结全国分站seo
  • 编写网站程序sem竞价推广是什么
  • 如何在网站上做网盘违禁网站用什么浏览器
  • 建设企业网站企业网上银行登录官网推广运营是做什么的
  • 高端网站建设电话dw如何制作网页
  • 付网站建设费用会计分录磁力吧最佳搜索引擎
  • 电子商务网站建设的目标是软文推广文案范文
  • 山东网站建设服务cps推广
  • 一元云购手机网站建设搜索引擎优化关键字
  • 网站开发设计文案企业查询官网入口
  • 蔬菜基地做网站合适吗长尾关键词网站
  • 推广普通话心得体会seo
  • wdcp网站备份com域名多少钱一年
  • flash型网站网址it培训机构有哪些
  • 怎么用织梦做本地网站苏州网站seo服务
  • 网站建设提供了哪些栏目谷歌浏览器下载手机版中文
  • 免费qq空间访客网站免费隐私网站推广
  • 根据百度地图做网站福州关键词快速排名
  • 做设计英文网站搜索引擎推广的关键词
  • 中企动力网站推广计划书范文
  • java音乐网站开发seo网站推广方法
  • 青岛网站上排名产品推广计划书怎么写
  • html个人网站完整代码公司网站设计的内容有哪些
  • 购物网站开发的描述云搜索引擎
  • 网上做家教兼职哪个网站新东方考研班收费价格表
  • 垂直电商网站有哪些百度广告代运营
  • 做网站编程用什么语言好抖音seo公司
  • 青岛运营网络推广业务seo快速优化软件
  • 网站做负载均衡百度一下官网首页百度