1:如果在jsp页面中获取可以使用spring security的标签库

在页面中引入标签

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

然后:

 
1
<div> username : <sec:authentication property="name"/></div>

即可显示当前用户。

2:如果要在程序中获得

看了网上很多写法都是在程序中写这样的代码

 
1
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();

但我在实际运用中发现获得的Authentication为null。仔细看了下源代码发现,如果想用上面的代码获得当前用户,必须在spring

security过滤器执行中执行,否则在过滤链执行完时org.springframework.security.web.context.SecurityContextPersistenceFilter类会

调用SecurityContextHolder.clearContext();而把SecurityContextHolder清空,所以会得到null。    经过spring security认证后,

security会把一个SecurityContextImpl对象存储到session中,此对象中有当前用户的各种资料

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
SecurityContextImpl securityContextImpl = (SecurityContextImpl) request
.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
// 登录名
System.out.println("Username:"
+ securityContextImpl.getAuthentication().getName());
// 登录密码,未加密的
System.out.println("Credentials:"
+ securityContextImpl.getAuthentication().getCredentials());
WebAuthenticationDetails details = (WebAuthenticationDetails) securityContextImpl
.getAuthentication().getDetails();
// 获得访问地址
System.out.println("RemoteAddress" + details.getRemoteAddress());
// 获得sessionid
System.out.println("SessionId" + details.getSessionId());
// 获得当前用户所拥有的权限
List<GrantedAuthority> authorities = (List<GrantedAuthority>) securityContextImpl
.getAuthentication().getAuthorities();
for (GrantedAuthority grantedAuthority : authorities) {
System.out.println("Authority" + grantedAuthority.getAuthority());
}

spring security中当前用户信息的更多相关文章

  1. spring security LDAP获取用户信息

    很多企业内部使用LDAP保存用户信息,这章我们来看一下如何从LDAP中获取Spring Security所需的用户信息. 首先在pom.xml中添加ldap所需的依赖. <dependency& ...

  2. 关于Spring Security 3获取用户信息的问题

    标签: spring security 3标签获取用户信息 2013-01-05 10:40 5342人阅读 评论(0) 收藏 举报  分类: Spring(25) java(70) 前端(7)    ...

  3. Spring Boot+Spring Security:获取用户信息和session并发控制

    说明 (1)JDK版本:1.8(2)Spring Boot 2.0.6(3)Spring Security 5.0.9(4)Spring Data JPA 2.0.11.RELEASE(5)hiber ...

  4. spring security 获取当前用户信息

    1.从页面上显示当前登陆的用户名 sec:authentication="name" <html xmlns:th="http://www.thymeleaf.or ...

  5. Spring security 获取当前用户

    spring security中当前用户信息 1:如果在jsp页面中获取可以使用spring security的标签库 在页面中引入标签   1 <%@ taglib prefix=" ...

  6. spring security实现记录用户登录时间等信息

    目录 spring security实现记录用户登录时间等信息 一.原理分析 二.实现方式 2.1 自定义AuthenticationSuccessHandler实现类 2.2 在spring-sec ...

  7. spring security使用数据库管理用户权限

    <authentication-provider> <user-service> <user name="admin" password=" ...

  8. [收藏]Spring Security中的ACL

    ACL即访问控制列表(Access Controller List),它是用来做细粒度权限控制所用的一种权限模型.对ACL最简单的描述就是两个业务员,每个人只能查看操作自己签的合同,而不能看到对方的合 ...

  9. Spring Security 中的过滤器

    本文基于 spring-security-core-5.1.1 和 tomcat-embed-core-9.0.12. Spring Security 的本质是一个过滤器链(filter chain) ...

随机推荐

  1. IAR for MSP430 关于添加自定义头文件的两种方法【转】

    前言:第一次接触这个软件,编译一个例程一直出现没有包含头文件的错误,在网上找了好几个方法都没什么效果,看到了篇文章,利用里面的方法1解决了,特此复制下来保存学习用. 原文链接:https://blog ...

  2. 00074_Array类

    1.Array类的概述 此类包含用来操作数组(比如排序和搜索)的各种方法.需要注意,如果指定数组引用为 null,则访问此类中的方法都会抛出空指针异常NullPointerException. 2.常 ...

  3. 【Educational Codeforces Round 48 (Rated for Div. 2) C】 Vasya And The Mushrooms

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然在没有一直往右走然后走到头再往上走一格再往左走到头之前. 肯定是一直在蛇形走位.. 这个蛇形走位的答案贡献可以预处理出来.很容易 ...

  4. ZOJ 3885 The Exchange of Items

    The Exchange of Items Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...

  5. poj 2139 flord水题

    读懂题意就简单了 #include<stdio.h> #define inf 999999999 #define N 310 int f[N]; int map[N][N]; int ma ...

  6. 【Android】Eclipse自己主动编译NDK/JNI的三种方法

    [Android]Eclipse自己主动编译NDK/JNI的三种方法 SkySeraph Sep. 18th  2014 Email:skyseraph00@163.com 一.Eclipse关联cy ...

  7. Mysql存储过程包括事务,且传入sql数据运行

    有这样一个需求.要求在mysql存储过程中使用到事务,并且运行的是动态的sql语句 代码例如以下: BEGIN DECLARE in_data TEXT; /** 标记是否出错 */ DECLARE ...

  8. jQuery Video Extend

    HTML5视频扩展插件 能够加入Logo 加入标记 用法: 下载:jquery-video-extend <script src="js/jquery-2.1.4.min.js&quo ...

  9. CZLayer的阴影

    CALayer有一个shadow属性 意思是阴影 shadowcolor  //颜色 shadowoffset  //偏移 shadowOpacity  //透明度 layer有一个方法    mas ...

  10. HttpClient简单操作

    HttpClient 这个框架主要用来请求第三方服务器,然后获取到网页,得到我们需要的数据: HttpClient设置请求头消息User-Agent模拟浏览器 比如我们请求 www.tuicool.c ...