Java 使用基于表单的身份验证登录后如何获取用户名

Java 使用基于表单的身份验证登录后如何获取用户名,java,security,weblogic,Java,Security,Weblogic,我按照链接执行基于表单的身份验证。我创建了一个安全域,然后成功地将身份验证检查委托给weblogic 10.3。 一切正常,但我无法获取用户名,HttpServletRequest->getRemoteUser()返回null 您知道如何在登录后获取用户名吗?我将在每个托管bean中使用用户名来记录用户操作 编辑: 我发现我的错误是在记录用户操作(注销操作)之前使会话无效,这就是HttpServletRequest->getRemoteUser()返回null的原因。感谢您的贡献。尝试从请求中获

我按照链接执行基于表单的身份验证。我创建了一个安全域,然后成功地将身份验证检查委托给weblogic 10.3。 一切正常,但我无法获取用户名,HttpServletRequest->getRemoteUser()返回null

您知道如何在登录后获取用户名吗?我将在每个托管bean中使用用户名来记录用户操作

编辑:
我发现我的错误是在记录用户操作(注销操作)之前使会话无效,这就是HttpServletRequest->getRemoteUser()返回null的原因。感谢您的贡献。

尝试从请求中获取用户主体,然后从请求中获取名称。像这样:

Principal p = request.getUserPrincipal();
String username = p.getName();

尝试从请求中获取用户主体,然后从中获取名称。像这样:

Principal p = request.getUserPrincipal();
String username = p.getName();

j_username
Principe
中可用,因此只需检查请求对象中是否有
Principle

String username;
Principal principal = request.getUserPrincipal();
            if (principal != null) {
                username= principal.getName(); // Find User by j_username.
            }

j_username
Principe
中可用,因此只需检查请求对象中是否有
Principle

String username;
Principal principal = request.getUserPrincipal();
            if (principal != null) {
                username= principal.getName(); // Find User by j_username.
            }

您也可以从SecurityContext获得它:

SecurityContextHolder.getContext().getAuthentication().getPrincipal().getUserName();

您也可以从SecurityContext获得它:

SecurityContextHolder.getContext().getAuthentication().getPrincipal().getUserName();

我发现我的错误是在记录用户操作(注销操作)之前使会话无效,这就是HttpServletRequest->getRemoteUser()返回null的原因。
感谢您的贡献。

我发现了一个错误,即在记录用户操作(注销操作)之前使会话无效,这就是HttpServletRequest->getRemoteUser()返回null的原因。
感谢您的贡献。

那么配置有问题或者身份验证不成功。我不这么认为,因为当我键入错误的用户名/密码时,我看不到页面。配置有问题或者身份验证不成功。我不这么认为,因为当我键入错误的用户名/密码时,我看不到Spring安全核心库中包含哪个API SecurityContextHolder类。下面是路径:org.springframework.security.core.context.SecurityContextHolder;其中API SecurityContextHolder类Consist位于Spring安全核心库中。下面是路径:org.springframework.security.core.context.SecurityContextHolder;