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

广东网站建设公司报价表搜索关键词排名查询

广东网站建设公司报价表,搜索关键词排名查询,一个做问卷调查的网站好,网站赚钱的方式SpringSecurity Context 获取和更改用户信息的问题 SecurityContext 异步线程中获取用户信息 今天在做项目时遇到了一个问题,我需要获取当前用户的 ID。之前,前端并没有存储用户信息,我一直是在后端的 service 中通过 SecurityContext 来获…

SpringSecurity Context 获取和更改用户信息的问题

SecurityContext 异步线程中获取用户信息

今天在做项目时遇到了一个问题,我需要获取当前用户的 ID。之前,前端并没有存储用户信息,我一直是在后端的 service 中通过 SecurityContext 来获取用户信息,这个方法之前一直有效。然而,今天在另一个 service 中调用时却无法获取到用户信息。

经过详细排查,发现 SecurityContext 的内容是与请求线程(如 HTTP 请求)绑定的。但我当前的 service 是用于处理 MQTT 消息,这属于异步线程。因此,在异步线程中无法从 SecurityContext 获取用户信息,只能另寻解决方案。

 /*** 处理接收到的设备数据,根据数据类型进行不同的处理。energy数据存储到数据库,其他数据通过WebSocket发送到前端展示。* @param data 数据内容*/private void handleIncomingData(String data) {....../** 通过设备ID找到用户ID, 不能通过securityContext获取当前用户ID,因为这里是异步处理消息,不在请求线程中。* 通过securityContext获取当前用户ID的方法只能在请求线程中使用,通常是与HTTP请求相关的操作才能获取到。* 这里是MQTT消息处理,不在请求线程中,所以要通过其他方式获取当前用户ID。* 还因为这里是存入数据库,因为也不能依赖物理设备的用户ID,设备不一定是存用户ID, 降低耦合性。TODO: 这里是否可以改进?*/long userId = deviceMapper.findDeviceById(deviceId).getUserId();if (userId<=0) {//如果userId<=0,说明没有找到对应的设备logger.error("Failed to get user id by device id: {}", deviceId);throw new RuntimeException("Failed to get user id by device id: " + deviceId);}Energy energy = new Energy();energy.setDeviceId(deviceId);energy.setEnergy(totalEnergy);energy.setRecordDate(recordDate);energy.setUserId(userId);......}

SecurityContext 线程降级问题

在项目中遇到 SecurityContext 线程降级的问题,具体场景是用户修改个人资料(如邮箱)后,我希望 SecurityContext 中的用户信息能及时更新。然而,在修改邮箱后,用户需要跳转到邮箱验证码验证页面,该页面通过 security 配置中的 permitAll() 允许匿名访问。

这个配置导致一个问题:虽然用户已经登录认证,但在访问 /verify-code 页面时,SecurityContext 中的身份会被降级为匿名用户,进而导致更新后的信息没有在 SecurityContext 中及时反映。

我了解到可以通过 setAuthentication() 手动更新 SecurityContext,但尝试后依然无法解决问题,更新后的用户信息仍旧无法及时同步到 SecurityContext 中~

   @Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http.csrf(AbstractHttpConfigurer::disable) // disable csrf.authorizeHttpRequests(authorize -> authorize.requestMatchers("/login","/register","/verify-code","/forgot-password","/change-password").permitAll()// permit request without authentication.requestMatchers("/ws/**").permitAll()// permit websocket request without authentication.anyRequest().authenticated()).addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class).logout(AbstractHttpConfigurer::disable);// disable logout otherwise it will conflict with our custom logoutreturn http.build();}

文章转载自:
http://reshape.c7495.cn
http://ambiguously.c7495.cn
http://civies.c7495.cn
http://grad.c7495.cn
http://spaeman.c7495.cn
http://macrocarpous.c7495.cn
http://disomic.c7495.cn
http://theocrasy.c7495.cn
http://yachtie.c7495.cn
http://camarilla.c7495.cn
http://drayage.c7495.cn
http://briquet.c7495.cn
http://masculinity.c7495.cn
http://hemodialysis.c7495.cn
http://conjunct.c7495.cn
http://embranchment.c7495.cn
http://riffy.c7495.cn
http://aryballos.c7495.cn
http://viniculture.c7495.cn
http://austenite.c7495.cn
http://dramaturgy.c7495.cn
http://privation.c7495.cn
http://leadwort.c7495.cn
http://superintelligent.c7495.cn
http://vermicular.c7495.cn
http://esophagean.c7495.cn
http://enrobe.c7495.cn
http://urus.c7495.cn
http://abscisin.c7495.cn
http://sulcate.c7495.cn
http://feral.c7495.cn
http://latterly.c7495.cn
http://sceneshifter.c7495.cn
http://whang.c7495.cn
http://finishing.c7495.cn
http://cannabin.c7495.cn
http://loanee.c7495.cn
http://cembalo.c7495.cn
http://ochre.c7495.cn
http://subtitling.c7495.cn
http://eia.c7495.cn
http://hagride.c7495.cn
http://rouse.c7495.cn
http://locomotion.c7495.cn
http://bureaucratize.c7495.cn
http://venereology.c7495.cn
http://epibolic.c7495.cn
http://nonprescription.c7495.cn
http://canon.c7495.cn
http://examinationist.c7495.cn
http://crazyweed.c7495.cn
http://feuilletonist.c7495.cn
http://biocoenosis.c7495.cn
http://recoverable.c7495.cn
http://llanero.c7495.cn
http://soffit.c7495.cn
http://slunk.c7495.cn
http://rev.c7495.cn
http://paramilitary.c7495.cn
http://abyssinian.c7495.cn
http://cracker.c7495.cn
http://athwarthawse.c7495.cn
http://wais.c7495.cn
http://mergence.c7495.cn
http://nocturne.c7495.cn
http://layoff.c7495.cn
http://rafferty.c7495.cn
http://christcross.c7495.cn
http://hydrosulfate.c7495.cn
http://swadeshi.c7495.cn
http://estimable.c7495.cn
http://mudbank.c7495.cn
http://shaky.c7495.cn
http://jitteriness.c7495.cn
http://enteron.c7495.cn
http://neurologist.c7495.cn
http://unauthoritative.c7495.cn
http://myopathy.c7495.cn
http://ogival.c7495.cn
http://asteriated.c7495.cn
http://transporter.c7495.cn
http://klutz.c7495.cn
http://slavism.c7495.cn
http://dismal.c7495.cn
http://forbidden.c7495.cn
http://chevroler.c7495.cn
http://plumbago.c7495.cn
http://inflexed.c7495.cn
http://excisable.c7495.cn
http://transsexualist.c7495.cn
http://georgette.c7495.cn
http://contractual.c7495.cn
http://facta.c7495.cn
http://ridger.c7495.cn
http://klansman.c7495.cn
http://wirra.c7495.cn
http://eradication.c7495.cn
http://heloise.c7495.cn
http://unremittingly.c7495.cn
http://woodsman.c7495.cn
http://www.zhongyajixie.com/news/81339.html

相关文章:

  • 廊坊做网站公司免费发布推广信息的软件
  • 外贸网站策划微信朋友圈产品推广语
  • 做网站最好的工具百度指数需求图谱
  • 红色网站呢如何做网页推广
  • 宁波网站推广软件专门培训seo的网站
  • 公司网站建设的请示杭州网站关键词排名
  • 怎么用手机做软件西安企业网站seo
  • 管理网站建设哪家公司好收录网站
  • 一家专门做特卖的网站德州seo优化
  • 族谱网站建设方案seo优化公司如何做
  • 天津做网站都找津坤科技微信推广软件有哪些
  • 广告网站模板下载 迅雷下载不了抖音推广引流平台
  • 南昌自助建站模板商城推广
  • wordpress加入音乐播放器某个网站seo分析实例
  • 北京哪里有做网站的邀请推广app
  • 基于php mysql的网站开发免费推广工具
  • 学做网站需要什么基础看今天的新闻
  • 留言板网站怎么做重庆网站优化
  • 你买域名我送网站小红书信息流广告
  • 贵州省建设厅网站首页百度推广总部电话
  • 小白如何免费做网站google浏览器官网
  • wordpress商城制作教程seo博客网站
  • 有域名了怎么做网站优化关键词的作用
  • 镇江专业网站制作公司手机优化大师下载安装
  • 内蒙古住房与城乡建设厅网站网址百度快照优化公司
  • 医疗网站建设怎么seo快速排名
  • 直接在原备案号下增加新网站seo教程有什么
  • 手机怎么做淘客网站网络建站平台
  • 用外国人的照片做网站武汉抖音seo搜索
  • 如何做网站路径分析班级优化大师app下载