-1
我在前端使用了angularjs,並試圖訪問在我的項目後端使用springboot實現的遠程服務器(tomcat)。如何使用springboot允許訪問控制允許來源
但是我面對這個錯誤:
迴應預檢要求未通過訪問控制檢查:沒有「訪問控制允許來源」標頭出現在所請求的資源。因此'Origin'http://localhost:8080'不允許訪問。該響應具有HTTP狀態碼403.
我試圖解決這個問題,仍然具有相同的issu。
這裏的前端的代碼:
變種服務= angular.module( 'TestService的',[]);
services.factory( 'TestService的',[ '$ HTTP', '$ Q',函數($ HTTP,$ Q){
var factory = {
add: add
};
return factory;
function add(body) {
var deferred = $q.defer();
var headers = {
'Access-Control-Allow-Origin':'*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, PUT',
'Content-Type': 'application/json',
'Accept': 'application/json'
};
$http({
headers: headers,
method: 'POST',
url: 'http://localhost:8081/test/add',
dataType: 'json',
data: body
})
.then(function(response) {
deferred.resolve(response.data);
console.log('Service works');
console.log(response.data);
}, function(errResponse) {
console.log("erreur");
deferred.reject(errResponse);
});
return deferred.promise;
}
}]);
這是我的後端代碼:
import java.io.IOException;
import org.springframework.web.bind.annotation.CrossOrigin;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import fr.edf.sdin.mrs.recherche.model.entities.ResultEntity;
import fr.edf.sdin.mrs.recherche.service.TestService;
@RestController
@RequestMapping("/test")
@CrossOrigin(origins="*",maxAge=3600, methods={RequestMethod.GET,RequestMethod.POST,RequestMethod.PUT,RequestMethod.OPTIONS,RequestMethod.DELETE}, allowedHeaders={"x-requested-with", "accept", "authorization", "content-type"},
exposedHeaders={"access-control-allow-headers", "access-control-allow-methods", "access-control-allow-origin", "access-control-max-age", "X-Frame-Options"},allowCredentials="false",value="/test")
public class TestController{
@Autowired
TestService service;
@PostMapping(value = "/add", produces = "application/json")
public ResponseEntity<ResultEntity<String>> add (@RequestBody String body, HttpServletResponse response) throws IOException {
//LOGGER.info("Entree dans le controller back");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
System.out.println(body);
String result = service.add(body);
System.out.println(result);
if (result.equals("")) {
new ResponseEntity<String>("KO", HttpStatus.OK);
}
return new ResponseEntity<ResultEntity<String>>(new ResultEntity<String>(result), HttpStatus.OK);
}
@GetMapping(value = "/add", produces = "application/json")
public ResponseEntity<?> delete (@RequestBody String body, HttpServletResponse response) throws IOException {
//LOGGER.info("Entree dans le controller back");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
System.out.println(body);
String result = service.add(body);
if (result.equals("")) {
new ResponseEntity<String>("KO", HttpStatus.OK);
}
return new ResponseEntity<String>(result, HttpStatus.OK);
}
}
謝謝你的幫助。
我認爲這是從正在添加的服務器出了問題,但仍然無法解決它。 – OussBou