jsp 迁移到 Spring security 6 后出现 ERR_TOO_MANY_REDIRECTS 异常

我把项目迁移到 Spring boot 3,配置了 jsp 。在使用 Spring Security 6 时,页面加载失败,浏览器显示错误 ERR_TOO_MANY_REDIRECTS

输出了很多一样的 DEBUG 日志:

2024-06-27T10:24:47.982+02:00 DEBUG 55771 --- [jsp] [0.1-8080-exec-2] o.s.w.servlet.view.InternalResourceView  : View name 'login', model {}
2024-06-27T10:24:47.983+02:00 DEBUG 55771 --- [jsp] [0.1-8080-exec-2] o.s.w.servlet.view.InternalResourceView  : Forwarding to [/WEB-INF/jsp/login.jsp]
2024-06-27T10:24:47.984+02:00 DEBUG 55771 --- [jsp] [0.1-8080-exec-4] o.s.w.servlet.view.InternalResourceView  : View name 'login', model {}
2024-06-27T10:24:47.984+02:00 DEBUG 55771 --- [jsp] [0.1-8080-exec-4] o.s.w.servlet.view.InternalResourceView  : Forwarding to [/WEB-INF/jsp/login.jsp]

Security 配置类 src/main/java/org/example/jsp/WebSecurityConfig.java 如下:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http

                .authorizeHttpRequests(auth -> auth
                        .requestMatchers("/login").permitAll()
                        .anyRequest().authenticated()
                )
                .formLogin(form -> form.loginPage("/login")
                                .loginProcessingUrl("/login")
                        .permitAll()
                )
                .logout(logout -> logout
                        .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                        .logoutSuccessUrl("/login")
                        .permitAll()
                );
        return http.build();
    }

    @Bean
    public UserDetailsService userDetailsService() {
        UserDetails user =
            User.withDefaultPasswordEncoder()
                .username("user")
                .password("password")
                .roles("USER")
                .build();

        return new InMemoryUserDetailsManager(user);
    }
}

JSP 文件 src/main/webapp/WEB-INF/jsp/login.jsp 如下:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="${pageContext.request.contextPath}/login" method="post">
    <div>
        <label for="username">Username:</label>
        <input type="text" id="username" name="username"/>
    </div>
    <div>
        <label for="password">Password:</label>
        <input type="password" id="password" name="password"/>
    </div>
    <div>
        <button type="submit">Login</button>
    </div>
    <c:if test="${param.error != null}">
        <div>Invalid username or password.</div>
    </c:if>
</form>
</body>
</html>

JspController src/main/java/org/example/jsp/JspController.java 如下:

@Controller
public class JspController {

    @GetMapping("/login")
    public String getLogin() {
        return "login";
    }

    @GetMapping("/home")
    public String home() {
        return "home";
    }
}

Mave pox.xml 依赖、配置如下:

<properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <!-- Spring Boot dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
        </dependency>

        <!-- JSP dependencies -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.servlet.jsp</groupId>
            <artifactId>jakarta.servlet.jsp-api</artifactId>
            <version>3.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jakarta.servlet.jsp.jstl</artifactId>
<!--            <version>2.0.0</version>-->
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- Server dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- Test dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

我的 Spring boot 版本是 3.2.7

src/main/resources/application.properties 配置如下:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

我也试过添加 Mvc 配置,src/main/java/org/example/jsp/WebMvcConfig.java 如下:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

最后都不行,有没有大佬指点一下?:slight_smile: