详解Spring Security中获取当前登录用户的详细信息的几种方法

 更新时间:2022年05月09日 10:08:25   作者:R先生专栏  
本文主要介绍了详解Spring Security中获取当前登录用户的详细信息的几种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

在Bean中获取用户信息

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
    String currentUserName = authentication.getName();
    return currentUserName;
}

Spring Security 框架提供了多种 AuthenticationToken 的派生类,根据自己的应用场景,可以对 SecurityContextHolder 里面的 AuthenticationToken 进行类型转换,如下:

UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
// details里面存放了当前登录用户的详细信息
User userDetails = (User) authenticationToken.getDetails();

PS. AuthenticationToken的类型转换同样适用于下面提到的Principal类。

在Controller中获取用户信息

  • 通过Principal参数获取:
import java.security.Principal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
 
@RestController
public class SecurityController {
 
    @GetMapping(value = "/username")
    public String currentUserName(Principal principal) {
        return principal.getName();
    }
}
  • 通过Authentication参数获取:
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
 
@RestController
public class SecurityController {
 
    @GetMapping("/username")
    @ResponseBody
    public String currentUserName(Authentication authentication) {
        return authentication.getName();
    }
}
  • 通过HttpServletRequest获取
import java.security.Principal;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
 
@RestController
public class SecurityController {
 
    @GetMapping("/username")
    public String currentUserNameSimple(HttpServletRequest request) {
        Principal principal = request.getUserPrincipal();
        return principal.getName();
    }
}

通过 Interface 获取用户信息

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

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

public interface IAuthenticationFacade {
    Authentication getAuthentication();
}
@Component
public class AuthenticationFacade implements IAuthenticationFacade {
 
    @Override
    public Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }
}

下面是使用方法:

@RestController
public class SecurityController {
    @Autowired
    private IAuthenticationFacade authenticationFacade;
 
    @GetMapping("/username")
    public String currentUserNameSimple() {
        Authentication authentication = authenticationFacade.getAuthentication();
        return authentication.getName();
    }
}

在JSP页面中获取用户信息

要使用 Spring Security 的标签特性,首先要在 JSP 页面引入 Security 的 tag:

<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>

通过以下方式可以获取到当前登录用户:

<security:authorize access="isAuthenticated()">
    authenticated as <security:authentication property="principal.username" /> 
</security:authorize>

到此这篇关于详解Spring Security中获取当前登录用户的详细信息的几种方法的文章就介绍到这了,更多相关Spring Securit获取登录用户信息内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 巧妙mybatis避免Where 空条件的尴尬

    巧妙mybatis避免Where 空条件的尴尬

    这篇文章主要介绍了巧妙mybatis避免Where 空条件的尴尬,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • Spring Boot Mysql 数据库操作示例

    Spring Boot Mysql 数据库操作示例

    本篇文章主要介绍了Spring Boot Mysql 数据库操作示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • java——多线程基础

    java——多线程基础

    Java多线程实现方式有两种,第一种是继承Thread类,第二种是实现Runnable接口,两种有很多差异,下面跟着本文一起学习吧,希望能给你带来帮助
    2021-07-07
  • IDEA中设置Tab健为4个空格的方法

    IDEA中设置Tab健为4个空格的方法

    这篇文章给大家介绍了代码缩进用空格还是Tab?(IDEA中设置Tab健为4个空格)的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2021-03-03
  • Java利用三目运算符比较三个数字的大小

    Java利用三目运算符比较三个数字的大小

    今天小编就为大家分享一篇关于Java利用三目运算符比较三个数字的大小,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Mybatis-Spring连接mysql 8.0配置步骤出错的解决方法

    Mybatis-Spring连接mysql 8.0配置步骤出错的解决方法

    这篇文章主要为大家详细介绍了Mybatis-Spring连接mysql 8.0配置步骤出错的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • Java 详解如何从尾到头打印链表

    Java 详解如何从尾到头打印链表

    在我们平时的代码过程中,链表是我们经常遇到的一个数据结构,它非常的简单,但Java并不能直接将一个链表打印出来,通过这篇文章我们来讲解一下这个问题
    2022-01-01
  • Java使用观察者模式实现气象局高温预警功能示例

    Java使用观察者模式实现气象局高温预警功能示例

    这篇文章主要介绍了Java使用观察者模式实现气象局高温预警功能,结合完整实例形式分析了java观察者模式实现气象局高温预警的相关接口定义、使用、功能操作技巧,并总结了其设计原则与适用场合,具有一定参考借鉴价值,需要的朋友可以参考下
    2018-04-04
  • 利用Java编写一个Java虚拟机

    利用Java编写一个Java虚拟机

    这篇文章主要为大家详细介绍了如何使用 Java17 编写的 Java 虚拟机,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的可以了解下
    2023-07-07
  • 详解Java二叉排序树

    详解Java二叉排序树

    这篇文章主要介绍了Java二叉排序树,包括二叉排序树的定义、二叉排序树的性质、二叉排序树的插入和查找等,感兴趣的小伙伴们可以参考一下
    2015-12-12

最新评论