1
1
.
.
9
9
.
.
1
1
A
A
p
p
p
p
l
l
i
i
c
c
a
a
t
t
i
i
o
o
n
n
-
-
S
S
o
o
u
u
r
r
c
c
e
e
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial shows how to create Source Application which will be used to demonstrate CORS functionality.
Source Application will run on Port 8080 and it will contain HTML with Java Script calling Destination Application.
Source Application created in this tutorial will remain the same across subsequent tutorials.
This is because all CORS related changes only need to be done in Destination Application.
It is Destination Application that needs to allow CORS access to its resources from other Origins/Applications.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller, @RequestMapping and Tomcat Server.
Template Engines
Thyemeleaf
Enables Controller to return reference to HTML Page Test.html
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_cors_source (add Spring Boot Starters from the table)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
Create HTML File: Test.html (inside directory resources/templates)
MyController.java
package com.ivoronline.springboot_cors_source.controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.stereotype.Controller;
@Controller
public class MyController {
@GetMapping("/Hello")
public String hello() {
return "Test";
}
}
Test.html
<title>Test.html</title>
<p>Hello from Test.html</p>
<script>
var request = new XMLHttpRequest();
request.open('GET', 'http://www.ivoronline.com/Images/Banners/Banner_1.png');
request.send(); //This line throws CORS Error
</script>
Test.html
http://localhost:8080/Hello
Tomcat
MyController
hello()
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello (just to confirm that Application is properly setup)
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>