我是Spring的新手,我在Spring应用程序(基于Java的配置)中遇到了这个问题。
我的应用程序找不到静态资源(css文件,js,e.t.c) 我尝试了不同的事情,这个问题仍然存在。
我的WebAppConfig.java文件中有以下内容如下:
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("com.x.apimanager")
@PropertySource("classpath:application.properties")
@EnableJpaRepositories("com.x.apimanager.repository")
public class WebAppConfig extends WebMvcConfigurerAdapter {
// Other codes here that is not shown.
@Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
我的目录结构如下(图片可以在下面的链接中看到): http://pbrd.co/1EAR1II
我的Spring Security Config文件看起来像这样。我添加了一个拦截器,允许特定文件夹的请求通过。
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//Implementation not shown
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**");
}
}
这是在jsp页面/视图上发生此问题时显示的firebug。
"NetworkError: 404 Not Found - http://localhost:9999/apimanager/resources/libs/bootstrap-3.3.1/css/navbar.css"
navbar.css
我的jsp页面包含以下内容:
<c:set var="url">${pageContext.request.requestURL}</c:set>
<base href="${fn:substring(url, 0, fn:length(url) - fn:length(pageContext.request.requestURI))}${pageContext.request.contextPath}/" />
<link href="resources/libs/bootstrap-3.3.1/css/navbar.css" rel="stylesheet">
我的日志显示以下内容:
2015-01-22 12:07:39 DEBUG SecurityContextPersistenceFilter:97 - SecurityContextHolder now cleared, as request processing completed
2015-01-22 12:07:40 DEBUG FilterChainProxy:337 - /resources/libs/bootstrap-3.3.1/css/navbar.css at position 1 of 14 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
2015-01-22 12:07:40 DEBUG FilterChainProxy:337 - /resources/libs/bootstrap-3.3.1/css/navbar.css at position 2 of 14 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
2015-01-22 12:07:40 DEBUG HttpSessionSecurityContextRepository:171 - Obtained a valid SecurityContext from SPRING_SECURITY_CONTEXT: 'org.springframework.security.core.context.SecurityContextImpl@e18bbb85: Authentication: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@e18bbb85: Principal: org.springframework.security.core.userdetails.User@a5979349: Username: x; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_USER; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@12afc: RemoteIpAddress: 127.0.0.1; SessionId: 0102DF1734E5DCC343FF4C767FE3AD17; Granted Authorities: ROLE_USER'
2015-01-22 12:07:40 DEBUG FilterChainProxy:337 - /resources/libs/bootstrap-3.3.1/css/navbar.css at position 3 of 14 in additional filter chain; firing Filter: 'HeaderWriterFilter'
2015-01-22 12:07:40 DEBUG HstsHeaderWriter:129 - Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@528199b
2015-01-22 12:07:40 DEBUG FilterChainProxy:337 - /resources/libs/bootstrap-3.3.1/css/navbar.css at position 4 of 14 in additional filter chain; firing Filter: 'CsrfFilter'
2015-01-22 12:07:40 DEBUG FilterChainProxy:337 - /resources/libs/bootstrap-3.3.1/css/navbar.css at position 5 of 14 in additional filter chain; firing Filter: 'LogoutFilter'
2015-01-22 12:07:40 DEBUG AntPathRequestMatcher:127 - Request 'GET /resources/libs/bootstrap-3.3.1/css/navbar.css' doesn't match 'POST /logout
2015-01-22 12:07:40 DEBUG FilterChainProxy:337 - /resources/libs/bootstrap-3.3.1/css/navbar.css at position 6 of 14 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
2015-01-22 12:07:40 DEBUG AntPathRequestMatcher:127 - Request 'GET /resources/libs/bootstrap-3.3.1/css/navbar.css' doesn't match 'POST /login
2015-01-22 12:07:40 DEBUG FilterChainProxy:337 - /resources/libs/bootstrap-3.3.1/css/navbar.css at position 7 of 14 in additional filter chain; firing Filter: 'DefaultLoginPageGeneratingFilter'
2015-01-22 12:07:40 DEBUG FilterChainProxy:337 - /resources/libs/bootstrap-3.3.1/css/navbar.css at position 8 of 14 in additional filter chain; firing Filter: 'BasicAuthenticationFilter'
2015-01-22 12:07:40 DEBUG FilterChainProxy:337 - /resources/libs/bootstrap-3.3.1/css/navbar.css at position 9 of 14 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
2015-01-22 12:07:40 DEBUG FilterChainProxy:337 - /resources/libs/bootstrap-3.3.1/css/navbar.css at position 10 of 14 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
2015-01-22 12:07:40 DEBUG FilterChainProxy:337 - /resources/libs/bootstrap-3.3.1/css/navbar.css at position 11 of 14 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
2015-01-22 12:07:40 DEBUG AnonymousAuthenticationFilter:107 - SecurityContextHolder not populated with anonymous token, as it already contained: 'org.springframework.security.authentication.UsernamePasswordAuthenticationToken@e18bbb85: Principal: org.springframework.security.core.userdetails.User@a5979349: Username: x; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_USER; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@12afc: RemoteIpAddress: 127.0.0.1; SessionId: 0102DF1734E5DCC343FF4C767FE3AD17; Granted Authorities: ROLE_USER'
2015-01-22 12:07:40 DEBUG FilterChainProxy:337 - /resources/libs/bootstrap-3.3.1/css/navbar.css at position 12 of 14 in additional filter chain; firing Filter: 'SessionManagementFilter'
2015-01-22 12:07:40 DEBUG FilterChainProxy:337 - /resources/libs/bootstrap-3.3.1/css/navbar.css at position 13 of 14 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
2015-01-22 12:07:40 DEBUG FilterChainProxy:337 - /resources/libs/bootstrap-3.3.1/css/navbar.css at position 14 of 14 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
2015-01-22 12:07:40 DEBUG FilterSecurityInterceptor:194 - Secure object: FilterInvocation: URL: /resources/libs/bootstrap-3.3.1/css/navbar.css; Attributes: [authenticated]
2015-01-22 12:07:40 DEBUG FilterSecurityInterceptor:310 - Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@e18bbb85: Principal: org.springframework.security.core.userdetails.User@a5979349: Username: x; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_USER; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@12afc: RemoteIpAddress: 127.0.0.1; SessionId: 0102DF1734E5DCC343FF4C767FE3AD17; Granted Authorities: ROLE_USER
2015-01-22 12:07:40 DEBUG AffirmativeBased:65 - Voter: org.springframework.security.web.access.expression.WebExpressionVoter@49950c0d, returned: 1
2015-01-22 12:07:40 DEBUG FilterSecurityInterceptor:215 - Authorization successful
2015-01-22 12:07:40 DEBUG FilterSecurityInterceptor:227 - RunAsManager did not change Authentication object
2015-01-22 12:07:40 DEBUG FilterChainProxy:323 - /resources/libs/bootstrap-3.3.1/css/navbar.css reached end of additional filter chain; proceeding with original chain
2015-01-22 12:07:40 DEBUG DispatcherServlet:823 - DispatcherServlet with name 'dispatcher' processing GET request for [/apimanager/resources/libs/bootstrap-3.3.1/css/navbar.css]
2015-01-22 12:07:40 DEBUG RequestMappingHandlerMapping:226 - Looking up handler method for path /resources/libs/bootstrap-3.3.1/css/navbar.css
2015-01-22 12:07:40 DEBUG RequestMappingHandlerMapping:236 - Did not find handler method for [/resources/libs/bootstrap-3.3.1/css/navbar.css]
2015-01-22 12:07:40 WARN PageNotFound:1108 - No mapping found for HTTP request with URI [/apimanager/resources/libs/bootstrap-3.3.1/css/navbar.css] in DispatcherServlet with name 'dispatcher'
2015-01-22 12:07:40 DEBUG DispatcherServlet:966 - Successfully completed request
2015-01-22 12:07:40 DEBUG ExceptionTranslationFilter:115 - Chain processed normally
我将很感激这个问题的解决方案。非常感谢您回答我的问题。
答案 0 :(得分:0)
我希望您缺少的是应用程序上下文路径。
试试这个
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/navbar.css" />