[英]Unable to send Post Request with SpringBoot
I´m tring to make a post request, according the code below:我正在尝试根据以下代码进行发布请求:
String url = new StringBuilder().append("https://...").toString();
Map<String, Map<String, String>> body = buildBotBody(email,message);
restTemplate.postForEntity(url, body, Void.class);
This request don´t needs authentication, it don´t needs login and password.此请求不需要身份验证,不需要登录名和密码。 At the Postman I can make the request with success, but when I try to execute the code above, I got:
在邮递员我可以成功提出请求,但是当我尝试执行上面的代码时,我得到:
401 Unauthorized 401 未授权
I got simulate the error at the Postman and it gave me the message below:我在邮递员处模拟了错误,它给了我以下消息:
"message": "The request has both SAS authentication scheme and 'Basic' authorization scheme. Only one scheme should be used."
When I changed the authentication method to "No Auth" at Postman, the request worked fine.当我在 Postman 将身份验证方法更改为“无身份验证”时,请求工作正常。
I think I have to set this option "No Auth" at the code, but I don´t know how.我想我必须在代码中设置这个选项“No Auth”,但我不知道如何。
I made this way:我是这样做的:
private HttpEntity<?> builderHeadersToBuildBotBody(String email, StringBuilder message) {
Map<String, String> map = new HashMap<String, String>();
map.put("emailadress", email);
map.put("emailSubject", "Pendência para lançamento de horas do Jira");
map.put("emailBody", message.toString());
return builderHeaders(map);
}
private HttpEntity<?> builderHeaders(Map<String, String> map) {
HttpHeaders requestHeaders = new HttpHeaders();
//requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
requestHeaders.setContentType(new MediaType("application", "json"));
//requestHeaders.set(HttpHeaders.USER_AGENT, "");
return new HttpEntity<>(requestHeaders);
}
private void sendBotMessage(StringBuilder message, String nome, String email) throws Exception{
try {
String url = new StringBuilder().append("https://prod-12.westeurope.logic.azure.com:443/workflows/fbf4c29cbcad4679b1a1159fff7b07f9/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=zxm46aQnBj3ZTKPOddnnwUgtQZoQcQfixNtXVxAJjPg").toString();
HttpEntity<?> requestEntity = builderHeadersToBuildBotBody(email,message);
restTemplate.exchange(url, HttpMethod.POST, requestEntity, Void.class);
logger.info("Bot enviado com sucesso! " + nome);
} catch (Exception e) {
logger.error("Erro ao enviar Bot.", e);
throw e;
}
}
But the error continues.但错误仍在继续。
If Spring Security is on your classpath you may need to disable authentication.如果 Spring Security 在您的类路径上,您可能需要禁用身份验证。 This is done by creating a @Configuration class which extends the WebSecurityConfigurerAdaptor
这是通过创建一个扩展 WebSecurityConfigurerAdaptor 的 @Configuration 类来完成的
@Configuration
public class NoSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeRequests().anyRequest().permitAll()
.and()
.csrf().disable();
}
}
Obviously this is something you would not want to do in production but can be useful in dev environments.显然,这是您不想在生产中做但在开发环境中很有用的事情。
<?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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>br.com.oss.jira.quality</groupId>
<artifactId>jira-quality</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jira-quality</name>
<description>Gerenciador de inconsistências no Jira</description>
<properties>
<java.version>1.8</java.version>
<lombok.version>1.18.6</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- <dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Exclude the default Jackson dependency -->
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>desenv</id>
<properties>
<activatedProperties>desenv</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
The endpoint requires authorization and you should provide it with the request (credentials, token, etc.).端点需要授权,您应该向它提供请求(凭据、令牌等)。 Clean the cookies on Postman and I guess it'll stop working too.
清理 Postman 上的 cookie,我想它也会停止工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.