SecurityContextHolder, SecurityContext and Authentication Objects

本文主要是介绍SecurityContextHolder, SecurityContext and Authentication Objects,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

SecurityContextHolder, SecurityContext and Authentication Objects

    最重要并且最根本的object是SecurityContextHolder,我们将当前应用security上下文的所有数据保存在里面,这些数据包括应用系统中使用的principle数据。SecurityContextHolder默认使用ThreadLocal局部变量保存这些数据,这意味着security上下文对于相同的正在执行的线程的方法是可用的,即使security上下文没有显式地将参数传递给这些方法,如果希望在当前principal请求处理完毕后要清理这些线程,这样使用ThreadLocal局部变量会是非常安全的。当然,Spring Security会自动的处理这些问题,所以你不需要有任何的担心。

    一些应用不一定适合使用ThreadLocal,因为它们可能会用特定的方式运行线程。举个例子,一个Swing客户端可能希望所有在JVM中的线程使用Security上下文。你可以配置SecurityContextHolder使得它在启动时以你希望的方式来保存上下文数据。对于一个独立的应用你可以使用SecurityContextHolder.MODE_GLOBAL策略。其他应用可能希望由安全线程与其衍生出来的线程具有统一的安全标示,则可以使用SecurityContextHolder.MODE_INHERITABLETHREADLOCAL策略。你可以通过两种方式改变默认的SecurityContextHolder.MODE_THREADLOCAL模式。第一种是设置一个系统属性,第二种方式是调用SecurityContextHolder的一个静态方法。大多数应用不需要修改这个默认的策略,如果需要修改,看看JavaDocs中的SecurityContextHolder获取更多信息。

Obtaining information about the current user

    在SecurityContextHolder中我们保存了当前与应用交互的principal数据,Spring Security使用一个Authentication对象来保存和展示这些数据。你不需要自己手工创建一个Authentication对象,而且查询这个对象也相当的简单,你可以使用下面的代码(在你的应用的任意位置)获取当前认证用户的姓名信息:
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal instanceof UserDetails) {String username = ((UserDetails)principal).getUsername();
} else {String username = principal.toString();
}
    getContext()方法返回的对象是SecurityContext接口的实例,这就是保存在ThreadLocal局部变量中的对象。

参考资料来自《 Spring Security Reference Documentation(3.1)》,下载地址: http://download.csdn.net/detail/u010009900/9169039

这篇关于SecurityContextHolder, SecurityContext and Authentication Objects的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/956712

相关文章

redis实战 migrate异常NOAUTH Authentication required.

当redis cluster集群设置密码的时候,使用migirate需要加上密码,否则会报如下异常: (error) ERR Target instance replied with error: NOAUTH Authentication required. redis在3.0之前是不支持migrate带有password的,在3.0之后可以在migrate中加入auth参数来做权限校验

二 Qt Remote Objects (REPC 编译器)

REPC 概述 Replica Compiler ( repc )基于 API 定义文件生成QObject头文件。该文件(称为“rep”文件)使用特定的(文本)语法来描述 API。文件扩展名为 .rep,是 Replica 的缩写。当这些文件被repc 处理时,repc 会同时生成Source和Replica头文件。 Qt Remote Objects 模块还包括 qmake 宏(REPC_S

一、认识Qt Romote Objects(QtRo)

文章目录 概念QtRo 基本框架QtRo创建过程source端 副本Replica 概念 Qt Romote Objects模块提供了一种在进程和设备之间共享Qt API的简单方法。为了实现这一点,我们需要在进程和设备之间建立一个数据通道。要建立这个数据通道,首先需要一个QRemoteObjectNode。 在QtRO中,节点node是通信的端点。在远程对象网络中,每个参与者,

boost.asio学习笔记03——io objects

asio的文档,告诉我们在声明一个io_service对象之后,就可以创建io对象去干活了,例如: int main(int argc, char* argv[]) { boost::asio::io_service io_service; tcp::resolver resolver(io_service); tcp::resolver::query query("www.boost.o

Pandas 报错 TypeError: ‘Series‘ objects are mutable, thus they cannot be hashed

一、需求 根据原始 CSV 文件的列 A 的值,添加一列 B。 二、尝试 1 1. 将 A 列与 B 列对应的值写入字典 dict,A 列为 key,B 列为 value。 2. 将 CSV 文件处理为 DataFrame。 3.  import pandas as pd# 如果 df['A']存在于 dict_a 中,则取 value,值,否则使用默认值 15dict_a = {'

Pandas报错AttributeError: Cannot access callable attribute 'sort_values' of 'DataFrameGroupBy' objects

完整报错如下: AttributeError: Cannot access callable attribute 'sort_values' of 'DataFrameGroupBy' objects, try using the 'apply' method 报错代码如下: import pandas as pddata = pf.read_csv('test.csv',header =

Navicat12版本连接,MySQL8.0版本,连接出现 Authentication plugin 'caching_sha2_password' cannot be loaded...

1、在使用Navicat Premium 12,连接MySQL8.0版本数据库时会出现Authentication plugin 'caching_sha2_password' cannot be loaded的错误。   出现这个问题的原因是mysql8.0版本之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_passw

missing authentication credentials for REST request

1、报错截图 2、解决办法 将elasticsearch的elasticsearch.yml的 xpack.security.enabled: true 改为 xpack.security.enabled: false

DirectX Media Objects

 DirectX Media Objects Microsoft® DirectX® Media Objects (DMOs)是基于COM的数据流组件。在某些部分上,DMOS是类似于Microsoft DirectShow filters。比如对于DirectShow filters, DMOs也是输入数据到创建输出数据。而且,DMOs的APIs比相应的DirectShow的APIs要

fatal: Authentication failed could not read from remote repository

Git 无法clone、pull、fetch 异常: fatal: could not read from remote repositoryfatal: Authentication failed for 'http://git.example.com/example/master.git/' 所在公司使用Git版本管理工具,由于以前用的都是SVN,所以不是很熟悉,公司用一个Git账号,