zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java获取postman的basic auth

JAVApostman 获取 auth Basic
2023-06-13 09:17:10 时间

postman的basic auth 实际上是把用户名和密码,加密后进行了传输:

在java拦截器中进行解密:

    String auth = request.getHeader("Authorization");
    if ((auth != null) && (auth.length() > 6)) {
        auth = auth.substring(6, auth.length());

        String decodedAuth = getFromBASE64(auth);
        System.out.println("auth decoded from base64 is " + decodedAuth);//admin:admin

        //使用admin和admin进行自登录,登录成功就返回true
        return true;
    }
    
    
    
	private String getFromBASE64(String s) {
		if (s == null)
			return null;
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			byte[] b = decoder.decodeBuffer(s);
			return new String(b);
		} catch (Exception e) {
			return null;
		}
	}