[스프링 부트] chapter 24. 스프링 웹 MVC 11부 CORS

스프링 웹 MVC 11부 CORS

SOP과 CORS

스프링 MVC @CrossOrigin

@RestController
@SpringBootApplication
public class SpringcorsserverApplication { 

    @CrossOrigin(origins = "[http://localhost:18080](http://localhost:18080/)") 
    @GetMapping("/hello") 
    public String hello() { 
        return "hello"; 
    } 

    public static void main(String[] args) { 
        SpringApplication.run(SpringcorsserverApplication.class, args); 
    } 
} 
@RestController
@SpringBootApplication
public class SpringcorsserverApplication { 

    @GetMapping("/hello") 
    public String hello() { 
        return "hello"; 
    } 

    public static void main(String[] args) { 
        SpringApplication.run(SpringcorsserverApplication.class, args); 
    } 
} 
@Configuration
public class WebConfig implements WebMvcConfigurer { 

    @Override 
    public void addCorsMappings(CorsRegistry registry) { 
        registry.addMapping("/**")
                .allowedOrigins("[http://localhost:18080](http://localhost:18080/)");
    } 
}
<html lang="en"> 
<head>
    <meta charset="UTF-8">
    <title>Hello Title</title> 
</head>
<body>
Hello static resources 
<script src="/webjars/jquery/dist/jquery.min.js"></script>
<script>
    $(function() { 
        $.ajax("<http://localhost:7777/hello>") 
            .done(function(msg) { 
                alert(msg); 
            }) 
            .fail(function() { 
                alert("alert") 
            }); 
    }); 
</script>
</body>
</html>

<!DOCTYPE html>