java获取postman的basic auth

2023-06-10 08:09:05 浏览数 (1)

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

在java拦截器中进行解密:

代码语言: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;
        }
        Base64.Decoder decoder = Base64.getDecoder();
        try {
            byte[] b = decoder.decode(s);
            return new String(b);
        } catch (Exception e) {
            return null;
        }
    }

0 人点赞