2017-07-17 75 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