Spring-boot3 redirection (status code: 302) problem (no matter which interface is accessed, it will be redirected to [/login] login interface)

Problem Description

Today, after using spring-boot to do hot reload, there is a problem. No matter which interface I visit, the interface will be redirected to the following interface address: "localhost:/login"
insert image description here

chatGPT solution

I asked chatGPT, and here is the answer he gave me (unable to solve it):


If you are using Spring Security for authentication in your Spring Boot project and it is not properly configured, you may have issues redirecting to the "/login" path. This is usually because Spring Security redirects unauthenticated requests to the "/login" path by default. To fix this, you can add the following code to your Spring Security configuration:

http
    .authorizeRequests()
        .antMatchers("/login").permitAll()
        .anyRequest().authenticated()
        .and()
    .formLogin()
        .loginPage("/custom-login-page")
        .permitAll();

Among them, "/login" is the default login page path, you can replace it with your own login page path. Also, if you want to disable redirects, you can add the following code to your configuration:

http
    .authorizeRequests()
        .antMatchers("/login").permitAll()
        .anyRequest().authenticated()
        .and()
    .formLogin()
        .loginPage("/custom-login-page")
        .permitAll()
        .successHandler((request, response, authentication) -> {})
        .failureHandler((request, response, exception) -> {});

This will disable redirection with empty success and failure handlers.


way to successfully resolve

1. Delete security-related packages

for example:

<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. Add to the annotation of the startup class

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})

@SpringBootApplication(exclude = {
    
    SecurityAutoConfiguration.class})
public class XXApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(XXApplication.class, args);
    }

}

After the addition is complete, restart the program, and it will be fine, because I haven't encountered it before, so record it.

Guess you like

Origin blog.csdn.net/weixin_51033461/article/details/131240984