Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit a167b9c

Browse files
committed
ADD: bearer configuration so a user can login via the api
1 parent 3b97702 commit a167b9c

File tree

4 files changed

+85
-30
lines changed

4 files changed

+85
-30
lines changed

src/main/java/com/wiemanboy/wiemanapi/SecurityConfig.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.wiemanboy.wiemanapi.config;
2+
3+
import com.okta.commons.lang.Assert;
4+
import org.springframework.security.oauth2.core.OAuth2Error;
5+
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
6+
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
7+
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
8+
import org.springframework.security.oauth2.jwt.Jwt;
9+
10+
import java.util.List;
11+
12+
class AudienceValidator implements OAuth2TokenValidator<Jwt> {
13+
private final String audience;
14+
15+
AudienceValidator(String audience) {
16+
Assert.hasText(audience, "audience is null or empty");
17+
this.audience = audience;
18+
}
19+
20+
public OAuth2TokenValidatorResult validate(Jwt jwt) {
21+
List<String> audiences = jwt.getAudience();
22+
if (audiences.contains(this.audience)) {
23+
return OAuth2TokenValidatorResult.success();
24+
}
25+
OAuth2Error err = new OAuth2Error(OAuth2ErrorCodes.INVALID_TOKEN);
26+
return OAuth2TokenValidatorResult.failure(err);
27+
}
28+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.wiemanboy.wiemanapi.config;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.http.HttpMethod;
7+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9+
import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator;
10+
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
11+
import org.springframework.security.oauth2.jwt.*;
12+
import org.springframework.security.web.SecurityFilterChain;
13+
14+
@Configuration
15+
@EnableWebSecurity
16+
public class SecurityConfig {
17+
18+
@Value("${okta.oauth2.audience}")
19+
private String audience;
20+
21+
@Value("${okta.oauth2.issuer}")
22+
private String issuer;
23+
24+
@Bean
25+
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
26+
return http
27+
.authorizeHttpRequests(authorize -> authorize
28+
.requestMatchers(HttpMethod.GET, "/services/profiles/actuator/**").permitAll()
29+
.requestMatchers(HttpMethod.GET, "/api/profiles/{id}").permitAll()
30+
.requestMatchers(HttpMethod.GET, "/api/profiles/{name}/{locale}").permitAll()
31+
.anyRequest().authenticated()
32+
)
33+
.oauth2Login(oauth2Login -> oauth2Login
34+
.defaultSuccessUrl("/services/profiles/docs/")
35+
.failureUrl("/")
36+
)
37+
.oauth2ResourceServer(oauth2ResourceServer -> oauth2ResourceServer
38+
.jwt(jwt -> jwt.decoder(jwtDecoder()))
39+
)
40+
.build();
41+
}
42+
43+
@Bean
44+
JwtDecoder jwtDecoder() {
45+
NimbusJwtDecoder jwtDecoder = JwtDecoders.fromOidcIssuerLocation(issuer);
46+
47+
OAuth2TokenValidator<Jwt> audienceValidator = new AudienceValidator(audience);
48+
OAuth2TokenValidator<Jwt> withIssuer = JwtValidators.createDefaultWithIssuer(issuer);
49+
OAuth2TokenValidator<Jwt> withAudience = new DelegatingOAuth2TokenValidator<>(withIssuer, audienceValidator);
50+
51+
jwtDecoder.setJwtValidator(withAudience);
52+
53+
return jwtDecoder;
54+
}
55+
}

src/main/resources/application.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ spring.data.mongodb.authentication-database=${MONGO_AUTH_DB:admin}
1313
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
1414
okta.oauth2.issuer=https://${AUTH0_BASE_URL}/
1515
okta.oauth2.client-id=${AUTH0_CLIENT_ID}
16-
okta.oauth2.client-secret=${AUTH0_CLIENT_SECRET}
16+
okta.oauth2.client-secret=${AUTH0_CLIENT_SECRET}
17+
okta.oauth2.audience=${AUTH0_AUDIENCE}

0 commit comments

Comments
 (0)