我有应用程序,其中服务器用Java Spring(REST,oauth2)编写,前端用jquery编写。 在localhost上一切正常。今天,我使用IIS在我的PC(Windows)上创建了HTTP Web服务器。 我将Web服务器的IP设置为192.168.0.103。 然后,我可以从连接到此WiFi的其他设备加入。登录页面显示确定。 但是问题是,我无法与在与Web服务器位于192.168.0.103:8843的同一台PC上运行的spring进行通信。重要的是,我在春季使用https的自生成证书test.jks(别名test.jks)。并将此行添加到主机文件:
192.168.0.103 test.com
Mozilla返回:
CORS error - CORS request did not succeed.
Chrome返回:
OPTIONS https://url.../oauth/token net::ERR_CONNECTION_REFUSED
但是,当我尝试在不使用https的情况下运行此应用程序时(因此我更改了端口并且未使用证书),则可以顺利登录。因此,我想该证书肯定有问题,但我不知道这是什么问题...
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsConfig implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
response.setHeader("Access-Control-Max-Age","3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with, authorization, content-type, access-control-alow-origin");
if("OPTIONS".equalsIgnoreCase(request.getMethod())){
response.setStatus(HttpServletResponse.SC_OK);
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
response.setHeader("Access-Control-Max-Age","3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with, authorization, content-type, access-control-alow-origin");
} else {
chain.doFilter(req, res);
}
// chain.doFilter(new XSSRequestWrapper((HttpServletRequest) request), response);
}
@Override
public void destroy() {
}
}
function authenticateUser2(email, password) {
$.ajax({
url: "https://192.168.0.103:8443/oauth/token", // 'http://localhost:8080/oauth/token',
type: 'POST',
dataType: 'json',
headers: {"Authorization": "Basic bXktY2xpZW50Og==", "Content-Type": "application/x-www-form-urlencoded"},
data: {
"password": password,
"username": email,
"grant_type": "password"
},
success: function(result) {
localStorage.access_token = result.access_token;
localStorage.refresh_token = result.refresh_token;
window.location.replace("index.html");
},
error: function(result) {
$("#psw-error").show();
},
});
return true;
}