【问题标题】:Add CSS file to Spring Boot + Spring Security Thymeleaf file将 CSS 文件添加到 Spring Boot + Spring Security Thymeleaf 文件中
【发布时间】:2017-06-14 01:57:08
【问题描述】:

我想将 CSS 文件添加到我的 HTML 文件中。 当我尝试将 CSS 添加到 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");
    }
}

我用 line 加载 CSS:

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

index.html 文件中。

【问题讨论】:

  • 请添加您的项目结构。它会帮助别人。谢谢。

标签: spring-boot spring-security stylesheet thymeleaf


【解决方案1】:

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

PathMatcher Ant 样式路径模式的实现。

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

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

  • ? 匹配一个字符
  • * 匹配零个或多个字符
  • ** 匹配路径中的零个或多个目录
  • {spring:[a-z]+} 匹配正则表达式 [a-z]+ 作为名为“spring”的路径变量

Spring Boot Reference:

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

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

要修复它,请将您的模式更改为 /css/**/images/**

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

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

示例用法:

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

【讨论】:

  • 这是代码 sn-p 也适用于我的子文件夹。谢谢@Dur http.authorizeRequests() .antMatchers(HttpMethod.GET, "/css/**", "/js/**").permitAll();
【解决方案2】:

web.ignore()对我来说效果最好。只需将以下方法添加到您的 WebSecurityConfig 类中即可。

@Override
public void configure(WebSecurity web) throws Exception {
    web
            .ignoring()
            .antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/img/**", "/icon/**");
}

【讨论】:

    【解决方案3】:
    .antMatchers("/**/*.js", "/**/*.css").permitAll();
    

    这允许资源/静态文件夹中存在的所有 js 和 css 文件被允许请求访问。

    【讨论】:

    • 非常感谢。这个问题我搜索了几天,终于找到了最好的解决方案!
    猜你喜欢
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 2021-01-17
    • 2017-05-26
    • 2017-06-21
    • 2019-02-10
    相关资源
    最近更新 更多