I am testing my understanding of how CORS works. I have 2 applications run on different port. I am sending request to app A from app B. App A run on port 8080
@Controller
@EnableAutoConfiguration
public class HelloController {
@CrossOrigin(origins = "http://localhost:9000")
@RequestMapping("/hello")
@ResponseBody
public String sayHello() {
return "Hello World Developer!!!";
}
}
App B runs on port 8081
@RequestMapping("/hello")
@ResponseBody
public String sayHello() throws IOException {
String GET_URL = "http://localhost:8080/hello";
URL obj = new URL(GET_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
InputStream inputStream;
if (200 <= responseCode && responseCode <= 299) {
inputStream = con.getInputStream();
} else {
inputStream = con.getErrorStream();
}
BufferedReader in = new BufferedReader(
new InputStreamReader(
inputStream));
StringBuilder response = new StringBuilder();
String currentLine;
while ((currentLine = in.readLine()) != null)
response.append(currentLine);
return response.toString();
}
By setting @CrossOrigin(origins = "http://localhost:9000")
the request should not have been succesful since the only request origin allow to app A is from port 9000. Is that correct ? But here i am getting the succesful response code 200 "Hello World Developer!!!"