【问题标题】:Spring Boot URL mapping for static HTML静态 HTML 的 Spring Boot URL 映射
【发布时间】:2016-09-26 16:02:43
【问题描述】:

我的 Spring Boot 应用程序基本上由两个主要“模块”组成:

  • 由静态 HTML 页面组成的“Web”模块,可供公众(未经身份验证/匿名用户)使用;和
  • 由多个动态页面组成的“App”模块,每个页面都需要(基于 Spring Security)身份验证才能访问

基本的应用结构是:

  • index.html:主页,映射自http://localhost:8080/
  • about.html:关于页面,映射自http://localhost:8080/about
  • contact.html:联系页面,映射自http://localhost:8080/contact
  • login.html:登录页面,映射自http://localhost:8080/login
  • dashboard.html:登录后进入的仪表板/登录页面,映射自 http://localhost:8080/account
  • http://localhost:8080/account/* 下的所有其他页面将是具有典型 @RequestMapping 映射的 MVC/动态页面

我不清楚的是,对于静态(“公共网络”)HTML 页面,我是否:

  • 只需使用基于标准控制器的@RequestMappings 来呈现一些 Thymeleaf/Handlebars 模板(由于内容是静态的,因此没有数据模型)?还是我:
  • 将这些页面视为静态内容(与 CSS/JS 相同)并将它们作为static content as Spring Boot prescribes 提供服务?如果这是正确的选项,那么我如何实现正确的 URL 映射(例如,用户可以转到 http://localhost:8080/contact 而不是 http://localhost:8080/contact.html)?

【问题讨论】:

  • 你在认证中使用了什么技术?
  • 感谢 @KamelMili (+1) - Spring Security,可能带有自定义身份验证/授权领域。
  • 如果您喜欢我的解决方案,spring security 可以使用 web 忽略或 ant matcher 处理您的问题使这些页面与 thymeleaf 一起使用,因为在这些静态网页中,有一个由 thymeleaf 和 spring security 处理的身份验证表单,所以如果我正确理解了您的问题,您需要将它们添加到您的 mvc 解析器并将 web 忽略添加到这些页面,这样您就可以无需认证即可访问
  • 再次感谢@KamelMili (+1) - 是的,如果您发布的答案显示了如何使用 spring 安全性、这个 mvc 解析器和 web 忽略的具体代码示例,我会欣然接受!再次感谢!

标签: spring-boot static-content


【解决方案1】:

首先你需要像这样通过 mvc 解析器映射你的视图:

@Configuration
public class WebMVCconfig extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/login").setViewName("login");
    registry.addViewController("/contact").setViewName("contact");

   }
} 

映射您的视图后,我们需要在antMatchers 中添加像/login 这样的视图,所以我假设您的配置方法如下所示:

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

    .csrf().disable()   

    .authorizeRequests()
    .antMatchers("/contact","/about", ...).permitAll() //add your static pages here ( public pages ) 
    .anyRequest()
        .authenticated()        
            .and()
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .successHandler(successHandler) // i don't think you'll be needing this it's to redirect the user if it's admin or a simple user 
            .and()  // this is for the logout   
            .logout()
            .invalidateHttpSession(true)
            .logoutUrl("/logout")
            .permitAll();   
}

antMatchers 中添加您的视图地图后,spring security 不会处理这些页面的身份验证,这可以解决您的问题

【讨论】:

  • 非常感谢@Kamel Mili (+1) - 如果您不介意的话,可以问一个简单的问题:registry.addViewController("/login") 返回一个 ViewControllerRegistration 实例,其 setViewName(...) 方法需要一个字符串(例如"login" 等)。此视图名称表示什么类型的文件(HTML 文件、Thymeleaf 模板等),我应该将此文件放在我的项目中的什么位置?换句话说:什么文件定义了"login" 视图?再次感谢!
  • 这是一个简单的 html 页面,以 <html xlmns:th="http://www.thymeleaf.org" xml:lang="fr"> 开头,假设您确实在 pom.xml 中添加了 thymeleaf,并且您将页面放在模板文件夹下,以便 thymleaf 可以映射到您的视图
  • 感谢@Kamel Mili (+1),但 在哪里 是位于我的项目中的“模板文件夹”,在src/main/resources 下? Spring 需要知道在哪里可以找到 login 视图……那么它(确切地)在哪里寻找?再次感谢!
  • 它在 src\main\resources\ ps 你确定你在 pom.xml 中添加了你的 thymeleaf 依赖项
  • 模板文件夹假设在您的项目中并且不添加您的项目类型是否为spring starter project
猜你喜欢
  • 2016-12-24
  • 1970-01-01
  • 2020-07-21
  • 1970-01-01
  • 1970-01-01
  • 2017-02-21
  • 2016-03-21
  • 2014-04-13
  • 1970-01-01
相关资源
最近更新 更多