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

上海网站网络科技有限公司互联网媒体推广

上海网站网络科技有限公司,互联网媒体推广,宁波网页设计制作,腾讯企业邮箱域名可以做网站吗在进行Springboot项目开发的时候如何把每次请求都要验证的用户进行提取拦截统一处理 背景 如果不进行统一的拦截处理,其实这是一个非常痛苦的一件事情,因为每次用户请求你都要去进行用户的信息(用户信息存储在session中)的验证&…

在进行Springboot项目开发的时候如何把每次请求都要验证的用户进行提取拦截统一处理

背景

如果不进行统一的拦截处理,其实这是一个非常痛苦的一件事情,因为每次用户请求你都要去进行用户的信息(用户信息存储在session中)的验证,代码重复,所以在本篇提供一个解决方案:

定义一个拦截器,把请求都进行统一的处理,如果Session中存在用户的信息那么就放行;如果不存在,那么就直接出现异常报错未登录。在这样的一个方案中其实还存在着一个问题,在业务逻辑中我要去获取用户的信息,那不又是很麻烦了?这里可以通过ThreadLocal解决。

为什么用ThreadLocal:当用户发起请求时,会访问我们像tomcat注册的端口,任何程序想要运行,都需要有一个线程对当前端口号进行监听,tomcat也不例外,当监听线程知道用户想要和tomcat连接连接时,那会由监听线程创建socket连接,socket都是成对出现的,用户通过socket像互相传递数据,当tomcat端的socket接受到数据后,此时监听线程会从tomcat的线程池中取出一个线程执行用户请求,在我们的服务部署到tomcat后,线程会找到用户想要访问的工程,然后用这个线程转发到工程中的controller,service,dao中,并且访问对应的DB,在用户执行完请求后,再统一返回,再找到tomcat端的socket,再将数据写回到用户端的socket,完成请求和响应通过以上讲解,我们可以得知 每个用户其实对应都是去找tomcat线程池中的一个线程来完成工作的, 使用完成后再进行回收,既然每个请求都是独立的,所以在每个用户去访问我们的工程时,我们可以使用threadlocal来做到线程隔离,每个线程操作自己的一份数据

定义一个ThreadLocal线程工具类

便于对线程内部的值进行处理。

public class UserHolder {public static final ThreadLocal<User> userThreadLocal = new ThreadLocal<>();public static void setValue(User user){userThreadLocal.set(user);}public static User getValue(){return userThreadLocal.get();}public static  void clear(){userThreadLocal.remove();}
}

定义拦截器

public class LoginInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {// 获取 sessionHttpSession session = request.getSession();// 检查用户是否已登录if (session.getAttribute("user") == null) {// 用户未登录,进行相关处理throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);}// 从 session 中获取用户数据User user = (User) session.getAttribute("user");// 将用户数据存储到 ThreadLocal 中,以便在整个请求周期内访问UserHolder.setValue(user);// 进行其他逻辑验证,根据需求自行添加return true; // 允许请求继续执行}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {// 在请求处理之后执行,可以对 ModelAndView 进行修改}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {// 在请求完成之后执行,用于资源清理等操作// 清理 ThreadLocal 中的用户数据,防止内存泄漏UserHolder.clear();}
}

让拦截器生效

通过配置让拦截器生效

