【问题标题】:Sending security parameter in Script tag with Thymeleaf使用 Thymeleaf 在脚本标签中发送安全参数
【发布时间】:2019-03-15 23:16:40
【问题描述】:

我正在尝试在脚本标签中发送一个参数,如下所示:

<script th:src="@{/js/myScript.js(arg0=${value})}"></script>

其中 value 是以下结果:hasAuthority('USER')

所以,我尝试了这个:

<script th:src="@{/js/myScript.js(arg0=${hasAuthority('USER')})}"></script>

但这不起作用:

Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "hasAuthority('USER')" (template: "groups" - line 61, col 9)

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method hasAuthority(java.lang.String) cannot be found on org.thymeleaf.spring4.expression.SPELContextMapWrapper type

我之前用过这个函数来显示没有问题的 div:

<li sec:authorize="${hasAuthority('ADMIN')}">

【问题讨论】:

    标签: java spring-mvc thymeleaf


    【解决方案1】:

    试试这个。

    <script sec:authorize="${hasAuthority('USER')}" th:src="@{/js/myScript.js(arg0=ADD_VALUE_HERE)}"></script>
    

    如果你有正确的权限,这只会加载这个特定的 js。对于您的情况,只需将 js 参数的值更改为您需要的任何值。

    【讨论】:

    • 我的想法是使用 hasAuthority 发送真/假。这可能吗?
    • 遗憾的是没有,但是可以使用 Spring Security 的 Thymeleaf extras 实现非常相似的功能。这个问题可以帮助你。 stackoverflow.com/questions/35133062/…
    • 如果您需要有关 Thymeleaf Spring Security Extras 的更多帮助,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2016-05-30
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多