java 警告 osweb.servlet.PageNotFound - 未找到带有 URI 的 HTTP 请求的映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33480364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:44:03  来源:igfitidea点击:

WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI

javaspringjspspring-mvcspring-security

提问by krishna Ram

I am getting this error while adding Assets folder. It is giving error for every file which is included from "assets" folder.

添加 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'

Here is the Dispacther Config file

这是 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;
    }   
}

This is 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("/");
    }

}

This is security Config

这是安全配置

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; 
    }
}

Folder Structure source->main->webapp->WEB-INF->JSP->assets(This folder is not recognised) source->main->webapp->WEB-INF->JSP->homePage.jsp

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

From the Style or Icon is not coming in homePage.

从样式或图标未进入主页。

homePage.jsp

主页.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>

采纳答案by jny

You need to add support for static web resources.

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

To configure it to be managed by Spring see this question, for example.

例如,要将其配置为由 Spring 管理,请参阅此问题