如何连接CSS到弹簧启动?



我的静态文件夹位于资源中。我使用下面的链接,但它不适合我怎么办?

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

如果你的css存储在css文件夹中,这是正确的语法:

<link th:href="@{/css/style.css}" rel="stylesheet" type="text/css">

你发布的看起来是正确的,所以假设你所有的文件都在他们应该在的地方,问题可能来自spring安全

如果您将spring security添加为依赖项,则需要告诉spring security不需要对静态文件进行身份验证。

你可以创建一个WebSecurityConfig类来扩展WebSecurityConfigurerAdapter,并实现以下内容:

protected void configure(HttpSecurity http) throws Exception {
String [] staticResources = {
"/css/**",
"/js/**"
};
http
.authorizeRequests()
.antMatchers(staticResources).permitAll()
}

Spring自动查找静态文件夹中的staticResources数组应该包含,嗯…所有静态资源。如果您添加了一个图像文件夹,您还应该将其添加到staticResources数组中,如:/images/**,如果您希望能够在所有页面中使用图像

剩余代码:

Hey http, I need you to authorizeRequests matching the contents of staticResources. 
I also would like to permitAll users to access the content of staticResources.

最新更新