2017-07-17 62 views
2

我將application.properties中的contextPath設置爲server.contextPath =/myWebApp在彈簧安全性的spring啓動應用程序中,默認網址爲/ login它沒有將上下文路徑設置爲/ myWebApp並且重定向回我/ /登錄而不是/ myWebApp /登錄。如何設置spring安全4.0的contextPath?由於tomcat發出警告作爲上下文[] ..不在容器中部署應用程序。如何設置彈簧安全的春季啓動的上下文路徑

+0

對這個問題有什麼建議?我有同樣的問題。 – RaHe

回答

0

在Spring Boot中,您可以設置上下文路徑3種方式。

首先在application.properties像你一樣。

server.contextPath=/myWebApp 

二的變化可以編程方式,以及:

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 

春季安全配置是:

@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