【问题标题】:How to make thymeleaf spring security namespace available?如何使百里香弹簧安全命名空间可用?
【发布时间】:2015-12-31 06:17:59
【问题描述】:

我有一个 Spring Boot 应用程序。据我了解,启动应用程序只需要 pom 中的依赖项,一切都很好。不幸的是,情况并非如此,即使我的配置过于复杂,它仍然无法工作 - 我不能在我的页面中使用 sec 命名空间。

在我的页面中,第一个问题是命名空间 URI:

我已经尝试了 Intellij 修复菜单中的所有可用选项,但无法获得。

我想这个问题的结果是我不能在任何地方使用sec 命名空间。图片中的示例可能确实是无效的使用,但我也使用了 <div>,这直接来自 Thymeleaf 示例:

这里和其他来源的许多答案也依赖于 xml 配置,这是没有用的。尽管如此,我还是根据这些 xml 示例制作了基于 Java 的 bean,但没有成功。

仅使用 基于 Java 的配置(如果有的话)在 Spring Boot 应用程序中使用 Spring Security 和 thymeleaf 集成需要哪些步骤?

pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>
    ...
</dependencies>

【问题讨论】:

  • 从您的问题中不清楚这是否只是您的 IDE 中的一个问题,或者它是否在运行时失败。如果它在运行时确实失败了,那么有关该失败的更多详细信息可能有助于找出问题所在。
  • 此外,您的第一个屏幕截图表明您需要配置 IDEA 以了解架构,并且您可以在设置 > 项目设置 > 架构和 DTD 中执行此操作。你试过这样做吗?
  • @AndyWilkinson,我不认为这是 IDE,尽管 Thymeleaf 与 Intellij 的集成不是很好。它在 Eclipse STS 中的运行时也不能正常运行,但不显示任何错误。是的,“我已经尝试了 Intellij 修复菜单中的所有可用选项,但无法获得它。”我还关注了足够多的教程和博客,让我疯狂地尝试用无数的配置来修复它。主要的是我正在使用 Boot 并且这些都不是 supposed 必需的。我实际上认为我找到了一种不使用命名空间的解决方法。
  • 您使用的是什么版本的 Boot?我可以看到您正在使用 Thymeleaf 的 Spring Security 4 集成。要开箱即用,这需要 Spring Boot 1.3。如果您使用的是 Boot 1.2,则应使用 Thymeleaf 的 Spring Security 3 集成。
  • @AndyWilkinson,使用 1.2.6。屏幕截图只是另一个失败的尝试。不幸的是,使用 3 会给出完全相同的错误和结果。我从 Rob Winch 的演讲中得到了解决方法,不管怎样,这似乎更干净一些,也让我为更好的安全实践做好了准备。我稍后会做一些测试,以确保它在其他地方也能正常工作,如果是这样,请删除这篇文章。令人沮丧的是它只适用于 spring 安全标签库,这使得迁移到 thymeleaf 并不有趣。

标签: java maven spring-mvc spring-boot thymeleaf


【解决方案1】:

我遇到了同样的问题,对我来说,这样定义架构位置很有帮助:

<html lang="en"
  xmlns:th="http://www.thymeleaf.org"
  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
  xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.thymeleaf.org http://www.thymeleaf.org
  http://www.ultraq.net.nz/thymeleaf/layout http://www.ultraq.net.nz/thymeleaf/layout
  http://www.thymeleaf.org/thymeleaf-extras-springsecurity4 http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

...

这还包括用于布局和 spring security 4 的其他方言,如果您不使用它们,您可以将其删除。

【讨论】:

    猜你喜欢
    • 2016-09-17
    • 2018-11-24
    • 2017-07-20
    • 2021-12-11
    • 2014-05-13
    • 2018-11-20
    • 2014-06-25
    • 2018-04-03
    相关资源
    最近更新 更多