如何追踪剩余请求认证参数(用户名/密码) [英] How to trace the rest request authentication parameters(username/password)

查看:68
本文介绍了如何追踪剩余请求认证参数(用户名/密码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个带有 spring 安全性的 rest api.我在 nginx 日志中收到 401 错误.现在,当我通过rest客户端发送发布请求时,我想在身份验证之前拦截和跟踪用户名、密码、请求正文和URL.我可以将 URL 设为 String url = httpRequest.getRequestURL().toString();任何人都可以让我知道如何从 HttpServletRequest 获取用户名、密码和请求正文.

I have build a rest api with spring security. I'm getting the 401 errors in the nginx logs. Now I want to intercept and trace the username, password, request body and URL before the authentication when i send a post request via rest client. I'm able to get the url as String url = httpRequest.getRequestURL().toString(); Could any one please let me know how can i get the username, password and request body from HttpServletRequest.

推荐答案

下面的代码对我有用,可以从 httpRequest 获取用户名和密码,以获取从其余客户端发送的信息.

Below code worked for me to get the username and password from the httpRequest for the information sent from the rest client.

String header = httpRequest.getHeader("Authorization");
    if ((header != null) && (header.startsWith("Basic "))) {
        String base64Token = header.substring(6);
        String token = new String(Base64.decodeBase64(base64Token.getBytes()));

        String username = "";
        String password = "";
        int delim = token.indexOf(":");

        if (delim != -1) {
            username = token.substring(0, delim);
            password = token.substring(delim + 1);
        }
    }

这篇关于如何追踪剩余请求认证参数(用户名/密码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