请自行揣摩代码

package com.hudai.platform.manager.util;

import java.net.URI;
import java.net.URISyntaxException; import javax.annotation.Resource; import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import com.alibaba.fastjson.JSONObject;
import com.hudai.platform.manager.config.BaseEnum;
import com.hudai.platform.manager.model.BaseResponse; /**
* @author WanHongLei
* @version 创建时间:2019年2月19日 下午3:09:13 类说明
*/
@Component
public class RestApi {
private static final Logger logger = LoggerFactory.getLogger(RestApi.class); @Resource
private RestTemplate restTemplate; @Value("${xjd.applications.url}")
private String url; @Value("${clientCredentials}")
private String clientCredentials; public BaseResponse<JSONObject> proxy(Object obj, String path) {
URI uri;
try {
uri = new URI(url + path);
} catch (URISyntaxException e) {
logger.error("URI构建失败", e);
return new BaseResponse<>(BaseEnum.FAILED.getCode(), BaseEnum.FAILED.getName());
} String base64ClientCredentials = new String(Base64.encodeBase64(clientCredentials.getBytes())); RequestEntity<Object> requestEntity = RequestEntity.post(uri).header("Authorization", "Basic " + base64ClientCredentials).contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON).body(obj);
ResponseEntity<JSONObject> responseEntity = restTemplate.exchange(requestEntity, JSONObject.class);
JSONObject jsonObj = responseEntity.getBody();
if(responseEntity.getStatusCode() == HttpStatus.OK){
return new BaseResponse<>(jsonObj);
}else{
logger.error("请求失败,errmsg = " + jsonObj.toJSONString());
return new BaseResponse<>(jsonObj.getIntValue("error"), jsonObj.getString("msg"));
}
}
}

application.properties中添加:clientCredentials=user:password

spring rest 请求怎样添加Basic Auth请求頭的更多相关文章

  1. 精讲RestTemplate第9篇-如何通过HTTP Basic Auth认证

    本文是精讲RestTemplate第9篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...

  2. java 发送带Basic Auth认证的http post请求实例代码

    构造http header private static final String URL = "url"; private static final String APP_KEY ...

  3. java 发送带Basic Auth认证的http post请求

    构造http header private static final String URL = "url"; private static final String APP_KEY ...

  4. iOS AFNetWorking下得Basic Auth认证请求方式

    我新入职了一家公司,做了一个项目,服务器的大哥说他采用的是Basic Auth认证请求方式,一般我们用的都是OAuth的认证方式,下面我们就对比一下这两种认证方式 百度百科得到如下 Basic Aut ...

  5. 用Retrofit发送请求中添加身份验证

    用Retrofit发送请求中添加身份验证====================在安卓应用开发中, retrofit可以极大的方便发送http网络请求,不管是GET, POST, 还是PUT, DEL ...

  6. WebAuthorize(中间件对所有请求进行拦截)core只有通过添加中间件过滤请求方式 而非继承然后写特性的那种方式

    一.WebAuthorize 1.项目名称 WebAuthorize 2.加个中间件 过滤请求. using Microsoft.AspNetCore.Builder; using Microsoft ...

  7. Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求

    Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939   版权声明 ...

  8. Spring Cloud Zuul API服务网关之请求路由

    目录 一.Zuul 介绍 二.构建Spring Cloud Zuul网关 构建网关 请求路由 请求过滤 三.路由详解 一.Zuul 介绍 ​ 通过前几篇文章的介绍,我们了解了Spring Cloud ...

  9. 笔记:Spring Cloud Hystrix 异常处理、缓存和请求合并

    异常处理 在 HystrixCommand 实现的run方法中抛出异常,除了 HystrixBadRequestException之外,其他异常均会被Hystrix 认为命令执行失败并触发服务降级处理 ...

随机推荐

  1. IATHook

    IATHookClass.h #pragma once #include <Windows.h> class IATHookClass { private: DWORD oldAddr; ...

  2. 初识Java程序,编写简单代码?

    Dear All: 初识Java程序,编写简单代码? 首先小编在这里说下我们今天编写Java程序使用的是 eclipse 开发工具! 1.下载eclipse 官网地址:http://www.eclip ...

  3. k8s的configMap基本概念及案例

    pod中两种特殊类型的存储卷:secret,configMap  pod.spec.volumes.secret  pod.spec.volumes.configMap多数情况下,这两个存储卷不是给p ...

  4. 笔记1 python入门学习笔记

    目录 官方手册 菜鸟站手册地址: python的运行方法 注释 小技巧: input()接收用户输入的内容(默认为字符串) print() 运算符 is 是判断两个标识符是不是引用自一个对象 all和 ...

  5. day 85 Vue学习七之vue-cookie

      Vue学习七之vue-cookie   通过vue如何操作cookie呢 参考链接:https://www.jianshu.com/p/535b53989b39 第一步:安装vue-cookies ...

  6. Java中创建(实例化)对象的五种方式

    Java中创建(实例化)对象的五种方式1.用new语句创建对象,这是最常见的创建对象的方法. 2.通过工厂方法返回对象,如:String str = String.valueOf(23); 3.运用反 ...

  7. Girls and Boys-hdu 1068

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. 如何使用DroidPlugin——DroidPlugin初体验

    最近研究了下360的黑科技--DroidPlugin 刚开始不知道怎么用,于是看了这篇博客:http://www.jianshu.com/p/f1217cce93ef  算是引导了我,于是开始自己写写 ...

  9. WPF异步回调时回调函数如何获取异步函数产生的变量

    有这么一个问题,WPF在使用异步回调的时候,回调函数需要用到异步函数里产生的一个变量,例如异步函数里查询数据库得到了一个DataTable,如何传递给回调函数呢? [方案一]使用全局变量 很容易想到的 ...

  10. Java语言基础---逻辑运算(长路短路运算)

    长路短路运算的区别 长路与运算&:是指在两边都是整数时,是逐位与运算,在两边是关系运算时,是逻辑运算. 短路与运算&&:是指从左至右,遇到false,则停止后面的运算. 长路或 ...