2017-01-28 40 views
2

我想将CSS文件添加到我的HTML文件中。 当我尝试向Spring Security应用程序添加CSS时(我正在处理基本的Spring入门内容)时,出现了这个问题。我指责Spring Security,因为没有它,CSS文件会正确加载。将CSS文件添加到Spring Boot + Spring Security Thymeleaf文件

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" /> 

in index.html file。

回答

2

你的模式../static/css不符合相对URL ../static/css/index.css,看到AntPathMatcher

PathMatcher实施Ant风格的路径模式。

此映射代码的一部分已经从Apache Ant中慷慨地借用过。

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

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

Spring Boot Reference

默认情况下,资源被映射到/**但你可以调整它通过spring.mvc.static-path-pattern

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

要修复它,请将图案更改为/css/**/images/**

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

允许添加RequestMatcher情况下是春季安全应该忽略。由Spring Security提供的网络安全(包括SecurityContext)将不会在HttpServletRequest上提供匹配。通常,注册的请求应该只有静态资源。对于动态请求,请考虑将请求映射到允许所有用户。

实例应用:

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