当前位置: 首页 > 知识库问答 >
问题:

用户详细信息getPassword在Spring Security 3.1中返回null。如何获取当前登录用户的密码?

宰父宾实
2023-03-14

我已经使用Spring Security实现了更改密码功能,但((User细节)主体). getPassword())为登录用户返回null。

如果我没记错的话,这曾经在3.0的早期工作过。这在3.1中是否进行了更改,以便无法检索登录用户的当前密码?

在下面的代码中,我检查从网页输入的用户密码中的当前密码。然后我检查登录用户的密码是否与输入的密码匹配。如果是,那么我想设置oldPasswordMatchNewPassword=true。

如何实现此功能?

@RequestMapping(value = "/account/changePassword.do", method = RequestMethod.POST)
    public String submitChangePasswordPage(
            @RequestParam("oldpassword") String oldPassword,
            @RequestParam("password") String newPassword) {
        Object principal = SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        String username = principal.toString();
        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
            System.out.println("username: " + username);
            System.out.println("password: "
                    + ((UserDetails) principal).getPassword());
            if (((UserDetails) principal).getPassword() != null) {
                if (((UserDetails) principal).getPassword().equals(oldPassword)) {
                    oldPasswordMatchNewPassword = true;
                }
            }
        }
    if (oldPasswordMatchNewPassword == true) {
        logger.info("Old password matches new password. Password will be updated.");
        changePasswordDao.changePassword(username, newPassword);
        SecurityContextHolder.clearContext();
        return "redirect:home.do";
    } else {
        logger.info("Old password did not match new password. Password will be not be updated.");
        return null;
    }
}

我放了几个sysout()以便可以看到返回值。For ((UserDetails) principal)。getUsername()我可以看到正确登录的用户。((UserDetails) principal)。getPassword()返回空值。

如何获取((UserDetails)主体)。getPassword()是这个值吗?

提前致谢!

共有3个答案

公孙栋
2023-03-14

由于密码在用户经过身份验证后不会保留在内存中(通常是件好事),因此您需要显式重新加载它才能使用它。另一种更灵活的策略是注入AuthentiationManager的实例并直接使用它:

String name = SecurityContextHolder.getContext().getAuthentication();

try {
    authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(name, oldPassword));
    // Update password here with your dao
} catch (AuthenticationException e) {
    // Old password was wrong
}

这样你就不需要担心密码编码策略之类的事情了。请注意,您不应该以纯文本的形式存储密码。应该使用bcrypt或类似的东西对它们进行哈希处理。

公孙嘉禧
2023-03-14

是的,这在版本 3.1 中已更改。默认情况下,身份验证成功后将清除凭据。您可以在提供程序管理器上将擦除凭据后身份验证设置为 false 以防止出现这种情况。在此处查看详细信息: http://static.springsource.org/spring-security/site/docs/3.2.x/reference/core-services.html#core-services-erasing-credentials

申黎明
2023-03-14

我使用这个代码块(erase-credentials=“false”)来解决这个问题。我不知道这是否是一个优雅的解决方案,但它解决了我的问题:

<authentication-manager alias="authenticationManager" erase-credentials="false">
    <!-- authentication-provider user-service-ref="userService" -->
    <authentication-provider>
        <jdbc-user-service data-source-ref="dataSource" />
    </authentication-provider>
</authentication-manager>
 类似资料:
  • sp_get_current_user() 功能: 获取当前登录用户信息,包括users表里详细信息; 参数: 无 返回: 数组,用户包括users表里详细信息

  • 接口说明 获取当前登录用户的信息 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 API地址 GET /usercenter/api/userinfo/v1.0.0/getLoginUserInfo 是否需要登录 是 请求字段说明 参数 类型 请求类型 是否必须 说明 token string header 是 当前登录用户的TOKEN 响应字段说明 参

  • 接口说明 获取当前登录用户的信息 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 如开启https功能,请求地址的协议应改为https,如:https://www.example.com/wish3dearth/api/access/v1.0.0/getLicenseInfo API地址 GET /usercenter/api/userinfo/v1.0

  • cmf_get_current_user() 功能 获取当前登录的前台用户的信息,未登录时,返回false 参数 无 返回 array:用户信息;false表示未登录;

  • 目前,我正在使用以下内容将用户登录到我的应用程序中。然而,我想使用一个角函数来实际执行登录。为此,我想创建一个Rest网络服务来进行身份验证,但是我在SO上看到的所有示例都使用我认为被贬低的用户。我还希望该服务返回有关用户的信息。 我要问的是如何将MyUserDetailsService更改为用作登录的restful服务,或者如何创建一个可用于登录的服务,该服务将在登录后返回用户对象。 这是我的a