Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamstar-enterprises authored Aug 18, 2024
1 parent 6fd5ff4 commit 717d630
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Spring BFF/bff/auth/handlers/AuthenticationHandler.kt
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 Spring BFF/bff/auth/handlers/StaticResourceByPassFilter.kt
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)
}
}

0 comments on commit 717d630

Please sign in to comment.