繁体   English   中英

SpringBoot Thymeleaf 登录重定向到索引加载 html 但不是 css 和 js

[英]SpringBoot Thymeleaf login redirect to index loads html but not css and js

我有一个 springboot 应用程序,我在其中验证用户,如果他登录,我将他重定向到 index.html。 然而,这个索引页面只加载了plain.html,根本没有js或css。 我在服务器错误日志和浏览器控制台中都没有看到任何错误。 我尝试在我的 css 文件上禁用 spring 安全性无效。

这是我的项目结构:

  • 资源
    • static
      • css_general
      • css_page_specific
      • 登录.html
      • 索引.html
      • commons.js

这是我的 application.properties 配置。 我已将 thymeleaf 的默认路径指向 static 文件夹,这样我至少可以先运行它。

spring.thymeleaf.prefix=classpath:/static/

我在 static 的东西上禁用了 spring 安全性。 这是我的 WebSecurityConfig.java

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.requiresChannel().anyRequest().requiresSecure().and().authorizeRequests().antMatchers("/login","/css_general/**","/css_page_specific/**","/**/*.js","/**/*.css")
        .permitAll()
        .anyRequest()
        .authenticated()
        .and().formLogin(form -> form
            .loginPage("/login")
            .defaultSuccessUrl("/index.html")
            .failureUrl("/login?error=true")
        )
        .sessionManagement().invalidSessionUrl("/login")
        .and()
        .httpBasic()
        .and()
        .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login")
        .permitAll()
        .and()
        .cors().disable()
        .csrf().disable();
}

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/css_general/**", "/css_page_specific/**","/resources/static/css_general/**","/resources/static/css_page_specific/**","/static/css_page_specific/**","/static/css_general/**");
}

这是我的 LoginController.java

    @Controller
public class LoginController {

    @GetMapping("/login")
    public String showLoginForm(Model model) {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || authentication instanceof AnonymousAuthenticationToken) {
            return "login";
        }

        return "redirect:/";
    }

    @GetMapping("/logout")
    public String logoutPage(HttpServletRequest request, HttpServletResponse response) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null){
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }
        return "redirect:/";
    }
} 

我的 index.html 上的导入文件:

    <html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head id="Head1" >
<title>Home</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE11"/>
 <link rel="stylesheet" th:href="@{css_page_specific/index.css}">
<link rel="stylesheet" th:href="@{css_general/toggle-button-modern.css}">
<link rel="stylesheet" th:href="@{css_general/data-table-modern.css}">
<link rel="stylesheet" th:href="@{css_general/combobox-modern.css}">
<link rel="stylesheet" th:href="@{css_general/dropdown-modern.css}">
<link rel="stylesheet" th:href="@{css_general/radio-modern.css}">
<link rel="stylesheet" th:href="@{css_general/grid-modern.css}">
<link rel="stylesheet" th:href="@{css_general/input-modern.css}">
<link rel="stylesheet" th:href="@{css_general/button-modern.css}">

 <script type="text/javascript" th:src="@{commons.js}"></script>
<script type="text/javascript" th:src="@{js_page_specific/index.js}"></script>
</head>

但是,在成功登录时,我只看到 index.html 的 html 并且根本没有加载 css 或 js。 控制台也没有错误。

在此处输入图像描述

我确实看到了来自登录的 302 重定向和索引上的 200。

在此处输入图像描述

此外,如果我在浏览器上看到 index.html 的元素选项卡,thymeleaf 并没有真正解决 href 标签。 在此处输入图像描述

如果在我的浏览器上,我向https://localhost:8443/index.css发出请求,它只会按原样喷出整个 css。

在此处输入图像描述

我查看了 stackoverflow 上的所有答案,但没有一个解决方案适合我。 将不胜感激任何帮助和指导。

发生这种情况的原因是因为您将模板存储在/static中(这使您可以访问这些文件,而无需通过 Thymeleaf 的常规解析和呈现过程运行它们)。 访问/index.html将文件返回为 static html。 为了解决这个问题,您需要:

  1. 通过 Thymeleaf 渲染器创建另一个为index.html提供服务的 controller(或向您的登录控制器添加另一种方法)。

     @Controller public class IndexController { @GetMapping("/index") public String showIndex(Model model) { return "index"; } }
  2. 将您的登录更改为重定向到/index而不是/index.html

     .and().formLogin(form -> form.loginPage("/login").defaultSuccessUrl("/index").failureUrl("/login?error=true") )

(我建议将您的模板放入一个单独的文件夹中 - 通常这是在类路径中的某个位置 - 因为您现在拥有它的方式,您让用户能够查看可能暴露安全漏洞的源代码或模板)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM