-1
frontend에서 angularjs를 사용하고 있으며 내 프로젝트의 백엔드에서 springboot로 구현 된 먼 서버 (tomcat)에 접근하려고합니다. springboot에서 access-controll-allow-origin을 허용하는 방법
그러나이 오류에 직면하고있어 : 프리 플라이트 요청에응답은 액세스 제어 검사를 통과하지 않습니다 : 없음 '액세스 - 제어 - 허용 - 원산지'헤더는 요청 된 자원에 존재합니다. 따라서 원점 'http://localhost:8080'은 액세스 할 수 없습니다. 응답의 HTTP 상태 코드는 403입니다.
나는 여전히 동일한 issu가있는 문제를 해결하려고했습니다. 여기
는 프론트 엔드 코드이다 :VAR 서비스 angular.module = ('TestService'[]);
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);
}
}
:
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;
}
}]);
그리고 여기 내 백엔드 코드입니다
이 도움을 주셔서 감사합니다.
는
나는 그것이 서버에서 오는 문제라고 생각하지만, 여전히 그것을 해결할 수 없습니다. – OussBou