Spring Security 6 和 JSP 视图渲染

Mar*_*ark 3 spring-mvc spring-security

我正在将应用程序从 Spring Boot 2.7 升级到 Spring Boot 3,其中包括更新到 Spring Security 6。

我们设置了以下属性:

spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
Run Code Online (Sandbox Code Playgroud)

我们使用 JSP 作为模板语言,其中控制器返回视图文件名,例如

@RequestMapping("/")
public String home() {
  return "home";
}
Run Code Online (Sandbox Code Playgroud)

这将呈现 JSP 页面/WEB-INF/view/home.jsp

安全配置是例如

@Configuration
public class SecurityConfig  {
    @Bean
    public SecurityFilterChain config(HttpSecurity http) throws Exception {
       http.authorizeHttpRequests((auth) -> auth
          .requestMatchers("/").permitAll()
          .anyRequest().authenticated()
      );

}
Run Code Online (Sandbox Code Playgroud)

升级后,访问localhost/会将浏览器重定向到localhost/WEB-INF/view/home.jsp,并返回 403,因为不允许访问该页面。

如果我允许访问它,.requestMatchers("/", "/WEB-INF/**").permitAll()它就可以正常工作(即保持打开/并呈现 JSP 页面),但这似乎是一个坏主意,也是一个不必要的步骤。

打开调试日志记录后,Spring 会记录以下内容:

DEBUG [requestURL=/] o.s.security.web.FilterChainProxy        : Securing GET /
DEBUG [requestURL=/] o.s.security.web.FilterChainProxy        : Secured GET /
DEBUG [requestURL=/] o.s.security.web.FilterChainProxy        : Securing GET /WEB-INF/view/home.jsp
DEBUG [requestURL=/] o.s.security.web.FilterChainProxy        : Secured GET /WEB-INF/view/home.jsp
Run Code Online (Sandbox Code Playgroud)

我在 Spring Security 迁移指南中看不到任何关于此的内容,有人知道发生了什么事吗?

更新

我将其隔离为一个干净的示例:

pom.xml

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>jsptest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jsptest</name>
    <packaging>war</packaging>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>

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

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

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        </dependencies>
    <build>
        <finalName>app</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    </project>
Run Code Online (Sandbox Code Playgroud)

应用程序.java

@SpringBootApplication
@Controller
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public SecurityFilterChain config(HttpSecurity http) throws Exception {
        http
                .authorizeHttpRequests((auth) -> auth
                        .requestMatchers("/", "/WEB-INF/view/**").permitAll()
                        .anyRequest().authenticated()
                );

        return http.build();
    }

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

}
Run Code Online (Sandbox Code Playgroud)

src/main/resources/application.properties:

spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
Run Code Online (Sandbox Code Playgroud)

src/main/webapp/WEB-INF/view/home.jsp:

hello
Run Code Online (Sandbox Code Playgroud)

sch*_*hup 6

我首先也忽略了一些事情。

在 Spring Security 6 中,转发和包含默认是安全过滤器的一部分。

请参阅使用 Spring MVC 时允许转发

允许全局转发可以通过安全配置中的这一附加行来完成。

.dispatcherTypeMatchers(DispatcherType.FORWARD).permitAll()
Run Code Online (Sandbox Code Playgroud)