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

网站建设 繁体搜资源

网站建设 繁体,搜资源,网络建设费是什么,北京注册公司代办一般多少费用我喜欢你,可是你却并不知道. 上一章简单介绍了SpringBoot参数验证(三十七) ,如果没有看过,请观看上一章 关于过滤器和拦截器已经讲很多了, 这里老蝴蝶只说一下 SpringBoot 的用法。 可以看之前的文章: https://blog.csdn.net/yjltx1234csdn/article/d…

我喜欢你,可是你却并不知道.

上一章简单介绍了SpringBoot参数验证(三十七) ,如果没有看过,请观看上一章

关于过滤器和拦截器已经讲很多了, 这里老蝴蝶只说一下 SpringBoot 的用法。

可以看之前的文章:

https://blog.csdn.net/yjltx1234csdn/article/details/100667082

https://blog.csdn.net/yjltx1234csdn/article/details/105484464

一.过滤器

一.一 过滤器实现 Filter 接口

@Slf4j
public class LogFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {Filter.super.init(filterConfig);}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, 		ServletException {// 主体方法long start = System.currentTimeMillis();chain.doFilter(request, response);log.info("Execute cost= {}" ,(System.currentTimeMillis() - start));}@Overridepublic void destroy() {Filter.super.destroy();}
}

一.二 配置过滤器

@Configuration
public class WebFilterConfig {@Beanpublic FilterRegistrationBean registFilter() {FilterRegistrationBean registration = new FilterRegistrationBean();registration.setFilter(new LogFilter());registration.addUrlPatterns("/*");registration.setName("LogCostFilter");registration.setOrder(1);return registration;}}

一.三 过滤器代码

  @GetMapping("/hello")public String hello() {try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}return "你好啊,岳泽霖";}
2023-04-04 17:54:20.769  INFO 29068 --- [nio-8081-exec-4] top.yueshushu.learn.config.LogFilter     : Execute cost= 2012

二. 拦截器

设置一个登录拦截器

用户对象

@Data
public class User {private static final long serialVersionUID = 1L;private Integer id;private String account;private String name;private String token;// ... 其他属性信息
}

二.一 @AuthToken 注解

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthToken {}

