【发布时间】:2021-10-07 11:27:24
【问题描述】:
我在 Ububtu 服务器(在 Azure 中)的 Spring Boot(版本 2.1.6.RELEASE)中有一个 RestController。
Ubuntu 信息:
说明:Ubuntu 18.04.5 LTS, 发布:18.04, 代号:仿生
我的 RestController 有一个具有此定义的 GET 方法:
@RequestMapping(value = "/universidades_por_prioridad", method = RequestMethod.GET)
public Collection<UniversidadEntity> obtenerTodasPorPrioridad() {
return universidadService.obtenerTodasPorPrioridad();
}
该方法返回一个大学集合(该列表是从数据库中获取的)
我也有这个 CORS 配置
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {
private static final Logger LOG = LoggerFactory.getLogger(CorsConfig.class);
@Override
public void addCorsMappings(CorsRegistry registry) {
LOG.info("Adding CORS Mapping");
registry.addMapping("/**").allowedOrigins("http://localhost").allowedMethods("PUT", "DELETE", "POST", "GET")
.allowCredentials(false).maxAge(3600);
// here I put the public server IP
registry.addMapping("/**").allowedOrigins("http://52.XXX.XXX.XXX").allowedMethods("PUT", "DELETE", "POST", "GET")
.allowCredentials(false).maxAge(3600);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
LOG.info("Disabling CORS");
http.cors().and().csrf().disable() //
.authorizeRequests().antMatchers("/**") //
.access("hasIpAddress(\"127.0.0.1\") or hasIpAddress(\"::1\") or hasIpAddress(\"::9002\")") //
; //
}
}
我在 PHP 中有一个 Ajax,它通过 GET 调用 Rest。
我的 Ajax 调用这个 GET 如下:
jQuery.ajax({
url: 'http://localhost:9002/universidades_por_prioridad',
type: 'get',
cache: false,
async: true,
dataType: 'json',
success: function(universidades) {
console.log(universidades);
}
});
我的 Spring Boot 配置为在端口 9002 中运行。
application.properties 如下:
server.port=9002
server.address=localhost
两个模块都在同一个服务器中(都在 Azure 中的 Ubuntu 服务器中)。
如果我在服务器的命令行中执行 CURL,它会返回带有大学列表的 JSON。
curl -i http://localhost:9002/universidades_por_prioridad
HTTP/1.1 200
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 02 Aug 2021 03:04:46 GMT
[{THE JSON}]
但是从 Ajax 中我无法访问该方法。
- Chrome DevTools 显示:
GET http://localhost:9002/universidades_por_prioridad?_=123456789 net::ERR_CONNECTION_REFUSED
- Firefox DevTools 显示:
跨源请求被阻止:同源策略不允许读取位于 http://localhost:9002/universidades_por_prioridad?_=123456789 的远程资源。 (原因:CORS 请求未成功)。
我找到了很多页面来做我的配置,但在这个服务器上是不可能的。
在我的装有 Windows 10 的计算机上它可以正常工作,但在 Ubuntu(通常在 Linux 中)我无法让它工作。
其他信息:
Java 版本:1.8.0_292
PHP 版本:7.2.24
我是否还必须从我的 PHP 模块中修改 .htaccess 中的某些内容?
请告诉我我需要什么或缺少什么。 谢谢。
【问题讨论】:
标签: ajax spring-boot http ubuntu connection-refused