ToB企服应用市场:ToB评测及商务社交产业平台

标题: Spring Security登录用户数据获取(4) [打印本页]

作者: 渣渣兔    时间: 2022-9-5 20:56
标题: Spring Security登录用户数据获取(4)
1. 登录用户数据获取

  登录成功之后,在后续的业务逻辑中,开发者可能还需要获取登录成功的用户对象,如果不使用任何安全管理框架,那么可以将用户信息保存在HttpSession中,以后需要的时候直接从HttpSession中获取数据。在Spring Security中,用户登录信息本质上还是保存在 HttpSession中,但是为了方便使用,Spring Security对HttpSession中的用户信息进行了封装, 封装之后,开发者若再想获取用户登录数据就会有两种不同的思路:
这里列出来的两种方式是主流的做法,开发者也可以使用一些非主流的方式获取登录成功后的用户信息,例如直接从HttpSession中获取用户登录数据,
无论是哪种获取方式,都离不开一个重要的对象:Authentication。在Spring Security中, Authentication对象主要有两方面的功能:
一个Authentication对象主要包含三个方面的信息:
  Java中本身提供了 Principal接口用来描述认证主体,Principal可以代表一个公司、个人或者登录ID,Spring Security中定义了 Authentication接口用来规范登录用户信息, Authentication 继承自 Principal:
  1. public interface Authentication extends Principal, Serializable {
  2.         Collection<? extends GrantedAuthority> getAuthorities();
  3.         Object getCredentials();
  4.         Object getDetails();
  5.         Object getPrincipal();
  6.         boolean isAuthenticated();
  7.         void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
  8. }
复制代码
  这段源码中可以看到,SecurityContextHolder定义了三个静态常量用来描述三种不同的存储策略;存储策略strategy会在静态代码块中进行初始化,根据不同的strategyName初始化不同的存储策略;strategyName变量表示目前正在使用的存储策略,开发者可以通过配置系统变量或者调用setStrategyName来修改SecurityContextHolder中的存储策略,调用 setStrategyName 后会重新初始化 strategy。
  默认情况下,如果开发者试图从子线程中获取当前登录用户数据,就会获取失败,代码如下:
[code]package com.intehel.demo.controller;import org.springframework.security.core.Authentication;import org.springframework.security.core.GrantedAuthority;import org.springframework.security.core.context.SecurityContextHolder;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.util.Collection;@RestControllerpublic class UserController {    @GetMapping("/user")    public void userinfo(){        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();        String name = authentication.getName();        Collection




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4