Skip to content

Commit 7a4ad42

Browse files
authored
Merge pull request okta#49 from okta/fix-default-401
Cleanup Resource Server example
2 parents a3447be + 31e4f1b commit 7a4ad42

4 files changed

Lines changed: 15 additions & 31 deletions

File tree

custom-login/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<parent>
2121
<groupId>org.springframework.boot</groupId>
2222
<artifactId>spring-boot-starter-parent</artifactId>
23-
<version>2.1.3.RELEASE</version>
23+
<version>2.1.5.RELEASE</version>
2424
</parent>
2525

2626
<groupId>com.example.okta</groupId>
@@ -41,7 +41,7 @@
4141
<dependency>
4242
<groupId>com.okta.spring</groupId>
4343
<artifactId>okta-spring-boot-starter</artifactId>
44-
<version>1.2.0</version>
44+
<version>1.2.1</version>
4545
</dependency>
4646
<dependency>
4747
<groupId>org.springframework.security</groupId>

okta-hosted-login/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>2.1.3.RELEASE</version>
9+
<version>2.1.5.RELEASE</version>
1010
</parent>
1111

1212
<groupId>com.example.okta</groupId>
@@ -27,7 +27,7 @@
2727
<dependency>
2828
<groupId>com.okta.spring</groupId>
2929
<artifactId>okta-spring-boot-starter</artifactId>
30-
<version>1.2.0</version>
30+
<version>1.2.1</version>
3131
</dependency>
3232
<dependency>
3333
<groupId>org.springframework.boot</groupId>

resource-server/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>2.1.3.RELEASE</version>
9+
<version>2.1.5.RELEASE</version>
1010
</parent>
1111

1212
<groupId>com.example.okta</groupId>
@@ -27,7 +27,7 @@
2727
<dependency>
2828
<groupId>com.okta.spring</groupId>
2929
<artifactId>okta-spring-boot-starter</artifactId>
30-
<version>1.2.0</version>
30+
<version>1.2.1</version>
3131
</dependency>
3232

3333
<!-- Other standard Spring starters -->

resource-server/src/main/java/com/okta/spring/example/ResourceServerExampleApplication.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
package com.okta.spring.example;
22

3+
import com.okta.spring.boot.oauth.Okta;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
5-
import org.springframework.boot.web.servlet.FilterRegistrationBean;
6-
import org.springframework.context.annotation.Bean;
76
import org.springframework.context.annotation.Configuration;
8-
import org.springframework.core.Ordered;
97
import org.springframework.security.access.prepost.PreAuthorize;
108
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
119
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
1210
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
1311
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
12+
import org.springframework.web.bind.annotation.CrossOrigin;
1413
import org.springframework.web.bind.annotation.GetMapping;
1514
import 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

2016
import java.util.Arrays;
21-
import java.util.Collections;
2217
import java.util.Date;
2318
import java.util.HashMap;
2419
import 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

Comments
 (0)