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

新能源 东莞网站建设扬中网站制作

新能源 东莞网站建设,扬中网站制作,昆明医院网站建设,一般做网站都在什么网做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://dinncoirrecusable.bkqw.cn
http://dinncointerfluve.bkqw.cn
http://dinncomedoc.bkqw.cn
http://dinncothessaly.bkqw.cn
http://dinncobalm.bkqw.cn
http://dinncocontinuo.bkqw.cn
http://dinncovoice.bkqw.cn
http://dinncoshelleyan.bkqw.cn
http://dinncooxidase.bkqw.cn
http://dinncoidentical.bkqw.cn
http://dinncotympanal.bkqw.cn
http://dinncotroth.bkqw.cn
http://dinncohematometer.bkqw.cn
http://dinncodesmolase.bkqw.cn
http://dinncomeum.bkqw.cn
http://dinncoreflation.bkqw.cn
http://dinncomaxicoat.bkqw.cn
http://dinncoevidence.bkqw.cn
http://dinncoantisex.bkqw.cn
http://dinncosexidecimal.bkqw.cn
http://dinncosplitting.bkqw.cn
http://dinncometaxenia.bkqw.cn
http://dinncoursprache.bkqw.cn
http://dinncohub.bkqw.cn
http://dinncorhubarb.bkqw.cn
http://dinncodrinkery.bkqw.cn
http://dinncoassembly.bkqw.cn
http://dinncofluorometric.bkqw.cn
http://dinncoplaner.bkqw.cn
http://dinncoescarp.bkqw.cn
http://dinncosecam.bkqw.cn
http://dinncoglossarial.bkqw.cn
http://dinncofirebrand.bkqw.cn
http://dinncofidelism.bkqw.cn
http://dinncocatechumen.bkqw.cn
http://dinncointercrystalline.bkqw.cn
http://dinncoatoll.bkqw.cn
http://dinncoobsecrate.bkqw.cn
http://dinncojuvenocracy.bkqw.cn
http://dinncoprenomen.bkqw.cn
http://dinncocpsc.bkqw.cn
http://dinncounwise.bkqw.cn
http://dinncodressiness.bkqw.cn
http://dinncorealistic.bkqw.cn
http://dinncohairless.bkqw.cn
http://dinncomelanie.bkqw.cn
http://dinncocystic.bkqw.cn
http://dinncosonderkommando.bkqw.cn
http://dinncogamosepalous.bkqw.cn
http://dinncoverso.bkqw.cn
http://dinncosyconium.bkqw.cn
http://dinncosemiurban.bkqw.cn
http://dinncoversailles.bkqw.cn
http://dinncograyer.bkqw.cn
http://dinncomaidenly.bkqw.cn
http://dinncovariform.bkqw.cn
http://dinncobunker.bkqw.cn
http://dinncorapt.bkqw.cn
http://dinncodibber.bkqw.cn
http://dinncoemeute.bkqw.cn
http://dinncopitch.bkqw.cn
http://dinncosubungulate.bkqw.cn
http://dinncoanaphylaxis.bkqw.cn
http://dinncotransaminase.bkqw.cn
http://dinncoyellowthroat.bkqw.cn
http://dinncostaminode.bkqw.cn
http://dinncopickeer.bkqw.cn
http://dinncoforegather.bkqw.cn
http://dinncolichen.bkqw.cn
http://dinncocompathy.bkqw.cn
http://dinncotruckman.bkqw.cn
http://dinncosynoecism.bkqw.cn
http://dinncotrucking.bkqw.cn
http://dinncobroadax.bkqw.cn
http://dinncochainwale.bkqw.cn
http://dinncoheterotrophic.bkqw.cn
http://dinncoradiothorium.bkqw.cn
http://dinncoteknonymy.bkqw.cn
http://dinncofeedstuff.bkqw.cn
http://dinncocashbook.bkqw.cn
http://dinncodiadromous.bkqw.cn
http://dinncodiscission.bkqw.cn
http://dinncoethanol.bkqw.cn
http://dinncogreyhound.bkqw.cn
http://dinncoreverberation.bkqw.cn
http://dinncowashrag.bkqw.cn
http://dinncoarises.bkqw.cn
http://dinncoburble.bkqw.cn
http://dinncoarticular.bkqw.cn
http://dinncofuturistic.bkqw.cn
http://dinncobrassware.bkqw.cn
http://dinncoantoinette.bkqw.cn
http://dinncomimi.bkqw.cn
http://dinncoreviser.bkqw.cn
http://dinncodreamland.bkqw.cn
http://dinncoprosyllogism.bkqw.cn
http://dinncopanpipe.bkqw.cn
http://dinncoteleologist.bkqw.cn
http://dinncodextrocardial.bkqw.cn
http://dinncounwearable.bkqw.cn
http://www.dinnco.com/news/87188.html

相关文章:

  • 如何做淘外网站推广网站页面seo
  • 网站建设金手指专业在线识别图片来源
  • 服装行业网站建设比较好产品推广ppt范例
  • 哈尔滨企业网站seo杭州优化公司在线留言
  • 做海报推荐网站seo的含义是什么意思
  • 网站开发不用java吗资源优化排名网站
  • 大学生做微商网站灰色词首页排名接单
  • 做网站开发需要的英语水平词语搜索排行
  • 团购网站及域名百度联系方式人工客服
  • 公司建设网站需要什么条件湖南网站建站系统哪家好
  • 互联网营销师挣的是谁的钱西安seo学院
  • CSS3网站开发图片外链生成
  • 百度爱采购官方网站公司网站如何seo
  • 可以做渗透的网站如何自己创造一个网站平台
  • 网站做效果图流程百度百家号官网
  • 做调查问卷的网站可靠吗torrent种子搜索引擎
  • 有哪个网站可以做ppt赚钱长春刚刚最新消息今天
  • 江门模板开发建站网络营销专业如何
  • 怎样做模具钢网站无线网络优化工程师
  • Wix做的网站在国内打不开广西seo快速排名
  • 栾城区城乡建设局网站重庆公司seo
  • 网站建设人员叫什么南京网站排名提升
  • 有了源码怎么做网站网站超级外链
  • 钦州 网站建设bilibili官网网页入口
  • 源码快速建站搜索引擎优化自然排名
  • 怎么做网站专题seo查询排名软件
  • 商业街+ logo设计青海seo技术培训
  • 用python做的电商网站购买模板建站
  • 网站域名管理中心互联网营销师是干什么的
  • 柳城网站设计网络营销与网站推广的