-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSecurityConfig.kt
More file actions
64 lines (55 loc) · 2.39 KB
/
Copy pathSecurityConfig.kt
File metadata and controls
64 lines (55 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.example.attendanceapimono.adapter.infra.config
import com.example.attendanceapimono.adapter.infra.security.AuthenticationManager
import com.example.attendanceapimono.adapter.infra.security.PermitSet
import com.example.attendanceapimono.adapter.infra.security.SecurityContextRepository
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpMethod
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity
import org.springframework.security.config.web.server.ServerHttpSecurity
import org.springframework.security.web.server.SecurityWebFilterChain
@Configuration
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
class SecurityConfig(
private val authenticationManager: AuthenticationManager,
private val securityContextRepository: SecurityContextRepository,
) {
private val permitAllUrls = listOf(
PermitSet(HttpMethod.GET, "/favicon.ico"),
PermitSet(HttpMethod.POST, "/event"),
PermitSet(HttpMethod.POST, "/user"),
PermitSet(HttpMethod.POST, "/user/login"),
// Swagger
PermitSet(HttpMethod.GET, "/swagger-resources/**"),
PermitSet(HttpMethod.GET, "/webjars/**"),
PermitSet(HttpMethod.GET, "/v3/api-docs/**"),
PermitSet(HttpMethod.GET, "/doc"),
PermitSet(HttpMethod.GET, "/swagger-ui/**"),
)
@Bean
fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
return http.cors().and()
.csrf().disable()
.headers().frameOptions().disable().and()
.formLogin().disable()
.httpBasic().disable()
.authenticationManager(authenticationManager)
.securityContextRepository(securityContextRepository)
.authorizeExchange()
.pathMatchers(HttpMethod.OPTIONS).permitAll()
.processPermitAll()
.anyExchange().authenticated()
.and()
.build()
}
private fun ServerHttpSecurity.AuthorizeExchangeSpec.processPermitAll():
ServerHttpSecurity.AuthorizeExchangeSpec {
var last = this
permitAllUrls.forEach {
last = last.pathMatchers(it.method, it.path).permitAll()
}
return last
}
}