httpservletrequest用户普通在首次加载时为null

发布于 2025-02-14 00:13:00 字数 1311 浏览 3 评论 0原文

我使用 javax.servlet.http.httpservletrequest ;要在Java控制器中获取请求,我只想创建返回实际用户名的控制器,即IM使用方法 getuserprincipal()为了使用户在实际会话中获取用户,它在首页加载(返回null),但是当i 重新加载页面时,它可以很好地工作。

我正在使用

  • 弹簧框架5.3.9
  • Javaee-api 8.0
  • WebLogic 14.1.1
  • vuejs 3.2.37

这是我的Java控制器,

@GetMapping("/username")
    public ResponseEntity<?> getname(HttpServletRequest request) {
        Map<String, String> response = new HashMap<String, String>();

        String username = "";
        try {

            Principal p = request.getUserPrincipal();
            if (p == null || p.getName() == null || p.getName().equals("")) {

                username = "undefined";

            } else {
                username = p.getName();
            }
            response.put("username", username);

            return new ResponseEntity<Object>(
                    response, HttpStatus.OK);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            response.put("username", "");
            return new ResponseEntity<Object>(response, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

我尝试过一些棘手的东西,在用户名不确定时在Vue文件中重新加载页面首先工作。

您能帮我解决这个问题吗?

谢谢

im using javax.servlet.http.HttpServletRequest; to get the request in java controller, i just want to create controller that returns actual user name, im using method getUserPrincipal() in order to get the user in the actual session, it doesn't work at the first page load (returns null) but when i reload the page it works perfectly.

Im using

  • spring framework 5.3.9
  • javaee-api 8.0
  • weblogic 14.1.1
  • vuejs 3.2.37

Here is my java controller

@GetMapping("/username")
    public ResponseEntity<?> getname(HttpServletRequest request) {
        Map<String, String> response = new HashMap<String, String>();

        String username = "";
        try {

            Principal p = request.getUserPrincipal();
            if (p == null || p.getName() == null || p.getName().equals("")) {

                username = "undefined";

            } else {
                username = p.getName();
            }
            response.put("username", username);

            return new ResponseEntity<Object>(
                    response, HttpStatus.OK);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            response.put("username", "");
            return new ResponseEntity<Object>(response, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

I have tried something a little tricky, reload the page in my Vue file when username is undefined, it works but i need this to work at first load.

Can you help me with this issue?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青萝楚歌 2025-02-21 00:13:01

是否有可能在没有用户身份验证的情况下发送第一个请求?
这就是浏览器通常会做的,并且只有当服务器使用HTTP状态403响应时,他们才会再次尝试使用凭据。

为了在您的应用程序中处理此类大惊小怪,您可以简单地设置仅允许身份验证的用户访问的安全约束。然后,容器将自动发送403响应,您的应用程序只会看到有效的流量。

Is it possible the first request is sent without user authentication?
That's what browsers would typically do, and only when the server responds with HTTP status 403 they would try again with credentials.

To not deal with such fuss in your application you could simply set a security constraint that only allows authenticated users to access. The container will then automatically send the 403 response and your application would only see valid traffic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文