11package com .okta .spring .example ;
22
3+ import com .okta .spring .boot .oauth .Okta ;
34import org .springframework .boot .SpringApplication ;
45import org .springframework .boot .autoconfigure .SpringBootApplication ;
5- import org .springframework .boot .web .servlet .FilterRegistrationBean ;
6- import org .springframework .context .annotation .Bean ;
76import org .springframework .context .annotation .Configuration ;
8- import org .springframework .core .Ordered ;
97import org .springframework .security .access .prepost .PreAuthorize ;
108import org .springframework .security .config .annotation .method .configuration .EnableGlobalMethodSecurity ;
119import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
1210import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
1311import org .springframework .security .oauth2 .server .resource .authentication .JwtAuthenticationToken ;
12+ import org .springframework .web .bind .annotation .CrossOrigin ;
1413import org .springframework .web .bind .annotation .GetMapping ;
1514import org .springframework .web .bind .annotation .RestController ;
16- import org .springframework .web .cors .CorsConfiguration ;
17- import org .springframework .web .cors .UrlBasedCorsConfigurationSource ;
18- import org .springframework .web .filter .CorsFilter ;
1915
2016import java .util .Arrays ;
21- import java .util .Collections ;
2217import java .util .Date ;
2318import java .util .HashMap ;
2419import java .util .Map ;
@@ -40,10 +35,17 @@ protected void configure(HttpSecurity http) throws Exception {
4035 .anyRequest ().authenticated ()
4136 .and ()
4237 .oauth2ResourceServer ().jwt ();
38+
39+ // process CORS annotations
40+ http .cors ();
41+
42+ // force a non-empty response body for 401's to make the response more browser friendly
43+ Okta .configureResourceServer401ResponseBody (http );
4344 }
4445 }
4546
4647 @ RestController
48+ @ CrossOrigin (origins = "http://localhost:8080" )
4749 public class MessageOfTheDayController {
4850
4951 @ GetMapping ("/api/userProfile" )
@@ -66,24 +68,6 @@ public Map<String, Object> messages() {
6668 }
6769 }
6870
69- /*
70- * Configuring CORS is only needed when making browser based requests (see the "front-end") example.
71- * The actual CORS configuration would be specific to your application.
72- */
73- @ Bean
74- public FilterRegistrationBean <CorsFilter > simpleCorsFilter () {
75- UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource ();
76- CorsConfiguration config = new CorsConfiguration ();
77- config .setAllowCredentials (true );
78- config .setAllowedOrigins (Arrays .asList ("http://localhost:8080" ));
79- config .setAllowedMethods (Collections .singletonList ("*" ));
80- config .setAllowedHeaders (Collections .singletonList ("*" ));
81- source .registerCorsConfiguration ("/**" , config );
82- FilterRegistrationBean <CorsFilter > bean = new FilterRegistrationBean <>(new CorsFilter (source ));
83- bean .setOrder (Ordered .HIGHEST_PRECEDENCE );
84- return bean ;
85- }
86-
8771 class Message {
8872 public Date date = new Date ();
8973 public String text ;
0 commit comments