-
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.
- Loading branch information
1 parent
6fd5ff4
commit 717d630
Showing
2 changed files
with
69 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.example.bff.auth.handlers | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.MediaType | ||
import org.springframework.security.core.AuthenticationException | ||
import org.springframework.security.web.server.ServerAuthenticationEntryPoint | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.server.ServerWebExchange | ||
import reactor.core.publisher.Mono | ||
import java.io.IOException | ||
|
||
/**********************************************************************************************************************/ | ||
/****************************************************** HANDLER *******************************************************/ | ||
/**********************************************************************************************************************/ | ||
|
||
//* FOR AUTHENTICATION FAILURES *// | ||
|
||
@Component | ||
internal class AuthenticationHandler : ServerAuthenticationEntryPoint { | ||
private val objectMapper = ObjectMapper() | ||
|
||
@Throws(IOException::class) | ||
override fun commence(exchange: ServerWebExchange?, ex: AuthenticationException?): Mono<Void> { | ||
|
||
val response = exchange?.response ?: return Mono.empty() | ||
response.statusCode = HttpStatus.FORBIDDEN | ||
response.headers.contentType = MediaType.APPLICATION_JSON | ||
response.headers.add("Authentication-Status", "Forbidden") | ||
|
||
val errorMessage = ex?.message ?: "Authentication Failed" | ||
val errorResponse = objectMapper.writeValueAsString(mapOf("Authentication Failed" to errorMessage)) | ||
val responseBody = response.bufferFactory().wrap(errorResponse.toByteArray()) | ||
|
||
return response.writeWith(Mono.just(responseBody)) | ||
} | ||
} | ||
|
||
/**********************************************************************************************************************/ | ||
/**************************************************** END OF KOTLIN ***************************************************/ | ||
/**********************************************************************************************************************/ |
28 changes: 28 additions & 0 deletions
28
Spring BFF/bff/auth/handlers/StaticResourceByPassFilter.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,28 @@ | ||
package com.example.bff.auth.handlers | ||
|
||
import com.example.bff.auth.BffSecurityIgnoreConfig | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.server.ServerWebExchange | ||
import org.springframework.web.server.WebFilter | ||
import org.springframework.web.server.WebFilterChain | ||
import reactor.core.publisher.Mono | ||
|
||
|
||
@Component | ||
internal class StaticResourceBypassFilter( | ||
private val uriEndPointFilter: BffSecurityIgnoreConfig | ||
) : WebFilter { | ||
|
||
override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> { | ||
val requestPath = exchange.request.uri.path | ||
|
||
// check if the request path matches any of the static resource patterns | ||
if (uriEndPointFilter.shouldSkipStaticResources(requestPath)) { | ||
// skip further processing and directly return the response | ||
return exchange.response.setComplete() | ||
} | ||
|
||
// proceed with the security filters for non-static resources | ||
return chain.filter(exchange) | ||
} | ||
} |