警告o.s.web.servlet.PageNotFound - 找不到带URI的HTTP请求的映射

时间:2015-11-02 14:50:37

标签: java spring jsp spring-mvc spring-security

添加Assets文件夹时出现此错误。 它为“assets”文件夹中包含的每个文件提供错误。

WARN  o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/assets/plugins/datepicker/datepicker3.css] in DispatcherServlet with name 'dispatcher'

这是Dispacther配置文件

package com.springmaven.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


@Configuration
@EnableWebMvc
@ComponentScan({"com.springmaven.controller"})
public class DispatcherConfig {

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver()
    {
        InternalResourceViewResolver internalResourceViewResolver=new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/WEB-INF/JSP/");
        internalResourceViewResolver.setSuffix(".jsp");
        return internalResourceViewResolver;
    }   
}

这是App Config

package com.springmaven.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class AppIntializer implements WebApplicationInitializer{


    @Autowired
    public void onStartup(ServletContext servletCon) throws ServletException {
        // TODO Auto-generated method stub
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(ApplicationConfig.class);
        servletCon.addListener(new ContextLoaderListener(rootContext));

        AnnotationConfigWebApplicationContext servletConfig = new AnnotationConfigWebApplicationContext();
        servletConfig.register(DispatcherConfig.class);

        ServletRegistration.Dynamic registration = servletCon.addServlet("dispatcher", new DispatcherServlet(servletConfig));
        registration.setLoadOnStartup(1);
        registration.addMapping("/");
    }

}

这是安全配置

package com.springmaven.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension;
import org.springframework.security.web.csrf.CsrfTokenRepository;
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;


@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{



    @Autowired
    private AuthenticationProvider customAuthenticationProvider; 

    @Autowired
    CustomSuccessHandler customSuccessHandler;

      @Override
      public void configure(WebSecurity web) throws Exception {
        web
          .ignoring()
             .antMatchers("/assets/**"); 
      }

    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
         auth
         .authenticationProvider(customAuthenticationProvider);
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests().antMatchers("/assets/**").permitAll()
        .and()
           .formLogin().loginPage("/loginPage")
                .defaultSuccessUrl("/homePage")
                .failureUrl("/loginPage?error")
                .usernameParameter("username").passwordParameter("password")     
                .and().csrf().csrfTokenRepository(csrfTokenRepository())
            .and()
                .logout().logoutSuccessUrl("/loginPage?logout"); 

    }

    @Bean
    public SecurityEvaluationContextExtension securityEvaluationContextExtension() {
        return new SecurityEvaluationContextExtension();
    }


    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
     return super.authenticationManagerBean();
    }

    private CsrfTokenRepository csrfTokenRepository() 
    { 
        HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); 
        repository.setSessionAttributeName("_csrf");
        return repository; 
    }
}

文件夹结构 source-> main-> webapp-> WEB-INF-> JSP-> assets(无法识别此文件夹) 源 - > main-> webapp-> WEB-INF-> JSP-> homePage.jsp

来自样式或图标不会出现在主页中。

homePage.jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>New Member</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.5 -->
  <!--Favicon Image -->
  <link rel="shortcut icon" href="assets/dist/img/favicon.ico"/>
  <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css"/>
   <link rel="stylesheet" href="assets/plugins/datepicker/datepicker3.css">
 </head>   
 <body>
    Welcome,
      <a href="<c:url value="/logout" var="Signout" />" class="btn btn-default btn-flat"></a>

                <form id="logout" action="${Signout}" method="post" >
                  <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
                </form>
                <c:if test="${pageContext.request.userPrincipal.name != null}">
                    <a href="javascript:document.getElementById('logout').submit()">Logout</a>
                </c:if>
 </body>
 </html>

1 个答案:

答案 0 :(得分:3)

您需要添加对静态网络资源的支持。

要将其配置为由Spring管理,请参阅this question