httpClient携带token访问项目一直提示403拒绝访问,postman却是可以的

2个独立的springBoot项目突然有了关联,要A向B同步数据,但是使用httpClient工具访问一直是403拒绝访问,开始时候以为token拼接到路径里就可以呢,结果当然不行,然后放到请求头里,httpPost.addHeader("token", token); 结果还是403,最后找到后台token校验的代码,发现了原因

    public static String resolveToken(HttpServletRequest request) {
        String bearerToken = request.getHeader("Authorization");
        if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) {
            return bearerToken.substring(7);
        } else {
            Cookie[] cookies = request.getCookies();
            return cookies == null ? null : (String)Arrays.stream(cookies).filter((cookie) -> {
                return Objects.equals(cookie.getName(), "access-token");
            }).findFirst().map(Cookie::getValue).orElse((Object)null);
        }
    }

代码里对token进行了2种判断,第一个是判断是否postman等工具发送的请求,postman请求会默认在token拼接"Bearer "

Bearer eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiI0MGZjZGRlMDVhNWU0YjExYTQ1OTE4ODJmYzU5OWYzNiIsInN1YiI6ImFkbWluIiwiaWQiOiIxMTIzNTk4ODIxNzM4Njc1MjAxIiwicm9sZXMiOiJzdXBlcmFkbWluIiwiZ3JvdXBzIjoic3VwZXJhZG1pbiIsImV4cCI6MTY0MDc2MDAyNH0.BhotiZOY5fz_AVXdQZ27e_KKXzmtpL4rCtTDuBwbX8qu1O-MgDPGc-kYslzKyDAc9kas7VNx7iED8k4MBZpRAQ

第二种是浏览器等请求从cookie里取出token,然后在对token进行校验

posted @ 2021-12-29 14:45  wujingqi  阅读(2350)  评论(0编辑  收藏  举报