問題の説明
今日、Spring-Boot を使用してホット リロードを実行した後、問題が発生しました。どのインターフェイスにアクセスしても、インターフェイスはインターフェイス アドレス「localhost:/login」にリダイレクトされます。
チャットGPTソリューション
chatGPTに尋ねたところ、彼が私にくれた答えは次のとおりです(解決できませんでした)。
Spring Boot プロジェクトの認証に Spring Security を使用していて、それが適切に構成されていない場合、「/login」パスへのリダイレクトで問題が発生する可能性があります。これは通常、Spring Security がデフォルトで未認証リクエストを「/login」パスにリダイレクトするためです。これを修正するには、次のコードを Spring Security 構成に追加します。
http
.authorizeRequests()
.antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/custom-login-page")
.permitAll();
このうち、「/login」はデフォルトのログインページのパスです。独自のログインページのパスに置き換えることができます。また、リダイレクトを無効にする場合は、次のコードを構成に追加できます。
http
.authorizeRequests()
.antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/custom-login-page")
.permitAll()
.successHandler((request, response, authentication) -> {})
.failureHandler((request, response, exception) -> {});
これにより、空の成功ハンドラーと失敗ハンドラーによるリダイレクトが無効になります。
うまく解決する方法
1. セキュリティ関連のパッケージを削除する
例えば:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.5.0</version>
</dependency>
2. スタートアップクラスのアノテーションを追加する
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
@SpringBootApplication(exclude = {
SecurityAutoConfiguration.class})
public class XXApplication {
public static void main(String[] args) {
SpringApplication.run(XXApplication.class, args);
}
}
追加が完了したら、プログラムを再起動すると、今まで遭遇したことがないので大丈夫ですので、記録しておきます。