我正根据下面的代码提出发帖请求:
String url = new StringBuilder().append("https://...").toString();
Map<String, Map<String, String>> body = buildBotBody(email,message);
restTemplate.postForEntity(url, body, Void.class);
此请求不需要身份验证,不需要登录名和密码。在邮递员处,我可以成功地发出请求,但是当我尝试执行上面的代码时,我得到了:
401未经授权
我在邮递员处模拟了错误,它给了我以下消息:
"message": "The request has both SAS authentication scheme and 'Basic' authorization scheme. Only one scheme should be used."
当我在邮递员处将身份验证方法更改为“无身份验证”时,请求运行正常。
我认为我必须在代码中将此选项设置为“不验证”,但我不知道如何。
我是这样的:
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;
}
}
但是错误仍然存在。
答案 0 :(得分:0)
如果Spring Security在您的类路径中,则可能需要禁用身份验证。这是通过创建一个@Configuration类来完成的,该类扩展了WebSecurityConfigurerAdaptor
@Configuration
public class NoSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeRequests().anyRequest().permitAll()
.and()
.csrf().disable();
}
}
显然,这是您不想在生产中执行的操作,但在开发环境中可能会有用。
答案 1 :(得分:0)
<?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>
答案 2 :(得分:0)
端点需要授权,您应该向其提供请求(凭证,令牌等)。清洁邮递员上的cookie,我想它也将停止工作。