LoginSignup
1
0

More than 1 year has passed since last update.

spring-securityでpermitAllが効かずAPIのレスポンスが401になる

Posted at

はじめに

以下の書籍の7章でspring-securityを学習していた際につまづいたのでまとめます

spring-securityの学習について

そもそもspring-securityをどのように使うのかは書籍と以下のサイトが参考になりました
注意としては書籍のspring-securityのバージョンは古く、現在のバージョン6で大きく書き方が変わっているので注意が必要です

問題

以下のAPIを作成しました

ToDoController.kt
package com.example.auth.presentation.controller

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping

@Controller
class ToDoController() {
   @GetMapping("/hello")
   fun hello(): String {
       return "Hello"
   }
}

そしてspring-securityの設定ファイルを作成しました
ここでは/helloというアクセスは認証なしにアクセスできるように設定しました

SecurityConfig.kt
package com.example.auth.presentation.config

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
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 SecurityConfig {

    @Bean
    fun configureHttpSecurity(httpSecurity: HttpSecurity): SecurityFilterChain {
        httpSecurity
            .authorizeHttpRequests()
            .requestMatchers("/hello").permitAll()
            .and()
            .csrf().disable()
        return httpSecurity.build()
    }
}

しかしこの状態で

$ curl http://localhost:8080/hello

にアクセスすると

HTTP/1.1 401 
Set-Cookie: JSESSIONID=98C4234BA2E22192C36A48E2D07D1D43; Path=/; HttpOnly
WWW-Authenticate: Basic realm="Realm"
X-Content-Type-Options: nosniff
X-XSS-Protection: 0
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
WWW-Authenticate: Basic realm="Realm"
Content-Length: 0
Date: Sun, 15 Jan 2023 08:20:12 GMT

と認証エラーが発生してしまいます。認証する設定にしていないはずなのに、、、

画面にアクセスすると以下の画面がでて、エラーページを表示しようとしています

image.png

解決方法

まずはspring-securityの依存をすべてコメントにしてAPIが正しく動くか試しました
すると以下のエラーが発生

HTTP/1.1 401 
Set-Cookie: JSESSIONID=98C4234BA2E22192C36A48E2D07D1D43; Path=/; HttpOnly
WWW-Authenticate: Basic realm="Realm"
X-Content-Type-Options: nosniff
X-XSS-Protection: 0
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
WWW-Authenticate: Basic realm="Realm"
Content-Length: 0
Date: Sun, 15 Jan 2023 08:20:12 GMT

先程と同じエラーが発生しました
てことはspring-securityの設定は関係ないということがわかりました

このページはControllerに問題があるときにでるようなので確認してみると

ToDoContoroller
@Controller
class ToDoController() {

となっていました。今回の場合@Contorollerでなく@RestControllerでないとだめでした

@Controllerの場合はViewが必要になるのが原因でした

ToDoContoroller
@RestController
class ToDoController() {

と修正したところしっかり認証無しでAPIがたたけるようになりました

おわりに

問題を特定するまでにかなり時間がかかってしまいました
そもそもspring-securityのせいだと思い込み調べまくっていたのがよくなかったです

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0