spring rest 请求怎样添加Basic Auth请求頭
请自行揣摩代码
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请求頭的更多相关文章
- 精讲RestTemplate第9篇-如何通过HTTP Basic Auth认证
本文是精讲RestTemplate第9篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...
- java 发送带Basic Auth认证的http post请求实例代码
构造http header private static final String URL = "url"; private static final String APP_KEY ...
- java 发送带Basic Auth认证的http post请求
构造http header private static final String URL = "url"; private static final String APP_KEY ...
- iOS AFNetWorking下得Basic Auth认证请求方式
我新入职了一家公司,做了一个项目,服务器的大哥说他采用的是Basic Auth认证请求方式,一般我们用的都是OAuth的认证方式,下面我们就对比一下这两种认证方式 百度百科得到如下 Basic Aut ...
- 用Retrofit发送请求中添加身份验证
用Retrofit发送请求中添加身份验证====================在安卓应用开发中, retrofit可以极大的方便发送http网络请求,不管是GET, POST, 还是PUT, DEL ...
- WebAuthorize(中间件对所有请求进行拦截)core只有通过添加中间件过滤请求方式 而非继承然后写特性的那种方式
一.WebAuthorize 1.项目名称 WebAuthorize 2.加个中间件 过滤请求. using Microsoft.AspNetCore.Builder; using Microsoft ...
- Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求
Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939 版权声明 ...
- Spring Cloud Zuul API服务网关之请求路由
目录 一.Zuul 介绍 二.构建Spring Cloud Zuul网关 构建网关 请求路由 请求过滤 三.路由详解 一.Zuul 介绍 通过前几篇文章的介绍,我们了解了Spring Cloud ...
- 笔记:Spring Cloud Hystrix 异常处理、缓存和请求合并
异常处理 在 HystrixCommand 实现的run方法中抛出异常,除了 HystrixBadRequestException之外,其他异常均会被Hystrix 认为命令执行失败并触发服务降级处理 ...
随机推荐
- strong和weak
ios中使用ARC后,内存管理使用了新的关键字:strong(强引用) 和 weak(弱引用),默认是strong引用 strong: 使用strong类型指针指向的对象,会一直保持指向,直到所有st ...
- linux网络编程之断点传输文件
以下载链接"http://www.boa.org/boa-0.94.13.tar.gz"为例: 断点续传实验大概步骤: ===================== 1,使用geth ...
- JSTree下的模糊查询算法——树结构数据层次遍历和递归分治地深入应用
A表示区域节点,S表示站点结点 问题描述:现有jstree包含左图中的所有结点信息(包含区域结点和站点结点),需要做到输入站点名称模糊查询,显示查询子树结果如右图 解决策略: 1.先模糊查询所得站点所 ...
- MySQL解决中文编码问题
转载组员博客 地址:MySQL解决中文编码问题
- pymysql模块操作数据库及连接报错解决方法
import pymysql sql = "select host,user,password from user" #想要执行的MySQL语句 #sql = 'create da ...
- keepalived原理(主从配置+haproxy)及配置文件详解
下图描述了使用keepalived+Haproxy主从配置来达到能够针对前段流量进行负载均衡到多台后端web1.web2.web3.img1.img2.但是由于haproxy会存在单点故障问题,因此使 ...
- php扩展开发-哈希表
什么是哈希表呢?哈希表在数据结构中也叫散列表.是根据键名经过hash函数计算后,映射到表中的一个位置,来直接访问记录,加快了访问速度.在理想情况下,哈希表的操作时间复杂度为O(1).数据项可以在一个与 ...
- POJ 3414 BFS 输出过程
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17456 Accepted: 7407 Special J ...
- greenplum-时间处理
工作中遇到,需要改变两周以前的数据状态,于是查了下资料,原来数据库直接就可以处理,所以分享给大家! 在PostgreSQL中可以直接对时间进行加减运算:. SELECT now()::timestam ...
- Android拨打电话不弹出系统拨号界面总结
我在网上搜了一下,解决这个问题,有两种方式: 1.反射调用系统底层方法,并获取系统权限 反射调用的代码如下: Class phoneFactoryClass = Class.forName(" ...