如何向 Spring Security 用户详细信息添加其他详细信息

2024-03-09

我想向用户详细信息添加其他信息,例如用户的 IP 地址。有什么办法可以实现这一点吗?我尝试创建一个新的 CustomSpringUser 类,但问题是如何从 Authentication 对象获取此信息。有没有其他方法来存储经过身份验证的用户的附加信息?

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

我的自定义用户类别;

public class CustomSpringUser extends org.springframework.security.core.userdetails.User {

public String ip;

public CustomSpringUser(String username, String password, Collection<? extends GrantedAuthority> authorities) {
    super(username, password, authorities);
}

public CustomSpringUser(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities, String ip) {
    super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
    this.ip= ip;
}

}

Edit:我发现我们可以为身份验证添加其他信息,但我找不到如何做到这一点。http://docs.spring.io/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/core/Authentication.html#getDetails() http://docs.spring.io/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/core/Authentication.html#getDetails()

@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
    try {

        AppUser appUser = new AppUser();
        appUser.setUsername(userName);

        AppUser domainUser = genericDao.getByTemplate(appUser).get(0);

        boolean enabled = true;
        boolean accountNonExpired = true;
        boolean credentialsNonExpired = true;
        boolean accountNonLocked = true;

        List<String> roles = new ArrayList<String>();
        roles.add(domainUser.getRole().getName());

        return new CustomSpringUser(
                domainUser.getUsername(),
                domainUser.getPassword().toLowerCase(),
                enabled,
                accountNonExpired,
                credentialsNonExpired,
                accountNonLocked,
                getGrantedAuthorities(roles),
                ***domainUser.getAccount().getIdentificationId())*** ;

    } catch (Exception e) {
        genericLogger.saveLog(Logger.Status.ERROR, "Couldn't login", e);
        throw new RuntimeException(e);
    }
}

为了得到UserDetails来自Authentication对象/实例使用getPrincipal()方法。这getDetails()方法用于获取有关用户的附加信息(通常是WebAuthenticationDetails).

Links

  1. 验证javadoc http://docs.spring.io/spring-security/site/docs/3.1.4.RELEASE/apidocs/org/springframework/security/core/Authentication.html
  2. 身份验证.getDetails()javadoc http://docs.spring.io/spring-security/site/docs/3.1.4.RELEASE/apidocs/org/springframework/security/core/Authentication.html#getDetails%28%29
  3. Authentication.getPrincipal()javadoc http://docs.spring.io/spring-security/site/docs/3.1.4.RELEASE/apidocs/org/springframework/security/core/Authentication.html#getPrincipal%28%29
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何向 Spring Security 用户详细信息添加其他详细信息 的相关文章

