Skip to content

Commit

Permalink
Add Spring Security config and disable csrf protection
Browse files Browse the repository at this point in the history
  • Loading branch information
tintintti committed Sep 9, 2024
1 parent 2eedd31 commit f08840f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/main/kotlin/fi/oph/kitu/WebSecurityConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fi.oph.kitu

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.config.Customizer
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.web.SecurityFilterChain

@Configuration
@EnableWebSecurity
class WebSecurityConfig {
@Bean
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http
.csrf { csrf -> csrf.ignoringRequestMatchers("/api/*") }
.authorizeHttpRequests { authorize ->
authorize
.anyRequest()
.authenticated()
}.httpBasic(Customizer.withDefaults())
.formLogin(Customizer.withDefaults())

return http.build()
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/fi/oph/kitu/oppija/OppijaController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class OppijaController {
@Autowired
private lateinit var oppijaService: OppijaService

@GetMapping("/oppija")
@GetMapping("/api/oppija")
fun getOppijat(): Iterable<Oppija> = oppijaService.getAll()

@PostMapping("/oppija")
@PostMapping("/api/oppija")
fun addOppija(
@RequestBody name: String,
) = oppijaService.insert(name)
Expand Down
4 changes: 2 additions & 2 deletions src/test/kotlin/fi/oph/kitu/oppija/OppijaTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class OppijaTests(
fun `get oppija`() {
client
.get()
.uri("/oppija")
.uri("/api/oppija")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus()
Expand All @@ -37,7 +37,7 @@ class OppijaTests(
fun `post oppija`() {
client
.post()
.uri("/oppija")
.uri("/api/oppija")
.bodyValue("Mikko Mallikas")
.exchange()
.expectStatus()
Expand Down

0 comments on commit f08840f

Please sign in to comment.