Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8b2d686

Browse files
committedJan 16, 2025
Add Okta dependency
1 parent c700e32 commit 8b2d686

File tree

5 files changed

+20
-68
lines changed

5 files changed

+20
-68
lines changed
 

‎pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@
3232
<spring-cloud.version>2024.0.0</spring-cloud.version>
3333
</properties>
3434
<dependencies>
35+
3536
<dependency>
37+
<groupId>com.okta.spring</groupId>
38+
<artifactId>okta-spring-boot-starter</artifactId>
39+
<version>3.0.7</version>
40+
</dependency>
41+
<!--dependency>
3642
<groupId>org.springframework.boot</groupId>
3743
<artifactId>spring-boot-starter-security</artifactId>
38-
</dependency>
44+
</dependency-->
3945
<dependency>
4046
<groupId>org.springframework.boot</groupId>
4147
<artifactId>spring-boot-starter-web</artifactId>

‎src/main/java/net/coderic/core/api/config/MvcConfig.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77

88
@Configuration
99
public class MvcConfig implements WebMvcConfigurer {
10-
/*
11-
@Override
12-
public void addViewControllers(ViewControllerRegistry registry) {
13-
registry.addViewController("/").setViewName("home");
14-
}
15-
*/
10+
1611
@Override
1712
public void addResourceHandlers(ResourceHandlerRegistry registry) {
1813
registry.addResourceHandler(

‎src/main/java/net/coderic/core/api/config/SecurityConfiguration.java

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,13 @@
3737
@EnableWebSecurity
3838
public class SecurityConfiguration {
3939

40-
private final String loginPage;
41-
42-
public SecurityConfiguration(@Value("${app.login-page}") String loginPage) {
43-
this.loginPage = loginPage;
44-
}
45-
40+
@Value("${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}")
41+
private String jwksUri;
4642
@Bean
4743
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
4844

4945
http
5046
.cors(AbstractHttpConfigurer::disable)
51-
//.cors(cors -> cors.configurationSource(corsConfigurationSource()))
5247
.authorizeHttpRequests((authorize) -> authorize
5348
.requestMatchers(
5449
"/",
@@ -67,38 +62,6 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
6762
.sessionCreationPolicy(SessionCreationPolicy.ALWAYS)
6863
.maximumSessions(10)
6964
)
70-
/*
71-
.oauth2Client(Customizer.withDefaults())
72-
73-
.oauth2Login((oauth2Login) -> oauth2Login
74-
//.loginPage()
75-
.loginProcessingUrl("/swagger-ui/index.html")
76-
77-
//.loginPage(this.loginPage)
78-
)*/
79-
/*
80-
.logout((logout) -> logout
81-
.logoutUrl("/logout")
82-
.logoutSuccessUrl("/")
83-
.invalidateHttpSession(true)
84-
.deleteCookies("JSESSIONID")
85-
.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler())
86-
.permitAll()
87-
)*/
88-
/*.logout(logout -> logout
89-
.addLogoutHandler(
90-
new HeaderWriterLogoutHandler(
91-
new ClearSiteDataHeaderWriter(
92-
ClearSiteDataHeaderWriter.Directive.CACHE,
93-
ClearSiteDataHeaderWriter.Directive.COOKIES
94-
)
95-
)
96-
)
97-
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
98-
.deleteCookies()
99-
.permitAll()
100-
)*/
101-
10265
.headers(headers -> headers
10366
.httpStrictTransportSecurity((hsts) -> hsts
10467
.includeSubDomains(true)
@@ -109,10 +72,11 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
10972
new XFrameOptionsHeaderWriter(XFrameOptionsHeaderWriter.XFrameOptionsMode.DENY)
11073
)
11174
)
75+
11276
.oauth2ResourceServer(oauth2 -> oauth2
11377
.jwt(jwt -> jwt
11478
.jwtAuthenticationConverter(jwtAuthenticationConverter())
115-
.jwkSetUri("https://auth.coderic.org/.well-known/jwks.json")
79+
.jwkSetUri(jwksUri)
11680
)
11781
)
11882
.requiresChannel(
@@ -138,24 +102,6 @@ public CorsConfigurationSource corsConfigurationSource() {
138102

139103
private JwtAuthenticationConverter jwtAuthenticationConverter() {
140104
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
141-
// Configura cómo deseas mapear los claims del token JWT a roles/autorizaciones
142105
return converter;
143106
}
144-
@Bean
145-
public JwtDecoder jwtDecoder() throws IOException, JOSEException, ParseException {
146-
// URL del JWKS de Auth0
147-
String jwkSetUri = "https://auth.coderic.org/.well-known/jwks.json";
148-
JWKSet jwkSet = JWKSet.load(new URL(jwkSetUri));
149-
JWK jwk = jwkSet.getKeys().get(0);
150-
RSAKey rsaKey = (RSAKey) jwk;
151-
RSAPublicKey publicKey = rsaKey.toRSAPublicKey();
152-
return NimbusJwtDecoder.withPublicKey(publicKey).build();
153-
}
154-
/*
155-
@Bean
156-
public JwtDecoder jwtDecoder() {
157-
// URI del JWKS de Auth0
158-
String jwkSetUri = "https://auth.coderic.org/.well-known/jwks.json";
159-
return NimbusJwtDecoder.withJwkSetUri(jwkSetUri).build();
160-
}*/
161107
}

‎src/main/java/net/coderic/core/api/controllers/HelloController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public class HelloController {
1818
public ResponseEntity<Principal> getUser(@AuthenticationPrincipal Principal principal) {
1919
return new ResponseEntity<Principal>(principal, HttpStatus.OK);
2020
}
21+
@GetMapping("/hello")
22+
public String hello(Principal principal) {
23+
return "Hello, " + principal.getName() + "!";
24+
}
2125
/*
2226
@GetMapping("/logout")
2327
public ResponseEntity<Boolean> getLogout(Authentication authentication, HttpServletRequest request, HttpServletResponse response) {

‎src/main/resources/application.properties

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
server.port=${PORT}
22
server.http.port=${PORT}
33
server.http2.enabled=true
4-
spring.security.oauth2.resourceserver.jwt.issuer-uri= https://auth.coderic.org/
5-
4+
spring.security.oauth2.resourceserver.jwt.issuer-uri=https://auth.coderic.org/
5+
spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://auth.coderic.org/.well-known/jwks.json
66
logging.level.org.springframework.security=trace
7-
87
messages.base-url=http://localhost:8090
98

109
app.login-page=/oauth2/authorization/okta
@@ -30,3 +29,5 @@ memcached.username=${MEMCACHIER_USERNAME}
3029
memcached.password=${MEMCACHIER_PASSWORD}
3130

3231
security.require-ssl=true
32+
okta.oauth2.issuer=https://auth.coderic.org/
33+
okta.oauth2.audience=https://coderic.eu.auth0.com/api/v2/

0 commit comments

Comments
 (0)
Please sign in to comment.