小编典典

将CSS文件添加到Spring Boot + Spring Security Thymeleaf文件

spring-boot

我想将CSS文件添加到HTML文件中。当我尝试将CS​​S添加到Spring
Security应用程序时出现了问题(我处理的是基本的Spring入门内容)。我责怪Spring Security,因为没有它,CSS文件将无法正确加载。

Application.java 文件:

package mainpack;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

MvcConfig.java 文件:

package mainpack;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/hello").setViewName("hello");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/register").setViewName("register");
        registry.addViewController("/whatever").setViewName("whatever");
    }
}

WebSecurityConfig.java 文件:

package mainpack;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home", "/index", "/register", "../static/css", "../static/images").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
}

我用行加载CSS:

<link href="../static/css/index.css" th:href="@{/css/index.css}" rel="stylesheet" />

index.html文件中。


阅读 404

收藏
2020-05-30

共1个答案

小编典典

您的模式../static/css与您的相对URL不匹配../static/css/index.css,请参见AntPathMatcher

PathMatcher Ant-style 的路径模式的实现。

此映射代码的一部分已从Apache Ant借来。

映射使用以下规则匹配URL:

  • ? matches one character
  • * matches zero or more characters
  • ** matches zero or more directories in a path
  • {spring:[a-z]+}将正则表达式匹配[a-z]+为名为“ spring”的路径变量

Spring Boot参考

默认情况下,资源映射在上,/**但是您可以通过进行调整spring.mvc.static-path-pattern

您的请求将被重定向到登录表单,因为您尚未登录并且所有其他请求都需要身份验证。

要解决此问题,请将模式更改为/css/**/images/**

一个更好的静态资源解决方案是WebSecurity#ignoring

允许添加RequestMatcherSpring Security应该忽略的实例。Spring Security提供的Web
Security(包括SecurityContext)在HttpServletRequest该匹配项上不可用。通常,注册的请求应该仅是静态资源的请求。对于动态请求,请考虑映射请求以允许所有用户使用。

用法示例:

 webSecurityBuilder.ignoring()
 // ignore all URLs that start with /resources/ or /static/
                .antMatchers("/resources/**", "/static/**");
2020-05-30