代码之家  ›  专栏  ›  技术社区  ›  Mariano L

使用Thymeleaf发送脚本标记中的安全参数

  •  0
  • Mariano L  · 技术社区  · 6 年前

    我试图在脚本标记中发送一个参数,如下所示:

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

    hasAuthority('用户')

    所以,我试过这个:

    <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')}">
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Alain Cruz    6 年前

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

    只有在您拥有正确权限的情况下,才会加载这个特定的js。对于您的情况,只需更改js参数的值即可。