首页 > 解决方案 > 为什么在入口点 url 中添加项目名称时得到 404

问题描述

如果使用Spring security运行项目,入口点 URL 是:

http://localhost:8099/登录

同时我需要将全局项目名称放在入口点 URL 中,如下所示:

http://localhost:8099/pojoname/login

下面是我的 Spring Security 配置文件:

@Override
protected void configure(HttpSecurity http) throws Exception {
      http
        .csrf().disable()
        .authorizeRequests()
        .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
        .antMatchers("/login").permitAll()
        .antMatchers("/register", "/registration", "/editProfile").permitAll()
        .anyRequest().authenticated()
        .and()
        .formLogin()
            .loginPage("/login").permitAll()
            .usernameParameter("email")
        .and()
        .logout()
            .permitAll();
}

标签: spring-mvcspring-security

解决方案


请求的特性就是所谓的 web 上下文(或以前的Servlet容器工作中的Servlet上下文)。

这在Spring Boot中得到了开箱即用的支持,并且可以使用server.servlet.contextPath具有所需值的配置属性进行切换/激活。

在您的application.properties文件中,添加以下行:

  • 对于版本 < Spring Boot 2.0

      server.contextPath=/pojoname
    
  • 对于版本 > Spring Boot 2.0

      server.servlet.contextPath=/pojoname
    

推荐阅读