二.二 拦截器 AuthorizationInterceptor

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import top.yueshushu.learn.annotation.AuthToken;
import top.yueshushu.learn.model.User;
import top.yueshushu.learn.util.ThreadLocalUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.lang.reflect.Method;@Slf4j
public class AuthorizationInterceptor implements HandlerInterceptor {private String httpHeaderName = "Authorization";public static final String X_REAL_IP = "x-real-ip";public static final String REQUEST_CURRENT_KEY = "REQUEST_CURRENT_KEY";@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {if (!(handler instanceof HandlerMethod)) {return true;}HandlerMethod handlerMethod = (HandlerMethod) handler;Method method = handlerMethod.getMethod();boolean isForAllTimeUser = false;if (method.getAnnotation(AuthToken.class) != null ||handlerMethod.getBeanType().getAnnotation(AuthToken.class) != null) {String token = request.getHeader(httpHeaderName);String ip = request.getHeader(X_REAL_IP);User loginUser = null;if (StringUtils.hasText(token)) {// 根据 token 获取用户 信息// 验证 token 是否 jwt 过期, 过期的话, isFalse}if (!StringUtils.hasText(token) || loginUser == null) {isFalse(request, response);return false;}String userDoAccount = loginUser.getAccount();ThreadLocalUtils.put("userDoAccount", userDoAccount);ThreadLocalUtils.put("ip", ip == null ? request.getRemoteAddr() : ip);ThreadLocalUtils.put("userName", loginUser.getAccount());ThreadLocalUtils.put("user", loginUser);ThreadLocalUtils.put("token", token);ThreadLocalUtils.put("userId", loginUser.getId());// 可以进行续命, 将时间延长.return true;}request.setAttribute(REQUEST_CURRENT_KEY, null);return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {ThreadLocalUtils.release();}private void isFalse(HttpServletRequest httpServletRequest, HttpServletResponse response) {JSONObject jsonObject = new JSONObject();PrintWriter out = null;try {response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);response.setContentType(MediaType.APPLICATION_JSON_VALUE);response.setCharacterEncoding("utf-8");jsonObject.put("code", "10010001");jsonObject.put("message", "登录已过期,请重新登录");//添加其余的两个属性  success和 datajsonObject.put("data", "");jsonObject.put("success", false);out = response.getWriter();out.println(jsonObject);} catch (Exception e) {log.error("发生异常{}", e);} finally {if (out != null) {out.flush();out.close();}}}private void isUnValid(HttpServletRequest httpServletRequest, HttpServletResponse response) {JSONObject jsonObject = new JSONObject();PrintWriter out = null;try {response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);response.setContentType(MediaType.APPLICATION_JSON_VALUE);response.setCharacterEncoding("utf-8");jsonObject.put("code", "10010002");jsonObject.put("message", "您的账号已经在另一处登录了,您被迫下线!");//添加其余的两个属性  success和 data, 解决 PDA端无法翻译 退出的问题。 @zk_yjljsonObject.put("data", "");jsonObject.put("success", false);out = response.getWriter();out.println(jsonObject);} catch (Exception e) {log.error("发生异常", e);} finally {if (out != null) {out.flush();out.close();}}}
}

二.三 线程内工具类

public class ThreadLocalUtils {private static final ThreadLocal<Map<String, Object>> THREAD_LOCAL = new ThreadLocal<>();/*** 存储*/public static void put(String key, Object value) {Map<String, Object> map = THREAD_LOCAL.get();if (map == null) {map = new HashMap<>(1,1);}map.put(key, value);THREAD_LOCAL.set(map);}/*** 取值*/public static <T> T get(String key) {Map<String, Object> map = THREAD_LOCAL.get();if (map != null) {return (T) map.get(key);}return null;}/*** 获取当前用户*/public static User getUser() {Map<String, Object> map = THREAD_LOCAL.get();return (User) map.get("user");}public static void release() {THREAD_LOCAL.remove();}public static String getToken() {return get("token");}
}

二.四 添加拦截器


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.yueshushu.learn.interceptor.AuthorizationInterceptor;/*** @ClassName:MvcConfig* @Description Web端配置* @Author zk_yjl* @Date 2021/6/29 16:31* @Version 1.0* @Since 1.0**/
@Configuration
public class MvcConfig implements WebMvcConfigurer {@Beanpublic HandlerInterceptor getAuthInterceptor() {//返回自定义的拦截类return new AuthorizationInterceptor();}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(getAuthInterceptor())// 拦截所有请求.addPathPatterns("/**")// 静态资源过滤.excludePathPatterns("/usr/login","/static/**","/v2/**", "/swagger-resources/configuration/**","/swagger-resources/**", "/swagger-ui.html#/**", "/webjars/**");}/*** 配置静态的资源信息** @param registry*/@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");}
}

二.五 处理数据

