悠然哈哈哈

导航

spring clound @FeignClient @RequestHeader 设置token cookie

 

  

public Map<String, String> populateHeaders() {
        Map<String, String> headers = new HashMap();
        String cookie = this.getCookie();
        if (StringUtils.isNoneBlank(new CharSequence[]{cookie})) {
            headers.put("Cookie", cookie);
        } else{
    
    
  
// 获取cookie
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = Objects.requireNonNull(sra).getRequest();
Cookie[] cookies = request.getCookies();
StringBuilder cookieBuilder = new StringBuilder();
for (Cookie cookie : cookies) {
cookieBuilder.append(cookie.getName()).append("=").append(cookie.getValue()).append(";");
}
String cookie = cookieBuilder.substring(0,cookieBuilder.length()-1);
 
 headers.put("Cookie", cookie);

    
    } String accessToken
= this.getAccessToken(); if (StringUtils.isNoneBlank(new CharSequence[]{accessToken})) { headers.put("accessToken", accessToken); } return headers; }

 Map<String, String> headers = new HashMap();
headers.put("Cookie", cookie);
headers.put("accessToken", accessToken);
object bopFeign.getWarehouse(wRequest, headers);
 
@FeignClient(value = "test-bop-service")
public interface TestBopFeign {


@GetMapping(value = "/getPageWarehouse") object getWarehouse(@SpringQueryMap WarRequest wRequest,@RequestHeader Map<String, String> headers);
}
 

 

 

  

 

  

posted on 2024-07-10 18:59  悠然886  阅读(27)  评论(0编辑  收藏  举报