SpringBoot使用RestTempate基础认证
Posted liuxm-刘小明
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot使用RestTempate基础认证相关的知识,希望对你有一定的参考价值。
设置pom引用
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.itstudy</groupId> <artifactId>demo</artifactId> <version>1.0.0-SNAPSHOT</version> <name>demo</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.30</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
配置resttemplate
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; /** * RestTemplate配置类 */ @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate(ClientHttpRequestFactory factory){ return new RestTemplate(factory); } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory(){ SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setReadTimeout(5000);//单位为ms factory.setConnectTimeout(5000);//单位为ms return factory; } }
在controller中调用
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import org.apache.commons.codec.binary.Base64; @RestController public class IndexController { @Autowired private RestTemplate restTemplate; @GetMapping("basicauth") public void getBasicAuth(){ String user = "111"; String password = "66666"; String userMsg = user + ":" + password; String base64UserMsg = Base64.encodeBase64String(userMsg.getBytes()); System.out.println(); System.out.println(base64UserMsg); String url = "http://host:port/api"; String postBody = "data"; HttpHeaders headers = new HttpHeaders(); headers.add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"); headers.add("Authorization ", base64UserMsg); HttpEntity<String> entity = new HttpEntity<String>(postBody, headers); ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class); System.out.println(response.getBody()); } }
启动Springboot项目
import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication app = new SpringApplication(App.class); //关闭banner app.setBannerMode(Banner.Mode.OFF); app.run(args); } }
还有一种更简单的方式 使用 RestTemplateBuilder
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Autowired private RestTemplateBuilder restTemplateBuilder; @Bean public RestTemplate restTemplate(){ return restTemplateBuilder.basicAuthorization("admin","admin").build(); } }
RestTemplateBuilder
以上是关于SpringBoot使用RestTempate基础认证的主要内容,如果未能解决你的问题,请参考以下文章
Springboot联结万物学习笔记--Springboot微服务基础搭建篇-- SpringBoot中日志的使用
springboot:基础学习一 linux下后台启动springboot项目