spring security把用户的信息存在哪,spring security用户登录

  spring security把用户的信息存在哪,spring security用户登录

  

目录

在豆中获取用户信息在控制器中获取用户信息通过连接获取用户信息在JSP页面中获取用户信息

 

  

在Bean中获取用户信息

authentic ation authentic ation=security context holder。获取上下文().getAuthentication();如果(!(AnonymousAuthenticationToken的authentic ation实例)){ String current username=authentic ation。getname();返回当前用户名;}春季安全框架提供了多种认证令牌的派生类,根据自己的应用场景,可以对SecurityContextHolder里面的认证令牌进行类型转换,如下:

 

  usernamepasswordtuthenticationtoken authenticationToken=(usernamepasswordtuthenticationtoken)security context holder。获取上下文().getAuthentication();//详细信息里面存放了当前登录用户的详细信息用户用户详细信息=(用户)authenticationtoken。获取详细信息();PS .认证令牌的类型转换同样适用于下面提到的校长类。

  

在Controller中获取用户信息

通过校长参数获取:导入Java。安全。本金;导入org。spring框架。刻板印象。控制器;导入org。spring框架。网络。绑定。注释。请求映射;导入org。spring框架。网络。绑定。注释。请求方法;导入org。spring框架。网络。绑定。注释。响应正文;@ RestControllerpublic类安全控制器{ @ get mapping(value=/username )公共字符串当前用户名(委托人委托人){返回委托人。getname();}}通过证明参数获取:导入org。spring框架。安全。核心。真实性;导入org。spring框架。刻板印象。控制器;导入org。spring框架。网络。绑定。注释。请求映射;导入org。spring框架。网络。绑定。注释。请求方法;导入org。spring框架。网络。绑定。注释。响应正文;@ RestControllerpublic类安全控制器{ @ get mapping(/username )@响应正文公共字符串当前用户名(authentic ation authentic ation){返回authentic ation。getname();}}通过对象获取导入Java。安全。本金;导入javax。servlet。http。http servlet请求;导入org。spring框架。刻板印象。控制器;导入org。spring框架。网络。绑定。注释。请求映射;导入org。spring框架。网络。绑定。注释。请求方法;导入org。spring框架。网络。绑定。注释。响应正文;@ RestControllerpublic类安全控制器{ @ get mapping(/username )公共字符串currentUserNameSimple(http servlet request request){ Principal Principal=request。获取用户主体();归还本金。getname();}}

 

  

通过 Interface 获取用户信息

注意:IAuthenticationFacade 这个接口在springboot2.1.0中已不存在了

 

  通过连接获取其实和第一种在豆中获取用户信息是一样的,都是访问SecurityContextHolder获取的,只是进行了封装。

  公共接口IAuthenticationFacade { Authentication getAuthentication();} @ Componentpublic公共类认证外观实现IAuthenticationFacade { @ Override public Authentication getAuthentication(){ return security context holder。获取上下文().getAuthentication();}}下面是使用方法:

  @ RestControllerpublic类安全控制器{ @ auto wired private IAuthenticationFacade authenticationFacade;@GetMapping(/username )公共字符串currentUserNameSimple(){ authentic ation authentic ation=认证外观。getauthentication();返回正宗的。getname();}}

  

在JSP页面中获取用户信息

要使用春天安全的标签特性,首先要在JSP页面引入安全性的标签:

 

  % @ taglib prefix= security uri= http://www。spring框架。组织/安全性/标签 %通过以下方式可以获取到当前登录用户:

  security:authorize access= is authenticated() authenticated as security : authentic ation property= principal . username //这就是security : authorize。本文介绍了如何在Spring Securit中获取当前登录用户的详细信息。有关如何在Spring SecurityPlease中获取登录用户信息的更多信息,请搜索Popular IT以前的文章或继续浏览下面的相关文章。我希望你以后能更多地支持流行音乐!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: