【问题标题】:upgrading spring & thyme leaf broke my authorize升级春天和百里香叶打破了我的授权
【发布时间】:2018-08-01 19:53:30
【问题描述】:

我正在将我的 Spring Boot 应用程序从 1.3.5 升级到 1.4.4(最终是 1.5.x),我注意到我的 Thymeleaf 菜单项现在已损坏。 这是我拥有它们的方式:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

...
<ul class="nav pull-right">
  <li sec:authorize="${!isAuthenticated()}">
     <div>
          <span></span>
     </div>
  </li>
  <li sec:authorize="${isAuthenticated()}">
     <span th:inline="text">Logged in as [[${#httpServletRequest.remoteUser}]</span>
   </li>
</ul>

然后我做了以下更改

compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:2.1.2.RELEASE')

compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '1.4.4.RELEASE'
compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity4', version: '3.0.2.RELEASE'
compile group: 'org.thymeleaf', name: 'thymeleaf-spring4', version: '3.0.9.RELEASE'

但是,授权元素的显示就像绕过了安全性一样。 sec:authorize 的工作方式是否有所改变?我查看了 Thymeleaf 文档,但没有看到。我知道被认证的用户具有适当的角色并且已经过认证。

更新

为了它的价值,我尝试将 bootstrap 和 jquery 版本更新为:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"/>
    <script src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

在我的 html 中有这个

xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"

但这仍然不能解决显示错误项目的问题。

【问题讨论】:

  • 您可以尝试使用此声明吗? xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
  • 这似乎没有什么区别 - 所有项目都在页面加载时显示。
  • 您确定不想使用isAnonymous()isAuthenticated() 吗?不确定这个具体问题,但这似乎对我有用(Spring 4 non-Boot)
  • 我尝试将 isAuthenticated() 切换为 isAnonymous(),但这并没有什么不同。这一直有效,直到我将 Spring 1.3.5 升级到 1.4.4,这意味着我还必须更改我的 Thymeleaf、gradle 依赖项等

标签: spring-boot thymeleaf


【解决方案1】:

尝试sec:authorize="isAuthenticated()" 而不是sec:authorize="${isAuthenticated()}"

Spring Boot 1.x 使用 thymeleaf 2,SB 2.x 使用 thymeleaf 3。

如果你想在 SB 1.x 中使用 thymeleaf 3,你需要做额外的配置:https://github.com/spring-projects/spring-boot/issues/4393

【讨论】:

  • 我最终这样做了:sec:authorize="${isAuthenticated()}" 我没有使用 thymeleaf:3.0.3.RELEASE 和 thymeleaf-spring4:3.0.3.RELEASE 我没有能够让 sec:authorize="isAuthenticated()" 完全适用于这个新版本。
猜你喜欢
  • 2015-12-09
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 2018-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多