为什么我的'Access-Control-Allow-Credentials'不再被发送以响应Spring Boot 2.0.x(在我的情况下为2.0.1.RELEASE)下的预检调用(OPTIONS)?这是我的全局CORS配置在Spring Boot 1.5.6下正常工作:
@Configuration
public class CorsConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins(
"http://localhost:3000",..)
.allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD");
}
};
}}
Run Code Online (Sandbox Code Playgroud)
我的pom依赖项(我正在做自己的安全性并避免Spring Security):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我对REST端点的服务调用未通过预检:
无法加载http:// localhost:8080/api/v5/sec/auth:对预检请求的响应未通过访问控制检查:响应中的"Access-Control-Allow-Credentials"标头的值为' '当请求的凭据模式为'include'时,必须为'true'.因此不允许来源' http:// localhost:3000 '访问.
我已经验证了在Spring Boot 1.5.6的情况下确实存在'Access-Control-Allow-Credentials'标题,并且在Spring Boot 2.0.1下缺少.
所有的文件,我可以找到,包括spring.io最新这里,说我的全局配置仍然是正确的,即使WebMvcConfigurerAdapter现在似乎被弃用.
更新:
以下是迁移前后的响应标头:
迁移之前(Spring Boot 1.5.6):
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http:// localhost:3000
Content-Type:application/json; charset = UTF-8
Date:day,dd Mon yyyy hh:mm:ss …
我试图将包含图像的byte []上传到我的Spring休息服务(在Spring Boot中运行,顺便说一下)作为MultipartFile,我的客户端运行Spring RestTemplate并且我得到了HttpClientErrorException:400 Bad Request.
我的终点:
@RequestMapping(value="/scale/{percent}", method= RequestMethod.POST)
public ResponseEntity scaleImage(@PathVariable("percent") float percent,
@RequestParam("file") MultipartFile file) {
try {
if (!file.isEmpty()) {
byte [] result = transformService.scaleImage(percent, file.getBytes());
return getResponseEntityFromBytes(result);
} else {
return generateBadRequestError();
}
} catch (Exception e) {
if (e instanceof InvalidOperationParameterException) {
// TODO - populate message with bad parameters
LOGGER.log(Level.FINE, "Invalid Parameters: ");
return generateBadRequestError();
} else {
LOGGER.log(Level.SEVERE, "Exception caught: " + e.getMessage(), e);
return generateServerError(e.getMessage());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的Spring RestTemplate客户端:
public …Run Code Online (Sandbox Code Playgroud) 我刚刚下载了 Oracle OpenJDK 17 GA LTS 版本并将其安装到 IntelliJ 中。我选择了 jdk v17 作为我的项目 SDK,并尝试将我的项目语言级别也设置为 v17,但唯一的选择是版本 1.3 至 16,以及 SDK 默认值“X - 实验功能”。语言级别版本 17 在哪里?
我正在尝试编写一个循环方法,迭代TreeMap条目以获取其键的值(我使用自定义比较器根据值对映射进行排序,结果打破了get()方法,但这不是我在这里解决的问题).到目前为止,我已经得到了以下内容,但我没有看到为什么符号'K'和'V'没有被解析 - 即使它们是在传入的TreeMap上声明的.
private V forceGet(TreeMap<K, V> sortedMap, K targetKey) {
for (Map.Entry e : sortedMap.entrySet()) {
K key = (K) e.getKey();
V value = (V) e.getValue();
if (key.equals(targetKey)) {
return value;
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
我承认自己不是泛型专家,所以如果这应该是显而易见的,请道歉.
我正在从 2.x 升级到 SpringFox Swagger 3.0.0,它引入了 Spring Boot starterspringfox-boot-starter依赖项,消除了对 2.x-based 的需求SwaggerConfig:
/**
* NO LONGER NEEDED
*/
@Configuration
@EnableSwagger2
@Profile({"local", "dev", "beta"}) // <- HOW TO DISABLE IN PROD INSTEAD OF THIS
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
现在我不再需要这个@Configuration,它允许我指定我的环境配置文件@Profile并因此在生产中禁用 Swagger,我如何在 SpringFox Swagger-UI 3.x 中禁用生产中的 Swagger?
注意:这里讨论了基于 Spring Security 的方法,这可能是某些人的选择,但由于两个原因,这不是这种情况的选择:
spring-boot-security-starter依赖项java ×3
spring-boot ×3
cors ×1
generics ×1
key-value ×1
lts ×1
resttemplate ×1
spring ×1
swagger ×1
swagger-ui ×1
treemap ×1