7

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

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

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

From the Style or Icon is not coming in homePage.

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>
krishna Ram
  • 639
  • 3
  • 9
  • 20

1 Answers1

4

You need to add support for static web resources.

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

Community
  • 1
  • 1
jny
  • 8,007
  • 3
  • 37
  • 56
  • Hi, Thanks. I did through implemeting WebMvcConfigurer. I can't extend WebMvcConfigurerAdapter as I configured WebSecurityConfigurerAdapter. Your answer helped me. – krishna Ram Nov 02 '15 at 17:33