Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c7a824702e | ||
| adefbb465e | |||
| 46a4852257 | |||
| 7a910c8428 | |||
| b070a22b0e | |||
| e19e0e59bc | |||
|
|
c69a9cd07c |
@@ -1,5 +1,5 @@
|
||||
kotlin.code.style=official
|
||||
version=0.3.0
|
||||
version=0.4.0
|
||||
catalog=0.11.0
|
||||
docker.port.expose=8443
|
||||
container.port.expose=8443
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package ltd.hlaeja.configuration
|
||||
|
||||
import ltd.hlaeja.security.JwtAuthenticationConverter
|
||||
import ltd.hlaeja.security.JwtAuthenticationManager
|
||||
import ltd.hlaeja.security.authorize.publicPaths
|
||||
import ltd.hlaeja.security.converter.JwtAuthenticationConverter
|
||||
import ltd.hlaeja.security.manager.JwtAuthenticationManager
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity
|
||||
@@ -55,6 +56,6 @@ class SecurityConfiguration {
|
||||
private fun authorizeExchange(
|
||||
authorizeExchange: AuthorizeExchangeSpec,
|
||||
) = authorizeExchange
|
||||
.pathMatchers("/login").permitAll()
|
||||
.anyExchange().hasRole("REGISTRY")
|
||||
.publicPaths().permitAll()
|
||||
.anyExchange().hasAnyRole("REGISTRY", "ADMIN")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package ltd.hlaeja.security.authorize
|
||||
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity.AuthorizeExchangeSpec
|
||||
|
||||
fun AuthorizeExchangeSpec.publicPaths(): AuthorizeExchangeSpec.Access = pathMatchers(
|
||||
"/actuator/**",
|
||||
"/login",
|
||||
)
|
||||
@@ -1,9 +1,11 @@
|
||||
package ltd.hlaeja.security
|
||||
package ltd.hlaeja.security.converter
|
||||
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.jsonwebtoken.JwtException
|
||||
import java.util.UUID
|
||||
import ltd.hlaeja.jwt.service.PublicJwtService
|
||||
import ltd.hlaeja.security.user.JwtAuthentication
|
||||
import ltd.hlaeja.security.user.JwtUserDetails
|
||||
import org.springframework.http.HttpStatus.UNAUTHORIZED
|
||||
import org.springframework.security.core.Authentication
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority
|
||||
@@ -40,14 +42,14 @@ class JwtAuthenticationConverter(
|
||||
}
|
||||
|
||||
private fun jwtAuthenticationToken(token: String) = publicJwtService.verify(token) { claims ->
|
||||
JwtAuthenticationToken(
|
||||
JwtAuthentication(
|
||||
JwtUserDetails(
|
||||
UUID.fromString(claims.payload["id"] as String),
|
||||
claims.payload["username"] as String,
|
||||
),
|
||||
token,
|
||||
(claims.payload["role"] as String).split(",")
|
||||
.map { SimpleGrantedAuthority(it) }
|
||||
.map { SimpleGrantedAuthority("ROLE_$it") }
|
||||
.toMutableList(),
|
||||
true,
|
||||
)
|
||||
@@ -1,5 +1,6 @@
|
||||
package ltd.hlaeja.security
|
||||
package ltd.hlaeja.security.manager
|
||||
|
||||
import ltd.hlaeja.security.user.JwtAuthentication
|
||||
import org.springframework.security.authentication.ReactiveAuthenticationManager
|
||||
import org.springframework.security.core.Authentication
|
||||
import org.springframework.security.core.AuthenticationException
|
||||
@@ -11,14 +12,14 @@ class JwtAuthenticationManager : ReactiveAuthenticationManager {
|
||||
|
||||
override fun authenticate(
|
||||
authentication: Authentication,
|
||||
): Mono<Authentication> = if (authentication is JwtAuthenticationToken) {
|
||||
): Mono<Authentication> = if (authentication is JwtAuthentication) {
|
||||
handleJwtToken(authentication)
|
||||
} else {
|
||||
Mono.error(object : AuthenticationException("Unsupported authentication type") {})
|
||||
}
|
||||
|
||||
private fun handleJwtToken(
|
||||
token: JwtAuthenticationToken,
|
||||
token: JwtAuthentication,
|
||||
): Mono<Authentication> = if (token.isAuthenticated) {
|
||||
Mono.just(token)
|
||||
} else {
|
||||
@@ -1,9 +1,9 @@
|
||||
package ltd.hlaeja.security
|
||||
package ltd.hlaeja.security.user
|
||||
|
||||
import org.springframework.security.core.Authentication
|
||||
import org.springframework.security.core.GrantedAuthority
|
||||
|
||||
data class JwtAuthenticationToken(
|
||||
data class JwtAuthentication(
|
||||
private val jwtUserDetails: JwtUserDetails,
|
||||
private val token: String,
|
||||
private var authorities: MutableCollection<out GrantedAuthority>,
|
||||
@@ -1,4 +1,4 @@
|
||||
package ltd.hlaeja.security
|
||||
package ltd.hlaeja.security.user
|
||||
|
||||
import java.util.UUID
|
||||
|
||||
Reference in New Issue
Block a user