加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何将值添加到Spring SecurityContextHolder中

发布时间:2020-12-15 01:41:47 所属栏目:大数据 来源:网络整理
导读:我有登录参数 1.userName2.password3.companyId 我使用以下代码获得了用户名和密码 Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String name = auth.getName(); String pwd = auth.getCredentials(); String companyId=

我有登录参数

1.userName

2.password

3.companyId

我使用以下代码获得了用户名和密码

 Authentication auth = SecurityContextHolder.getContext().getAuthentication();

 String name = auth.getName();

 String pwd = auth.getCredentials();

 String companyId= ???//How can i set and then get company Id here.

我的问题是如何使用SecurityContextHolder获得额外的登录参数(companyId)?

提取类可能不是弹簧控制器.这就是我使用的原因
SecurityContextHolder而不是HttpSession.

谢谢,

最佳答案
创建简单的SpringSecurityFilter过滤器.使用setDetails方法为用户添加额外的详细信息.

package org.example;  
public class CustomDeatilsSecurityFilter extends SpringSecurityFilter {

   protected void doFilterHttp(HttpServletRequest request,HttpServletResponse response,FilterChain chain) {
      SecurityContext sec = SecurityContextHolder.getContent();
      AbstractAuthenticationToken auth = (AbstractAuthenticationToken)sec.getAuthentication();
      HashMap

像这样将它添加到Spring Security Filter Chain(这不是web.xml,但类似于applicationContext-security.xml):

然后在代码中的某处你可以做这样的事情:

Map

Spring Security的基本安装
在web.xml中

在applicationContext-security.xml中

在项目的pom.xml中

                            

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读