How to set context path in spring boot using spring security

I set contextPath in application.properties as server.contextPath = / myWebApp in spring bootable spring security app, by default url as / login it does not set context path as / myWebApp and redirects back to me as / login not as / myWebApp / login. How to set contextPath using spring security 4.0? Since tomcat is giving warning as context [] .. not deploying app to container.

+3


source to share


1 answer


In Spring Boot, you can set the context path in three ways.

First in application.properties just like you.

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 ");

    }

}

      



, , :

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

      

Spring :

@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")
                ;

    }
}

      

- tomcat /myWebApp/login

0









All Articles