SecurityContextHolder, SecurityContext和Authentication

原创地址:https://blog.csdn.net/ro_wsy/article/details/44341547 

SecurityContextHolder, SecurityContext和Authentication

SecurityContextHolder是SpringSecurity最基本的组件了,是用来存放SecurityContext的对象,默认是使用ThreadLocal实现的,这样就保证了本线程内所有的方法都可以获得SecurityContext对象。

SecurityContextHolder还有其他两种模式,分别为SecurityContextHolder.MODE_GLOBAL和SecurityContextHolder.MODE_INHERITABLETHREADLOCAL,前者表示SecurityContextHolder对象的全局的,应用中所有线程都可以访问;后者用于线程有父子关系的情境中,线程希望自己的子线程和自己有相同的安全性。

大部分情况下我们不需要修改默认的配置,ThreadLocal是最常用也是最合适大部分应用的。

获得认证主体信息

我们可以用下面的代码段获得认证的主体信息

[java]  view plain  copy
  1. Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();  
  2.   
  3. if (principal instanceof UserDetails) {  
  4.   String username = ((UserDetails)principal).getUsername();  
  5. else {  
  6.   String username = principal.toString();  
  7. }  
第一行代码返回的是一个UserDetails类型的实例,其中包含了username,password和权限等信息,当然,我们也可以通过实现这个接口自定义我们自己的UserDetails实例,给我们自己的应用使用,以符合需要的业务逻辑。

UserDetailsService

上面说到可以自定义UserDetails实例,我们怎么获得这个实例呢,就需要通过UserDetailsService来实现,这个接口只有一个方法

[java]  view plain  copy
  1. UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;  
这个方法接受一个用户名参数,返回UserDetails实例。

认证成功之后,UserDetails对象用来构建Authentication对象并存放在SecurityContextHolder中,所以,我们需要的用户信息都可以通过SecurityContextHolder拿到。

GrantedAuthority

Authentication对象还提供了getAuthorities方法,获取用户被赋予的权限,权限通常对应着角色,什么角色对应着什么样的访问权限,例如ADMIN_ROLE可以访问/admin下的内容,其他角色则无权访问。

GrantedAuthority对象也通常由UserDetailsService实例获取。

总结

我们上面提到了如下几个对象:

SecurityContextHolder:提供对SecurityContext的访问
SecurityContext,:持有Authentication对象和其他可能需要的信息
Authentication:Spring Security方式的认证主体
GrantedAuthority:对认证主题的应用层面的授权
UserDetails:构建Authentication对象必须的信息,可以自定义,可能需要访问DB得到
UserDetailsService:通过username构建UserDetails对象

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值