Spring-boot3 リダイレクト (ステータス コード: 302) の問題 (どのインターフェイスにアクセスしても、[/login] ログイン インターフェイスにリダイレクトされます)

問題の説明

今日、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);
    }

}

追加が完了したら、プログラムを再起動すると、今まで遭遇したことがないので大丈夫ですので、記録しておきます。

おすすめ

転載: blog.csdn.net/weixin_51033461/article/details/131240984