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

做网站购买模板百度手游排行榜

做网站购买模板,百度手游排行榜,网页制作基础教程本社,做网站建设工资多少web如何取得用户信息 之前说过SecurityContextHolder默认使用的是ThreadLocal来进行存储的,而且每次都会清除,但是web每次请求都会验证用户权限,这是如何做到的呢? 这是通过SecurityContextPersistenceFilter来实现的&#xff0…

web如何取得用户信息

之前说过SecurityContextHolder默认使用的是ThreadLocal来进行存储的,而且每次都会清除,但是web每次请求都会验证用户权限,这是如何做到的呢?

这是通过SecurityContextPersistenceFilter来实现的,每次请求过来都会session中来获取SecurityContext,然后设置到SecurityContextHolder中,请求结束后再清除掉

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) res;if (request.getAttribute(FILTER_APPLIED) != null) {// ensure that filter is only applied once per requestchain.doFilter(request, response);return;}request.setAttribute(FILTER_APPLIED, Boolean.TRUE);HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request,response);// 从session获取SecurityContextSecurityContext contextBeforeChainExecution = repo.loadContext(holder);try {// 将SecurityContext存入SecurityContextHolderSecurityContextHolder.setContext(contextBeforeChainExecution);chain.doFilter(holder.getRequest(), holder.getResponse());}finally {SecurityContext contextAfterChainExecution = SecurityContextHolder.getContext();// Crucial removal of SecurityContextHolder contents - do this before anything// else.SecurityContextHolder.clearContext();// 存储SecurityContextrepo.saveContext(contextAfterChainExecution, holder.getRequest(),holder.getResponse());request.removeAttribute(FILTER_APPLIED);}
}

loadContext获取SecurityContext

使用HttpSessionSecurityContextRepository

public SecurityContext loadContext(HttpRequestResponseHolder requestResponseHolder) {HttpServletRequest request = requestResponseHolder.getRequest();HttpServletResponse response = requestResponseHolder.getResponse();HttpSession httpSession = request.getSession(false);// 从session中获取SecurityContextSecurityContext context = readSecurityContextFromSession(httpSession);if (context == null) {context = generateNewContext();}SaveToSessionResponseWrapper wrappedResponse = new SaveToSessionResponseWrapper(response, request, httpSession != null, context);requestResponseHolder.setResponse(wrappedResponse);return context;
}

从session中获取SecurityContext

private SecurityContext readSecurityContextFromSession(HttpSession httpSession) {final boolean debug = logger.isDebugEnabled();if (httpSession == null) {return null;}// Session exists, so try to obtain a context from it.Object contextFromSession = httpSession.getAttribute(springSecurityContextKey);if (contextFromSession == null) {return null;}// Everything OK. The only non-null return from this method.return (SecurityContext) contextFromSession;
}

存储SecurityContext

public void saveContext(SecurityContext context, HttpServletRequest request,HttpServletResponse response) {SaveContextOnUpdateOrErrorResponseWrapper responseWrapper = WebUtils.getNativeResponse(response,SaveContextOnUpdateOrErrorResponseWrapper.class);if (!responseWrapper.isContextSaved()) {responseWrapper.saveContext(context);}
}
protected void saveContext(SecurityContext context) {final Authentication authentication = context.getAuthentication();HttpSession httpSession = request.getSession(false);// See SEC-776if (authentication == null || trustResolver.isAnonymous(authentication)) {if (httpSession != null && authBeforeExecution != null) {// SEC-1587 A non-anonymous context may still be in the session// SEC-1735 remove if the contextBeforeExecution was not anonymoushttpSession.removeAttribute(springSecurityContextKey);}return;}if (httpSession == null) {httpSession = createNewSessionIfAllowed(context);}// If HttpSession exists, store current SecurityContext but only if it has// actually changed in this thread (see SEC-37, SEC-1307, SEC-1528)if (httpSession != null) {// We may have a new session, so check also whether the context attribute// is set SEC-1561if (contextChanged(context)|| httpSession.getAttribute(springSecurityContextKey) == null) {// 存到session中httpSession.setAttribute(springSecurityContextKey, context);}}
}

https://zhhll.icu/2024/框架/springSecurity/7.web如何取得用户信息/

http://www.ds6.com.cn/news/25410.html

相关文章:

  • 政府网站建设问题与对策产品网络推广
  • 温州编程网站网络营销策略分析论文
  • 农村小学校园网站建设方案简单的html网页制作
  • 宝鸡市网站建设电商平台网站
  • 如何建设网站方便后期维护千锋教育官方网
  • 服务器架设国外做违法网站国内最新消息新闻
  • 台州做网站多少钱四川餐饮培训学校排名
  • 动画网站模板手机建立一个免费网站
  • 中国建设银行个人网上银行官网五年级下册数学优化设计答案
  • 上海装饰公司10排名天津seo外包平台
  • 建设网站公司浩森宇特抖音代运营
  • 国内最最早做虚拟货币的网站咖啡seo是什么意思
  • 网站建设哪家域名ip查询查网址
  • 长春网络建站浏览广告赚佣金的app
  • 小说网站快速做排名如何申请一个网站域名
  • seo实训总结aso优化渠道
  • 四海网络网站建设咨询深圳全网推广排名
  • 鞍山网站制作公司临安网站seo
  • 金坛做网站哪家好企业推广文案范文
  • 广州城市建设档案馆网站免费的行情软件app网站
  • 网站建设资料准备标准上海关键词优化报价
  • 专门做五金的网站网络营销制度课完整版
  • wordpress移动端音乐插件seo如何建立优化网站
  • 西峰网东莞seo网站制作报价
  • 深圳做网站便宜seo常用工具网站
  • wordpress站名在哪改竞价 推广
  • 跨境贸易电子商务服务平台网站怎么优化关键词
  • php网站建设方案网络舆情处理公司
  • aspx php哪个做门户网站好各种资源都有的搜索引擎
  • 网站建设的论坛网站流量监控