How to trace the rest request authentication param

2019-06-14 09:15发布

问题:

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.

回答1:

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);
        }
    }


标签: rest