我想将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");
}
}
我用行加载CSS:
<link href="../static/css/index.css" th:href="@{/css/index.css}" rel="stylesheet" />
在index.html
文件中。
您的模式../static/css
与您的相对网址../static/css/index.css
不匹配,请参阅AntPathMatcher:
PathMatcher
实现Ant风格的路径模式。部分映射代码已经从Apache Ant中借用。
映射使用以下规则匹配URL:
?
匹配一个字符*
匹配零个或多个字符**
匹配路径中的零个或多个目录{spring:[a-z]+}
将regexp[a-z]+
作为名为“spring”的路径变量进行匹配
默认情况下,资源映射到
/**
,但您可以通过spring.mvc.static-path-pattern
进行调整。
您的请求将被重定向到登录表单,因为您没有登录,所有其他请求都需要身份验证。
要解决此问题,请将您的模式更改为/css/**
和/images/**
。
更好的静态资源解决方案是WebSecurity#ignoring:
允许添加Spring Security应忽略的
RequestMatcher
实例。 Spring Security提供的网络安全(包括SecurityContext
)将无法在匹配的HttpServletRequest
上获得。通常,注册的请求应该只是静态资源的请求。对于动态请求,请考虑将请求映射为允许所有用户。用法示例:
webSecurityBuilder.ignoring() // ignore all URLs that start with /resources/ or /static/ .antMatchers("/resources/**", "/static/**");
.antMatchers("/**/*.js", "/**/*.css").permitAll();
这允许允许资源/静态文件夹中存在的所有js和css文件进行请求访问。