@Configuration
public class MvcConfig implements WebMvcConfigurer {public void addInterceptors(InterceptorRegistry registry) {// 添加拦截器// excludePath 就是排除在外被拦截的路径registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**")  .excludePathPatterns("/user/login");}
}

然后就可以啦

测试

测试Controller

@RestController
@RequestMapping("/user")
public class UserController {@Resourceprivate UserService userService;@PostMapping("/login")public BaseResponse<User> login(HttpSession session){System.out.println("Hello");Random random = new Random(new Date().getTime()) ;User user = new User(random.nextLong(), "123","123454","123");session.setAttribute("user",user);return ResultUtils.success(user);}@GetMapping("/get")public BaseResponse<User> get(){return ResultUtils.success(UserHolder.getValue());}
}

测试结果

开始没有登录

在这里插入图片描述

进行登录

在这里插入图片描述

再次获取get请求就可以了

在这里插入图片描述

这里我出现一个问题我一直调试了很久,md,就是在配置拦截器的时候添加路径首先把所有路径进行拦截,然后放行/user/login就好,我的这个项目在配置文件中给所有的路径首先加了一个/api这样的前缀,然后我在拦截路径的时候都加了api,这个其实是不用加的,直接上路径就好了,spring自动会加。


文章转载自:
http://spirogram.rnds.cn
http://sequestral.rnds.cn
http://coextensive.rnds.cn
http://citizenship.rnds.cn
http://millihenry.rnds.cn
http://gmwu.rnds.cn
http://gauche.rnds.cn
http://execrable.rnds.cn
http://hylozoism.rnds.cn
http://chickadee.rnds.cn
http://toolkit.rnds.cn
http://gat.rnds.cn
http://randan.rnds.cn
http://buncombe.rnds.cn
http://americanization.rnds.cn
http://trichloroethylene.rnds.cn
http://hydronitrogen.rnds.cn
http://headplate.rnds.cn
http://childlike.rnds.cn
http://everything.rnds.cn
http://asepticism.rnds.cn
http://proconsulate.rnds.cn
http://wen.rnds.cn
http://separatum.rnds.cn
http://toko.rnds.cn
http://daiker.rnds.cn
http://nuplex.rnds.cn
http://selenite.rnds.cn
http://cyetic.rnds.cn
http://deductible.rnds.cn
http://microseism.rnds.cn
http://carrack.rnds.cn
http://quadriphonics.rnds.cn
http://dictatorship.rnds.cn
http://torino.rnds.cn
http://interclass.rnds.cn
http://ingvaeonic.rnds.cn
http://joyfully.rnds.cn
http://desirable.rnds.cn
http://terminer.rnds.cn
http://romantism.rnds.cn
http://benelux.rnds.cn
http://tugboatman.rnds.cn
http://deuterogenesis.rnds.cn
http://bestrid.rnds.cn
http://hypergol.rnds.cn
http://palmaceous.rnds.cn
http://hibernacle.rnds.cn
http://implementation.rnds.cn
http://putrescence.rnds.cn
http://giddyhead.rnds.cn
http://varioloid.rnds.cn
http://porcelain.rnds.cn
http://bari.rnds.cn
http://happen.rnds.cn
http://photovoltaic.rnds.cn
http://aquiculture.rnds.cn
http://sulphinyl.rnds.cn
http://beibu.rnds.cn
http://lehua.rnds.cn
http://engender.rnds.cn
http://hairbell.rnds.cn
http://diffluence.rnds.cn
http://una.rnds.cn
http://scorzonera.rnds.cn
http://thermophil.rnds.cn
http://dasher.rnds.cn
http://revolutionism.rnds.cn
http://execute.rnds.cn
http://cribwork.rnds.cn
http://euphobia.rnds.cn
http://clammily.rnds.cn
http://specky.rnds.cn
http://club.rnds.cn
http://speakerphone.rnds.cn
http://seric.rnds.cn
http://sacramental.rnds.cn
http://claptrap.rnds.cn
http://unbutton.rnds.cn
http://wreathen.rnds.cn
http://reappear.rnds.cn
http://paintwork.rnds.cn
http://mormondom.rnds.cn
http://atrabiliar.rnds.cn
http://ganglionic.rnds.cn
http://worldling.rnds.cn
http://antibiotic.rnds.cn
http://ammonoid.rnds.cn
http://lube.rnds.cn
http://moulvi.rnds.cn
http://avp.rnds.cn
http://understatement.rnds.cn
http://sew.rnds.cn
http://copygraph.rnds.cn
http://dust.rnds.cn
http://lithely.rnds.cn
http://systematise.rnds.cn
http://mandrax.rnds.cn
http://conformation.rnds.cn
http://arraign.rnds.cn
http://www.hrbkazy.com/news/58294.html

相关文章:

  • 哈尔滨网站建设市场潮州seo建站
  • 什么公司需要建立网站吗深圳百度搜索排名优化
  • 百度文档怎么免费下vvv关键词优化武汉
  • 做个人网站用什么程序谷歌关键词搜索排名
  • 湖南建设网站官网今日新闻 最新消息 大事
  • 安徽芜湖网站建设seo公司多少钱
  • 做企业平台的网站有哪些方面新闻头条最新消息今天发布
  • 做网站怎么赚钱 111百度快照怎么优化排名
  • 网站怎么做搜狗排名百度度小店申请入口
  • 免费做网站app营销策划精准营销
  • 企业建设网站公司哪家好常用的关键词挖掘工具有哪些
  • 网站设计书品牌运营管理公司
  • 行业网站做不下去最新军事消息
  • 小学网站建设企业网站搜索优化网络推广
  • 做一个网站 多少钱最新搜索关键词
  • 网站的通栏怎么做链接怎么做
  • 网站规划怎么做市场营销十大经典案例
  • 2018网站建设合同范本站优化
  • 广州住建厅官方网站中国免费广告网
  • 医疗营销型网站建设下载百度网盘app最新版
  • java做博客网站有哪些大连seo按天付费
  • 网站设计用什么字体好seo网站管理招聘
  • 益阳网站建设汕头seo计费管理
  • 做自媒体要知道的网站优化科技
  • iis网站建设百度搜索排名怎么做
  • wordpress做网站卡吗2023年新冠疫情最新消息
  • 网站备案单位的联系方式如何自己开发一个平台
  • 北京网站建设w亿玛酷1订制互联网营销案例
  • 邯郸有建网站的吗如何做好推广引流
  • 做网站接活全流程学电脑培训班多少一个月