Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Webjars locator and context-path

I have Spring Boot application with enabled Spring Security configured like this:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home", "/webjars/**", "/css/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

Everything works fine until I change context-path. For example, if I set the it like this:

server:
   context-path: /myapp

I cant access my webjars, css... They are still looked at: http://localhost:8080/webjars/...

How can I configure Spring Webjars so that it works regardless of context-path?

like image 299
dplesa Avatar asked Mar 03 '16 09:03

dplesa


People also ask

What is WebJars locator?

A ResourceResolver that delegates to the chain to locate a resource and then attempts to find a matching versioned resource contained in a WebJar JAR file. This allows WebJars.org users to write version agnostic paths in their templates, like <script src="/jquery/jquery. min. js"/> .

What is org WebJars?

WebJars are client-side web libraries (e.g. jQuery & Bootstrap) packaged into JAR (Java Archive) files. Explicitly and easily manage the client-side dependencies in JVM-based web applications. Use JVM-based build tools (e.g. Maven, Gradle, sbt, ...) to download your client-side dependencies.

What is a Spring boot application?

Spring Boot is an open source Java-based framework used to create a micro Service. It is developed by Pivotal Team and is used to build stand-alone and production ready spring applications.

What is Java Spring used for?

The Spring Framework (Spring) is an open-source application framework that provides infrastructure support for developing Java applications. One of the most popular Java Enterprise Edition (Java EE) frameworks, Spring helps developers create high performing applications using plain old Java objects (POJOs).


1 Answers

OK, I solved the problem.

What I forget to do is request Thymeleaf engine to process relative paths of my CSS and js files. So, basically I changed this in my template:

<link rel="stylesheet" type="text/css" href="/webjars/bootstrap/css/bootstrap.min.css" />

to this:

<link rel="stylesheet" type="text/css" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}" />
like image 50
dplesa Avatar answered Oct 02 '22 18:10

dplesa