为什么我使用springMVC无法拦截根目录请求?总是使用默认的index.html

spring拦截 localhost:8080/1可以拦截到我的chat页面,而拦截根目录就拦截不到,回去访问默认的index页面;

控制台日志中有这么几行日志输出

2018-06-04 15:46:03.601  INFO 15836 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/login] onto handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
2018-06-04 15:46:03.601  INFO 15836 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Root mapping to handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
2018-06-04 15:46:03.601  INFO 15836 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/1] onto handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
2018-06-04 15:46:03.663  INFO 15836 --- [  restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page template: index

springMVC的配置

@Configuration
public class WebMvcConfg implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("/login");
        registry.addViewController("/").setViewName("/chat");
        registry.addViewController("/1").setViewName("/chat");
    }
}

spring security的配置

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                //开启默认登录页面
                .formLogin()
                //默认登录页面
                .loginPage("/login")
                //默认登录成功跳转页面
                .defaultSuccessUrl("/")
                .permitAll()
                .and()
                //设置注销
                .logout()
                .permitAll();
    }
   }
阅读 5.2k
2 个回答

index.html是springboot使用thymeleaf后的默认配置

新手上路,请多包涵

由于配置了thymeleaf后默认会去访问静态文件夹下的index.html页面,
因此在拦截时需要配置拦截index.html
registry.addViewController("/index.html").setViewName("login");
这样,在访问根目录时就会拦截index.html,进入login.html页面

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题