spring security中当前用户信息

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获取当前用户

    1.如果在jsp页面中获取可以使用spring security的标签 页面引入标签 <%@ taglib prefix="sec" uri="http://www ...

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

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

  3. Spring Security默认的用户登录表单 页面源代码

    Spring Security默认的用户登录表单 页面源代码 <html><head><title>Login Page</title></hea ...

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

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

  5. Spring Security获取已登录的用户信息的两种方法

    第一种是直接从session中手动拿: @RequestMapping(value = "/user", method = RequestMethod.GET)public Res ...

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

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

  7. spring security中当前用户信息

    1:如果在jsp页面中获取可以使用spring security的标签库 在页面中引入标签   1 <%@ taglib prefix="sec" uri="htt ...

  8. Spring Security实现禁止用户重复登陆(配置及原理)

    系统使用了Spring Security做权限管理,现在对于系统的用户,需要改动配置,实现无法多地登陆.   一.SpringMVC项目,配置如下: 首先在修改Security相关的XML,我这里是s ...

  9. Spring Security 使用数据库用户进行认证

    本文参考或摘录自:http://haohaoxuexi.iteye.com/blog/2157769 本文使用Spring Security自带的方式连接数据库对用户进行认证. 1.Spring Se ...

随机推荐

  1. SQL Server 查看数据库是否存在阻塞

    CREATE procedure [dbo].[sp_who_lock] as begin declare @spid int,@bl int, @intTransactionCountOnEntry ...

  2. fopen()函数以"a+"方式打开一个不存在的文件后读写出现问题

    问题:在完成课后习题的时候,使用fopen()函数以"a+"方式打开一个不存在的文件时,写入.读取出现错误: //添加用户输入单词后,在单词头加入编号,确保编号跟着前面的开始排序 ...

  3. javascript DOM对象(1)

    0.文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法. DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 将HTML代码分解为D ...

  4. js实现搜索框响应回车键

    1.HTML页面, 注意:不要用使用form标签. Html代码 收藏代码<input type="text" name="keyword" id=&qu ...

  5. openstack私有云布署实践【4.1 上层代理haproxy配置 (科兴环境)】

    官方文档上的高可用配置,它推荐的是使用haproxy的上层代理来实现服务组件的主备访问.或者负载均衡访问   一开始我也是使用haproxy来做的,但后来方式改了   测试环境:haproxy + n ...

  6. redis 5 种数据结构

    常用命令 就DB来说,Redis成绩已经很惊人了,且不说memcachedb和tokyocabinet之流,就说原版的memcached,速度似乎也只能达到这个级别.Redis根本是使用内存存储,持久 ...

  7. android app 集成 支付宝支付 微信支付

    项目中部分功能点需要用到支付功能,移动端主要集成支付宝支付和微信支付 支付宝sdk以及demo下载地址:https://doc.open.alipay.com/doc2/detail.htm?spm= ...

  8. SQL learning

    一.创建和删除数据库 1.创建用户 //创建用户且置密码,在MySQL中行,但在Oracle中行  ----必须在超级管理员身份下操作 create user hncu identified by ' ...

  9. git切换分支

    今天同事问我她的报错了,我看了一下,是分支没有指向远程仓库对应的分支;报错如下; $ git pull There is no tracking information for the current ...

  10. count 数字计数(bzoj 1833)

    Description 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. Input 输入文件中仅包含一行两个整数a.b,含义如上所述. Output 输出文 ...