【问题标题】:Spring Security 6 and JSP view renderingSpring Security 6 和 JSP 视图渲染
【发布时间】: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 默认情况下,可执行文件为 jartomcatjettyundertow 的容器不支持 jsp。您必须进行一些特定的配置并使它们在 spring-boot 2.7 中工作,所以请分享它们以及那些需要迁移的配置
  • 您的安全配置看起来不对。你覆盖了哪个init方法?你应该有一个从 HttpSecurity 方法返回 SecurityFilter 的方法。
  • @M.Deinum 已经修复了配置,很抱歉它是从 AbstractHttpConfigurer 类中为这篇文章提取的。
  • 我仍然想念@EnableWebSecurity...尽管如此,我怀疑安全是您的问题,但并非所有问题都在您的问题中,因此很难得到答案。

标签: spring-mvc spring-security


【解决方案1】:

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

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

Permit FORWARD when using Spring MVC

可以通过安全配置中的这一附加行来实现全局转发。

.dispatcherTypeMatchers(DispatcherType.FORWARD).permitAll()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2011-10-20
    • 2015-06-12
    • 2016-06-15
    相关资源
    最近更新 更多