-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update auth flow * #50 Update auth flow --------- Co-authored-by: akselsf <akselsf>
- Loading branch information
Showing
8 changed files
with
127 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/com/example/autobank/security/CookieTokenResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import jakarta.servlet.http.HttpServletRequest | ||
import org.springframework.security.oauth2.server.resource.web.BearerTokenResolver | ||
import org.springframework.util.StringUtils | ||
|
||
class CookieTokenResolver : BearerTokenResolver { | ||
|
||
private val tokenName = "access_token" | ||
|
||
override fun resolve(request: HttpServletRequest): String? { | ||
val cookies = request.cookies ?: return null | ||
val tokenCookie = cookies.find { it.name == tokenName } | ||
|
||
return tokenCookie?.value?.takeIf { StringUtils.hasText(it) } | ||
} | ||
|
||
} |
53 changes: 37 additions & 16 deletions
53
src/main/kotlin/com/example/autobank/security/SecurityConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,61 @@ | ||
package com.example.autobank.security | ||
|
||
|
||
|
||
|
||
import CookieTokenResolver | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity | ||
import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator | ||
import org.springframework.security.oauth2.core.OAuth2TokenValidator | ||
import org.springframework.security.oauth2.jwt.* | ||
import org.springframework.security.oauth2.jwt.JwtDecoders | ||
import org.springframework.security.oauth2.jwt.JwtDecoder | ||
import org.springframework.security.oauth2.jwt.JwtValidators | ||
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder | ||
import org.springframework.security.oauth2.server.resource.web.BearerTokenResolver | ||
import org.springframework.security.web.SecurityFilterChain | ||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher | ||
|
||
@EnableWebSecurity | ||
class SecurityConfig() { | ||
@Configuration | ||
class SecurityConfig { | ||
|
||
@Value("\${auth0.audience}") | ||
private val audience: String = String() | ||
private val audience: String = "" | ||
|
||
@Value("\${spring.security.oauth2.resourceserver.jwt.issuer-uri}") | ||
private val issuer: String = String() | ||
private val issuer: String = "" | ||
|
||
@Bean | ||
fun jwtDecoder(): JwtDecoder { | ||
val jwtDecoder = JwtDecoders.fromOidcIssuerLocation(issuer) as NimbusJwtDecoder | ||
val audienceValidator: OAuth2TokenValidator<Jwt> = AudienceValidator(audience) | ||
val withIssuer: OAuth2TokenValidator<Jwt> = JwtValidators.createDefaultWithIssuer(issuer) | ||
val withAudience: OAuth2TokenValidator<Jwt> = DelegatingOAuth2TokenValidator(withIssuer, audienceValidator) | ||
jwtDecoder.setJwtValidator(withAudience) | ||
val withIssuer = JwtValidators.createDefaultWithIssuer(issuer) | ||
jwtDecoder.setJwtValidator(withIssuer) | ||
return jwtDecoder | ||
} | ||
|
||
@Bean | ||
@Throws(Exception::class) | ||
fun filterChain(http: HttpSecurity): SecurityFilterChain { | ||
return http.authorizeHttpRequests { authz -> | ||
authz.anyRequest().authenticated() | ||
}.build() | ||
fun bearerTokenResolver(): BearerTokenResolver { | ||
return CookieTokenResolver() | ||
} | ||
|
||
} | ||
@Bean | ||
@Override | ||
fun filterChain(http: HttpSecurity, bearerTokenResolver: BearerTokenResolver): SecurityFilterChain { | ||
println("YEEEEEEEEEEEEE") | ||
return http | ||
.authorizeHttpRequests { authz -> | ||
authz | ||
.requestMatchers(AntPathRequestMatcher("/api/auth/setuser")).permitAll() | ||
.anyRequest().authenticated() | ||
} | ||
.oauth2ResourceServer { oauth2 -> | ||
oauth2 | ||
.bearerTokenResolver(bearerTokenResolver) | ||
.jwt { jwt -> jwt.decoder(jwtDecoder()) } | ||
} | ||
.csrf { csrf -> csrf.disable() } | ||
.build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters