2017-01-28 2 views
2

Je voulais ajouter un fichier CSS à mon fichier HTML. Le problème est apparu lorsque j'ai essayé d'ajouter CSS à l'application Spring Security (je travaille sur le contenu de base Spring Getting Started). Je blâme Spring Security car sans cela le fichier CSS se charge correctement.Ajouter un fichier CSS à Spring Boot + Spring Security Fichier Thymeleaf

fichier 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); 
    } 
} 

fichier 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"); 
    } 
} 

fichier 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"); 
    } 
} 

je charge CSS avec la ligne:

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

dans le fichier index.html.

Répondre

2

Votre modèle ../static/css ne correspond pas à votre URL relative ../static/css/index.css, voir AntPathMatcher:

PathMatcher mise en œuvre des modèles de chemin de style Ant.

Une partie de ce code de mapping a été aimablement empruntée à Apache Ant.

Les matches de mappage des URL en utilisant les règles suivantes:

  • ? correspond à un caractère
  • * correspond à zéro ou plusieurs caractères
  • ** correspond à zéro ou plusieurs répertoires dans un chemin
  • {spring:[a-z]+} correspond à la regexp [a-z]+ comme variable de chemin nommée "ressort"

et Spring Boot Reference:

Par défaut, les ressources sont mises en correspondance sur /** mais vous pouvez régler que par spring.mvc.static-path-pattern.

Votre demande sera redirigée vers le formulaire de connexion, car vous n'êtes pas connecté et toutes les autres demandes nécessitent une authentification.

Pour le réparer, changez votre modèle pour /css/** et /images/**.

Une meilleure solution pour les ressources statiques est WebSecurity#ignoring:

permet d'ajouter RequestMatcher instances de sécurité de printemps devrait ignorer. Web Security fourni par Spring Security (y compris le SecurityContext) ne sera pas disponible sur HttpServletRequest correspondant. Généralement, les demandes enregistrées doivent être uniquement des ressources statiques.Pour les demandes dynamiques, envisagez de mapper la requête pour autoriser tous les utilisateurs à la place.

Exemple d'utilisation:

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