C
C
Chvalov2017-11-20 04:29:23
Java
Chvalov, 2017-11-20 04:29:23

Error accessing static resources SpringBoot, where to look?

Spring boot 1.5.7
I get this picture:
5a122e99d88f1858780511.png

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
    }
}

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.
                authorizeRequests()
                .antMatchers("/").hasAnyRole("USER", "ADMIN")
                .antMatchers("/welcome").hasAnyRole("USER", "ADMIN")
                .antMatchers("/admin").hasRole("ADMIN")
                .and()
                .formLogin().loginPage("/login").defaultSuccessUrl("/welcome").failureUrl("/login?error")
                .usernameParameter("username").passwordParameter("password")
                .and().logout()
                .logoutSuccessUrl("/login?logout");
    }

Here's what's going on in the logs:
2017-11-20 03:22:36.740  WARN 11224 --- [nio-8080-exec-9] o.s.web.servlet.PageNotFound             : No mapping found for HTTP request with URI [/img/logo.png] in DispatcherServlet with name 'dispatcherServlet'
2017-11-20 03:22:36.740 DEBUG 11224 --- [nio-8080-exec-9] o.s.b.a.e.mvc.EndpointHandlerMapping     : Looking up handler method for path /error
2017-11-20 03:22:36.741 DEBUG 11224 --- [nio-8080-exec-9] o.s.b.a.e.mvc.EndpointHandlerMapping     : Did not find handler method for [/error]
2017-11-20 03:22:37.180 DEBUG 11224 --- [nio-8080-exec-4] o.s.b.a.e.mvc.EndpointHandlerMapping     : Looking up handler method for path /js/jquery.min.js
2017-11-20 03:22:37.182 DEBUG 11224 --- [nio-8080-exec-4] o.s.b.a.e.mvc.EndpointHandlerMapping     : Did not find handler method for [/js/jquery.min.js]
2017-11-20 03:22:37.182  WARN 11224 --- [nio-8080-exec-4] o.s.web.servlet.PageNotFound             : No mapping found for HTTP request with URI [/js/jquery.min.js] in DispatcherServlet with name 'dispatcherServlet'
2017-11-20 03:22:37.183 DEBUG 11224 --- [nio-8080-exec-4] o.s.b.a.e.mvc.EndpointHandlerMapping     : Looking up handler method for path /error
2017-11-20 03:22:37.184 DEBUG 11224 --- [nio-8080-exec-4] o.s.b.a.e.mvc.EndpointHandlerMapping     : Did not find handler method for [/error]
2017-11-20 03:22:37.274 DEBUG 11224 --- [io-8080-exec-10] o.s.b.a.e.mvc.EndpointHandlerMapping     : Looking up handler method for path /js/login.js
2017-11-20 03:22:37.276 DEBUG 11224 --- [io-8080-exec-10] o.s.b.a.e.mvc.EndpointHandlerMapping     : Did not find handler method for [/js/login.js]
2017-11-20 03:22:37.277  WARN 11224 --- [io-8080-exec-10] o.s.web.servlet.PageNotFound             : No mapping found for HTTP request with URI [/js/login.js] in DispatcherServlet with name 'dispatcherServlet'

5a122fcd58c79997474012.png
I connect resources like this:
<head>
    <title>Auth page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" type="text/css" />
    <link rel="stylesheet" th:href="@{/css/bootstrap-grid.min.css}" type="text/css" />
    <link rel="stylesheet" th:href="@{/css/bootstrap-reboot.min.css}" type="text/css" />
    <link rel="stylesheet" th:href="@{/css/main.css}" type="text/css" />
    <link rel="stylesheet" th:href="@{/css/font-awesome.min.css}" type="text/css" />
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700,800' rel='stylesheet' type='text/css'/>
</head>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2017-11-20
@zolt85

Spring tells you everything is true. Try linking to resources in html as "@{/resources/css/bootstrap.min.css}". Or add handlers differently

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/js/**")
                .addResourceLocations("/resources/js/");
        registry
                .addResourceHandler("/css/**")
                .addResourceLocations("/resources/css/");
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question