【发布时间】:2017-09-19 14:59:37
【问题描述】:
我正在重构一个 Spring-Boot 应用程序,但我遇到了
o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/rootProject/css/app.css]
适用于所有 CSS 和 JavaScript 文件。
项目的结构如下:
-
根项目
-
src
-
主要
java
-
资源
css
js
-
-
我的主要安全实现是:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
// other methods
@Override
protected void configure(HttpSecurity http) throws Exception {
// TODO: api must be protected but default alpaca connector does not support protected sources. We'll need to register a custom connector
http
.authorizeRequests()
.antMatchers(
"/images/**",
"/css/**",
"/health/**",
"/js/**",
"/api/**",
"/reports/**",
"/h2-console/**",
"/oauth/authorize",
"/oauth/confirm_access",
"/oauth/token_key").permitAll()
.antMatchers("/admin/**").hasAuthority(BaseRoleEnum.BASE_ADMIN.toString())
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login")
.and()
.headers()
.frameOptions()
.sameOrigin()
.and()
.csrf()
.disable();
}
}
而我的主要配置类是:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "basePackages")
// Enable caching for the application
//@EnableCaching
public class TouchConfiguration extends WebMvcConfigurerAdapter{
// other beans and methods
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
}
}
我在这里遗漏了什么,所以这可以按预期工作?
【问题讨论】:
-
为什么
.addResourceLocations("/static/");你提供的项目结构中没有这样的文件夹。还有/rootProject/css/app.css是确切的消息还是你替换了rootProject部分?你的 html css/js 链接是什么?像这样的东西?<link rel="stylesheet" type="text/css" href="/static/css/main.css">因为这就是你在.addResourceLocations("/static/");中所说的,但错误显示不同的链接 -
确切消息是:
WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/central/css/app.css] in DispatcherServlet with name 'dispatcherServlet' -
和文件夹结构是否正确?你有
resources/static/css还是只有resources/css? -
是的,
resources/static/css。 -
试试
registry.addResourceHandler("/central/**").addResourceLocations("/static/");
标签: java spring spring-mvc spring-security