5

I set contextPath in application.properties as server.contextPath=/myWebApp in spring boot application with spring security, default url as /login it not setting context path as /myWebApp and redirect back me as /login not as /myWebApp/login. How to set contextPath with spring security 4.0 ? As tomcat give warning as context []..not deploying app in container.

1
  • 1
    Any suggestions on this problem? I have the same issue. Commented Oct 5, 2017 at 16:17

2 Answers 2

2

I solved this issue by setting server.servlet.context-path in Application.properties file

Code Example

Application.properties

server.servlet.context-path=/myWebApp/


with this, you can also achieve a login page as you want http://localhost:8080/myWebApp/login


Sign up to request clarification or add additional context in comments.

1 Comment

Correct way to change context path in Spring boot 2
1

In Spring Boot you can set context path 3 ways.

First in application.properties like as you do.

server.contextPath=/myWebApp 

Second the change can be done programmatically as well:

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Component;

@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {

        container.setPort(8181);
        container.setContextPath("/myWebApp ");

    }

}

And Finally, by passing the system properties directly:

java -jar -Dserver.contextPath=/myWebApp spring-boot-example-1.0.jar

Spring Security Config is:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/static/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .usernameParameter("username")
                .passwordParameter("password")
                .loginProcessingUrl("/j_spring_security_check")
                .failureUrl("/login?error=true")
                .defaultSuccessUrl("/index")
                .and()
                .logout().logoutUrl("/logout").logoutSuccessUrl("/login")
                ;

    }
}

And without any further changes,tomcat will start automatically /myWebApp/login

1 Comment

what if we are deploying on external container. What class to use instead of EmbeddedServletContainerCustomizer ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.