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