我正在尝试为我的spring boot项目设置基本的Auth。到目前为止,我已经做到了:
com.app.apitool.RESTConsumer 软件包中的CallAPI.java
public class CallAPI {
RestTemplate restTemplate = new RestTemplate();
@Autowired
RestTemplateBuilder restTemplateBuilderObj;
@Autowired
BasicAuthSetup basicAuthObj;
@Autowired
RestOperations rest;
String endpointURL = "";
public String pingAPi() {
HttpEntity<String> request = new HttpEntity<String>(basicAuthObj.getHeaders());
ResponseEntity<String> response = rest.exchange(endpointURL, HttpMethod.GET, request, String.class);
System.out.println(response.getBody());
return response.getBody();
}
}
com.app.apitool.HTTPAuthSetup 软件包中的BasicAuthSetup.java
@Component
public class BasicAuthSetup {
private String plainCreds = "id@account1:password";
private String base64Creds = Base64.getEncoder().encodeToString(plainCreds.getBytes());
@Bean
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic" + base64Creds);
System.out.println("Auth created: "+headers);
return headers;
}
}
运行时,出现以下异常:
申请无法开始
说明:
com.appd.apitool.RESTConsumer.CallAPI中的字段休息需要一个Bean 键入“ org.springframework.web.client.RestOperations”,该值不能为 找到。
操作:
考虑定义一个类型的bean 配置中的“ org.springframework.web.client.RestOperations”。
我搜索了此错误,遇到了类似的问题({如果不是相同的话)here,我正在跟踪提到的解决方法here。
我之所以决定创建新刊物,是因为在第一个链接的文章中没有看到任何解决方案。另外,我是spring-boot框架的新手。
com.app.apitool 软件包中的主要 App.java :
@SpringBootApplication
@RestController
public class App {
private static final Logger log = org.slf4j.LoggerFactory.getLogger(App.class);
@Autowired
private CallAPI callAPIobj;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@RequestMapping("/api")
public String testAPI() {
return callAPIobj.pingAPi();
}
}
答案 0 :(得分:1)
好的,因此在您的Maven项目中,基本的auth设置将是您进行bean配置的好地方:
BasicAuthSetup.java:
package com.app.apitool.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.util.Base64;
@Configuration
public class BasicAuthSetup {
private static final String plainCreds = "id@account1:password";
private static final String base64Creds = Base64.getEncoder().encodeToString(plainCreds.getBytes());
@Bean
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic" + base64Creds);
return headers;
}
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(new ObjectMapper());
restTemplate.getMessageConverters().add(converter);
return restTemplate;
}
@Bean
public HttpEntity<String> stringHttpEntity() {
return new HttpEntity<>(getHeaders());
}
}
然后,在/ src / main / java下的适当包中创建一个测试类,如下所示:
CallAPITest.java:
package com.app.apitool;
import com.app.apitool.config.BasicAuthSetup;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestOperations;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = BasicAuthSetup.class)
public class CallAPITest {
private static final String endpointURL = "";
@Autowired
private RestOperations rest;
@Autowired
private HttpEntity<String> stringHttpEntity;
@Test
public void pingAPi() {
ResponseEntity<String> response = rest.exchange(endpointURL, HttpMethod.GET, stringHttpEntity, String.class);
System.out.println(response.getBody());
final String responseBody = response.getBody();
assertNotNull(responseBody);
}
}
以下是您在Maven pom.xml中需要的依赖项(根据您的喜好更改版本):
<properties>
<jackson.version>2.9.6</jackson.version>
<spring.version>4.3.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>