直到最近,我的Spring MVC应用程序都可以正常工作。但是现在主页无法加载静态资源,我看到以下错误
Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@bc10afd2: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS
No mapping for GET /css/style.css
No mapping for GET /images/logo.png
No mapping for GET /css/home.css
org.springframework.web.servlet.FrameworkServlet[0;39m: Completed 404 NOT_FOUND
前端是HTML。 这是很长一段时间的工作代码,我怀疑添加大张旗鼓会破坏加载静态图像。从日志中,我可以推断出它正在正确地搜索路径,但由于某种原因(如下所示)却无法使用
org.springframework.security.web.util.matcher.AntPathRequestMatcher Checking match of request : '/css/home.css'; against '/css/**'
org.springframework.security.web.util.matcher.AntPathRequestMatcher [0;39m: Checking match of request : '/css/style.css'; against '/css/**'
org.springframework.security.web.util.matcher.AntPathRequestMatcher[0;39m: Checking match of request : '/images/logo.png'; against '/images/**'
下面是安全配置的代码
@Configuration
@EnableWebSecurity
public class SecurityConfigurer extends WebSecurityConfigurerAdapter {
//...
@Override
public void configure(WebSecurity webSecurity) throws Exception {
webSecurity
.ignoring()
.antMatchers(
"/assets/**",
"/css/**",
"/dis/**",
"/fonts/**",
"/images/**",
"/js/**",
"/resources/**",
"/static/**",
"/v2/api-docs",
"/v2/api-docs/**",
"/v2/api-docs/swagger-ui.html",
"/swagger-resources",
"/swagger-resources/**",
"/configuration/ui",
"/configuration/**",
"/configuration/security",
"/swagger-ui.html",
"/webjars/springfox-swagger-ui/**",
"/webjars/**"
);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
String login = "/login";
http
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers(login).permitAll()
.antMatchers("/forgot").permitAll()
.antMatchers("/admin/**").hasAuthority("ROLE_ADMIN")
.antMatchers("/actuator/**").permitAll()
.anyRequest().authenticated()
.and().csrf().disable()
.formLogin().loginPage(login).loginPage("/")
.defaultSuccessUrl("/default")
}
}
这是大张旗鼓的配置
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.xyz.abc.controller"))
.paths(PathSelectors.any()).build();
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
这里类似于discussion,但是我使用的是MVC的当前版本,因此我的代码中没有基于XML的配置