【发布时间】:2023-02-24 03:09:59
【问题描述】:
我正在将应用程序从 Spring Boot 2.7 升级到 Spring Boot 3,其中包括更新到 Spring Security 6。
我们设置了以下属性:
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
我们使用 JSP 作为模板语言,其中控制器返回视图文件名,例如
@RequestMapping("/")
public String home() {
return "home";
}
这将呈现 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()
);
}
自升级以来,访问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
我在 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>
应用.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";
}
}
src/main/resources/application.properties:
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
src/main/webapp/WEB-INF/view/home.jsp:
hello
【问题讨论】:
-
spring-boot默认情况下,可执行文件为jar,tomcat或jetty或undertow的容器不支持jsp。您必须进行一些特定的配置并使它们在 spring-boot 2.7 中工作,所以请分享它们以及那些需要迁移的配置 -
您的安全配置看起来不对。你覆盖了哪个
init方法?你应该有一个从HttpSecurity方法返回SecurityFilter的方法。 -
@M.Deinum 已经修复了配置,很抱歉它是从
AbstractHttpConfigurer类中为这篇文章提取的。 -
我仍然想念
@EnableWebSecurity...尽管如此,我怀疑安全是您的问题,但并非所有问题都在您的问题中,因此很难得到答案。
标签: spring-mvc spring-security