  @GetMapping("/hello2")@AuthTokenpublic String hello2() {try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}// 线程内获取User user = ThreadLocalUtils.getUser();return "你好啊,岳泽霖";}

本章节的代码放置在 github 上:

https://github.com/yuejianli/springboot/tree/develop/SpringBoot_Interceptor

谢谢您的观看,如果喜欢,请关注我,再次感谢 !!!


文章转载自:
http://dinncodeepfry.stkw.cn
http://dinncogreenfly.stkw.cn
http://dinncoconcutient.stkw.cn
http://dinncocartogram.stkw.cn
http://dinncopetrologic.stkw.cn
http://dinncosympatholytic.stkw.cn
http://dinnconabeshima.stkw.cn
http://dinncosulfurize.stkw.cn
http://dinncoovercrop.stkw.cn
http://dinncoverbalism.stkw.cn
http://dinncobiauricular.stkw.cn
http://dinncolapsible.stkw.cn
http://dinncodynamometry.stkw.cn
http://dinncolaughably.stkw.cn
http://dinncotoughly.stkw.cn
http://dinncofree.stkw.cn
http://dinncocarambola.stkw.cn
http://dinncopregnant.stkw.cn
http://dinncoenterozoan.stkw.cn
http://dinncovaud.stkw.cn
http://dinncoscalarly.stkw.cn
http://dinncolightface.stkw.cn
http://dinncosupraspinal.stkw.cn
http://dinncotestamur.stkw.cn
http://dinncocedarn.stkw.cn
http://dinncokurbash.stkw.cn
http://dinncoplotline.stkw.cn
http://dinncoarcade.stkw.cn
http://dinncoferdinand.stkw.cn
http://dinncooverflow.stkw.cn
http://dinncohippic.stkw.cn
http://dinncoallround.stkw.cn
http://dinncoattribution.stkw.cn
http://dinncochambermaid.stkw.cn
http://dinncoindophenol.stkw.cn
http://dinncodeclutch.stkw.cn
http://dinncodemythologize.stkw.cn
http://dinncoslubbing.stkw.cn
http://dinncondp.stkw.cn
http://dinncodowntrod.stkw.cn
http://dinncodumb.stkw.cn
http://dinncovariolite.stkw.cn
http://dinncoradiotoxic.stkw.cn
http://dinncococci.stkw.cn
http://dinncosivaite.stkw.cn
http://dinncorepaid.stkw.cn
http://dinncolouis.stkw.cn
http://dinncofoiling.stkw.cn
http://dinncosidetrack.stkw.cn
http://dinncoanhydro.stkw.cn
http://dinncoinsensate.stkw.cn
http://dinncomorose.stkw.cn
http://dinncotyrannical.stkw.cn
http://dinncobrickie.stkw.cn
http://dinncomitered.stkw.cn
http://dinncomanxwoman.stkw.cn
http://dinncoyapese.stkw.cn
http://dinncorosser.stkw.cn
http://dinncobeaufort.stkw.cn
http://dinncoradially.stkw.cn
http://dinncosubemployed.stkw.cn
http://dinncotrebly.stkw.cn
http://dinncoappraisement.stkw.cn
http://dinncovane.stkw.cn
http://dinncotankbuster.stkw.cn
http://dinncoinjunct.stkw.cn
http://dinncogasser.stkw.cn
http://dinncodalailama.stkw.cn
http://dinncotricap.stkw.cn
http://dinncospinneret.stkw.cn
http://dinncothimbleberry.stkw.cn
http://dinncoconsubstantial.stkw.cn
http://dinncoaminoaciduria.stkw.cn
http://dinncoantiozonant.stkw.cn
http://dinncocleanbred.stkw.cn
http://dinncoperiodontics.stkw.cn
http://dinncofriar.stkw.cn
http://dinncoxenophile.stkw.cn
http://dinncodivider.stkw.cn
http://dinncoethnomycology.stkw.cn
http://dinncoastp.stkw.cn
http://dinnconeighbourship.stkw.cn
http://dinncoformulism.stkw.cn
http://dinncomoneyed.stkw.cn
http://dinncorepellency.stkw.cn
http://dinncoaerobomb.stkw.cn
http://dinncoimplicit.stkw.cn
http://dinncocoelom.stkw.cn
http://dinncocuboidal.stkw.cn
http://dinncostrata.stkw.cn
http://dinncocentrifugalization.stkw.cn
http://dinncocatenative.stkw.cn
http://dinncohydrosoma.stkw.cn
http://dinncocolicroot.stkw.cn
http://dinncoidumaean.stkw.cn
http://dinncostratopause.stkw.cn
http://dinncoevasively.stkw.cn
http://dinncopatience.stkw.cn
http://dinncobrougham.stkw.cn
http://dinncoplenitude.stkw.cn
http://www.dinnco.com/news/148166.html

相关文章:

  • 一个网站两个域名外贸网站推广seo
  • 东莞市建设培训中心网站2021年度关键词有哪些
  • 网站怎样做优化网站优化资源
  • 哪个网站有做阿里巴巴流量网盟推广是什么意思
  • 给公司建网站 深圳公司网络推广该怎么做
  • 做网站收费杭州seo关键字优化
  • 网站建设烟台网站怎么创建
  • 网站开发程序测试维护人员友链购买有效果吗
  • 设计理念万能模板整站优化案例
  • 专题网站建设意义何在微信crm系统软件
  • 做本地团购网站怎么样百度指数大数据
  • 作业提交免费网站大二网页设计作业成品
  • 网站提示建设中免费网站制作软件平台
  • 江西建设城乡网站查询上海seo优化
  • 哈尔滨信息网租房信息小红书seo排名帝搜软件
  • 公司的网站怎么运营seo短视频网页入口
  • 郑州郑东新区网站建设免费网络推广平台有哪些
  • 内部的网络营销推广渠道防疫优化措施
  • 常用的网站制作西安seo网络优化公司
  • 南京h5设计公司seo算法优化
  • epanel wordpress优化网站性能监测
  • 有什么办法做自己的网站自助建站系统开发
  • wordpress免费网站模板网站友情链接查询
  • 怎么做企业推广关键词的分类和优化
  • 泉州做网站公司58同城关键词怎么优化
  • 关于节约化建设网站的表态发言2023网站推广入口
  • 手工制作小玩具简单又好玩北京seo关键词排名优化
  • 用数据库做新闻网站系统如何做好网站推广优化
  • 微信如何做商城网站上海百度关键词优化公司
  • 百度做一个网站多少钱百度seo排名优化费用