随机推荐

  • 捆绑包格式无法识别、无效或不合适

    代码 8 1 语言 Objective C 我可以在 iPhone 上运行该项目 但无法在模拟器上运行 I tried 1 删除DerivedData文件夹的所有数据 2 清理项目并重新启动项目 3 删除资源文件夹 但这没有用 对于那些无法
  • PHP 中的正则表达式重复捕获组

    我正在尝试从一个带有路线的文件中获取信息 因此对于这项工作 我选择了正则表达式 但我遇到了重复信息的问题 为了做一个更好的问题 我将把我拥有的内容和我想要的内容放在一起 所以我有一个文件 Codes C Connected S Static
  • 如何在 scala 中按 Ordered 参数化 Int

    我有一个具有参数化类型的类 我想对其进行比较运算符 我认为我需要使用 Ordered 特征来实现这一点 但编译器不喜欢我使用它 假设我有以下课程 class Test T lt Ordered T def someOp t T if t l
  • 仅当在构建服务器上运行时单元测试才会失败

    为了帮助单元测试 我们已经完成了DateTime委托中的类 以便DateTime Now可以在单元测试中被覆盖 public static class SystemTime region Static Fields public stati
  • 如何检测我的 Android 应用程序中的广告拦截器?

    有什么方法可以检测广告拦截器何时在应用程序中运行 我想在我的应用程序中检测用户是否使用任何应用程序来阻止应用程序中的广告 如果是 那么我想向用户显示弹出窗口 要求他在使用应用程序时禁用广告拦截器 Android 操作系统没有提供官方方法来以
  • Typescript/babel 导入导致“_1.default 不是函数”

    我正在尝试使用https github com timmywil panzoom https github com timmywil panzoom来自使用 webpack 和 babel 编译的 typescript 项目 问题是打字稿方
  • 自定义树视图

    有没有办法自定义 winform 树视图以获得类似的东西 目的是通过父项目使用一种颜色并定义一个三角形而不是 图标来开发项目 Use TreeViewDrawMode OwnerDrawText所以缩进将由TreeView 除此之外 你应该
  • ::ng-deep 将被弃用 - 有其他选择吗?

    医生说 阴影穿透后代组合器已被弃用 并且主要浏览器和工具正在删除支持 因此 我们计划放弃对 Angular 的支持 对于 deep gt gt gt 和 ng deep 的所有 3 个 在那之前 应该首选 ng deep 以获得与工具更广泛
  • 在 JBoss 上部署 Hibernate 应用程序时出错 - 找不到适用于 jdbc 的驱动程序

    我有一个 Spring Hibernate Web 应用程序 目前在 Tomcat 上运行良好 我正尝试将其部署到 JBoss 7 1 AS 应用程序全部正确启动 但如果我设置 Hibernate Persistence 设置来创建表 则会
  • 如何使用冒号解组 XML 属性?

    我正在使用的一些 SVG XML 文件的属性名称中包含破折号和冒号 例如
  • 如何使用 jasmine 测试 $window.open

    这是我的功能 scope buildForm function majorObjectId name window open FormBuilder Index scope currentAppId form majorObjectId n
  • 如何在 perl 中创建多维数组?

    我这样创建一个多维数组 usr bin perl use warnings use strict my a1 1 2 my a2 a1 3 但事实证明我仍然得到一个一维数组 Perl 中正确的方法是什么 你得到一个一维数组 因为数组 a1在
  • 让 Collections.binarySearch() 与 CompareToIgnoreCase 一起使用?

    因此 我正在一个巨大的 ArrayList 中搜索特定的 String 值 但如果我正在查找的 String 与我传递给的 String 相等 不区分大小写 我需要 Collections binarySearch 返回一个 gt 0 的值
  • 如何使用 jQuery 获取文本输入的所有值?

    我有一个表 其中有一列预先填充的文本输入 table tr td td td td tr table
  • 如何将 zip 文件上传到 azure blob,然后在那里解压缩 [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 我有很多 zip 文件 其中只有几个文件夹和 50 多个文件 如何将这些 zip 文件上传到 azure blob 然后在那里解压缩 将服务
  • mvc c# html.dropdownlist 和 viewbag

    所以我有以下 伪代码 string selectedvalud C List
  • 如何在node.js模块中实现继承?

    我正在编写 Nodejs 应用程序 它基于expressjs 我对在 Nodejs 模块中进行继承感到困惑 我想做的是创建一个模型基类 比方说 my model js module exports function my model my
  • 在 C# 中提取 .cab 文件

    我正在开发一个 C 应用程序 我需要提取一个 cab 文件 我找不到在 C 中执行此操作的库 由于许可问题 我无法使用 Microsoft Deployment Compression Cab dll I found this https
  • 联系表格 7 至 WordPress 用户数据库

    我编写了以下函数 以便在发送联系表单时将其添加到用户数据库字段中 问题是它发送了最终电子邮件 但没有向数据库输入任何内容 因此我一定在某个地方出现错误 任何帮助将不胜感激 add action wpcf7 before send mail
  • 如何向 Spring Security 用户详细信息添加其他详细信息

    我想向用户详细信息添加其他信息 例如用户的 IP 地址 有什么办法可以实现这一点吗 我尝试创建一个新的 CustomSpringUser 类 但问题是如何从 Authentication 对象获取此信息 有没有其他方法来存储经过身份验证的用