自己写的SpringBoot里的Controller加Auth验证

@RequestMapping("/user")
public String user(@RequestHeader(name="Authorization") String auth, @RequestBody Users items, HttpServletResponse response){
    if(!this.authRequest(auth)) {
        response.setStatus(401);
        return "";
    }
    ...
}
private Boolean authRequest(String auth){
    String authStr = Base64.decodeStr(auth.replaceAll("Basic ","").trim());
    String username = "";
    String password = "";
    if(authStr != null){
        username = authStr.split(":")[0];
        password = authStr.split(":")[1];
    }
    logger.info(authStr);
    if(auth == null){
        return false;
    }
    if(!username.equals("cmp") || !password.equals("Cmp@123")){
        return false;
    }

    return true;
}
原文地址:https://www.cnblogs.com/wpcnblog/p/15193524.html