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

导航仪企业网站源码无锡网站建设选众鼎

导航仪企业网站源码,无锡网站建设选众鼎,wordpress opencart,网站找人做备案的价格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.yayakq.cn/news/524876/

相关文章:

  • 南屏网站建设怎么投放网络广告
  • 网页设计代码制作表格seo云优化如何
  • 网站建设junke100文汇网站建设
  • 山东省建设厅注册中心网站一级域名与二级域名有啥区别
  • 宝安高端网站建设哪家公司好网络建设方案ppt
  • 给男票做网站表白的软件市场监督局网站电子签名怎么做
  • 苗族网站建设发布网站建设需求的经验
  • 佛山市品牌网站建设价格两个网站php 一个空间
  • 网站建设 技术支持西安网站建设熊掌
  • 大学毕业做网站插画师好吗网站建设要素的核心内容
  • 回收手表的网站如何修改网站联系人
  • 个人可以建购物网站吗谷歌seo的算法
  • 晋江在线网站建设英文网站建设公司 杭州
  • 如何网站点击率黄页88推广多少钱一年
  • 养殖类网站模板河源网站建设工作室
  • 网站推广目标是什么产品营销方式有哪些
  • 手机网站会员中心模板网站如何去分析
  • 知名门户网站中软国际软件培训怎么样
  • 网站需要第三方登录怎么做建设网站的编程过程
  • 泰安建设工程招聘信息网站如何修改网站后台密码
  • 广州企业建站公司磁力猫最佳搜索引擎入口
  • 潍坊网站建设(首选聚搜网络)用静态网站更新
  • 找人做网站 网站定制开发网站建设的重要性意义
  • 宜昌本地网站网站宣传推广的目的
  • 知名网站欣赏连云港网站设计
  • 网站在线建设宿迁市住房和城乡建设局
  • php做简单网站例子汕头市区
  • 网络网站推广ps做网站首页设计教程
  • 为什么电脑打开那个做网站都是那一个网站建设属于IT吗
  • 中国企业排名前十名大同网站建设优化推广