@aya0724masa

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

spring-boot-starter-securityの依存関係を追加すると、ページ表示できなくなる

解決したいこと

Spring bootでWebアプリを作っています。
ユーザーのログインページを作りましたが、
spring-boot-starter-securityの依存関係を追加すると、HTTPリクエストエラーになり、ログインページを表示できません。
依存関係を追加しないと、正常に画面表示できます。
依存関係を追加した状態で、正常に画面表示できるようにしたいです。

発生している問題・エラー

ログインページに遷移しようとすると、以下のエラーになります。

This page isn’t working
localhost redirected you too many times.
Try deleting your cookies.
ERR_TOO_MANY_REDIRECTS

開発者ツールでリクエストを確認すると、大量のリクエストが送られていて、全て302エラーになっています。

該当するソースコード

以下のように依存関係を追加しています。

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>

自分で試したこと

SecurityConfigクラスを作成し、
/loginと/registerのURLは認証されていなくてもアクセスできるように設定しているのですが、正常に画面遷移できません。

package com.reading.tracker.readingtracker.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
public class SecurityConfig{

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .csrf(csrf -> csrf.disable())
            .authorizeHttpRequests(auth -> auth
                .requestMatchers("/register", "/login").permitAll()
                .anyRequest().authenticated()
            )
            .formLogin(form -> form
                .loginPage("/login").permitAll()
                .defaultSuccessUrl("/register")
            )
            .logout(logout -> logout
                .permitAll()
                .logoutSuccessUrl("/login")
            );

        return http.build();
    }
}
0 likes

1Answer

Spring Securityでは、デフォルトで提供されるログインフォームのパスが /loginであり、カスタムのログインフォームと同じパスの場合、競合が発生する可能性があります。一度Spring Securityのログイン機能を無効化してみるのはいかがでしょうか?

@Configuration
@EnableWebSecurity
public class SecurityConfig{
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .csrf(csrf -> csrf.disable())
                .authorizeHttpRequests(auth -> auth
                       .requestMatchers("/register", "/login").permitAll()
                        .anyRequest().authenticated()
                )
+                .formLogin(form -> form.disable())
                .logout(logout -> logout
                        .permitAll()
                        .logoutSuccessUrl("/login")
                );
        return http.build();
    }
}
 .formLogin(form -> form.disable())

韓国人なので日本語翻訳を 돌렸습니다。文脈が変かもしれません。

0Like

Comments

  1. @aya0724masa

    Questioner

    コードの修正案をいただき、ありがとうございます。
    ただ、コードを修正しても、挙動は変わりませんでした。
    @tomdog735 さんの言うようにSecurityConfig以外がおかしいのかもしれません。

    Screenshot 2024-11-06 at 19.46.44.png

Your answer might help someone💌