【问题标题】:Spring Thymeleaf #lists.contains not working correctlySpring Thymeleaf #lists.contains 无法正常工作
【发布时间】:2017-08-13 19:43:25
【问题描述】:

我的最终目标是用当前用户没有的角色填充下拉列表。

<p th:text="${#bools.listIsTrue(Item.granted_authorities)}"></p>

打印出[true, true, true]

<p th:text="${Item.granted_authorities}"></p>

打印出[Ljava.lang.Object;@63a6ff07

<p th:text="${#lists.toList(Item.granted_authorities)}"></p>

打印出[ROLE_ADMIN, ROLE_SUPERADMIN, ROLE_USER]

<div th:with="RoleList=${#lists.toList(activeSessionsItem.granted_authorities)}">
<p th:text="${#lists.contains(RoleList, RoleList[0])}"></p>
</div>

打印出true

<div th:with="RoleList=${#lists.toList(activeSessionsItem.granted_authorities)}">
<p th:text="${#lists.contains(RoleList, 'ROLE_USER')}"></p>
</div>

打印出false

【问题讨论】:

  • 我猜你的RoleListGrantedAuthoritys 的列表,不是Strings?
  • 通过所有用户的 SessionRegistry 填充的对象 userSettings.setGranted_authorities(((User) principal_item).getAuthorities().toArray());
  • 您的RoleList 不包含任何字符串,因此#lists.contains 将失败。考虑使用 &lt;sec:authorizehasRole 代替。
  • 感谢您的快速回复。这不适用于当前登录的用户。
  • 在这种情况下,考虑创建一个服务来为你做这件事并使用@beanName.method(...) 语法?

标签: spring spring-security thymeleaf


【解决方案1】:
<select th:with="myAuthoritiesList=${myAuthorities}" >
    <option 
        th:each="r : ${allAuthorities}" 
        th:if="${not #lists.contains(myAuthoritiesList, r)}"
        th:value="${r.authority}" 
        th:text="${r.authority}">
    </option>
</select>




@RequestMapping("/home")
public String home(Model m) {
    GrantedAuthority authAdmin = new SimpleGrantedAuthority("ROLE_ADM");
    GrantedAuthority authUser = new SimpleGrantedAuthority("ROLE_USR");

    List<GrantedAuthority> allAuths = new ArrayList<>() ;
    allAuths.add(authAdmin);
    allAuths.add(authUser);
    m.addAttribute("allAuthorities", allAuths);

    List<GrantedAuthority> myAuths = new ArrayList<>() ;
    myAuths.add(authUser);
    m.addAttribute("myAuthorities", myAuths);

    return "home";
}

结果:只有选择ROLE_ADM的选择

【讨论】:

    【解决方案2】:

    我必须将 user_roles 转换为字符串。

    <select class="form-control form-big-letters" th:field="*{AllRoles}">
       <option th:each="itemRole, iStat : ${AllRoles}"
       th:if="${not #strings.contains(#strings.toString(#lists.toList(activeSessionsItem.granted_authorities)),#strings.toString(itemRole.role_list_role))}"            th:value="${itemRole.role_list_role}" th:text="${#strings.substringAfter(itemRole.role_list_role,'ROLE_')}">Roles
    </option></select>
    

    【讨论】:

      【解决方案3】:

      RoleList 是 GrantedAuthority 的列表。

      contains() 总是返回 false,因为 GrantedAuthority 对象从不等于字符串。

      要填充您的下拉列表,您可以使用此代码,但您需要在模型之前添加availableAuthorities

      <select>
          <option 
             th:each="r : ${availableAuthorities}"
             th:value="${r.authority}" 
             th:text="${r.authority}">
          </option>
      </select>
      

      【讨论】:

      • 所以在您看来,没有办法添加类似 th:if="${not #lists.contains(myAuthorities, r.authority)}"
      • 使用此解决方案的 HTML 更简单。
      猜你喜欢
      • 2022-01-22
      • 2017-03-09
      • 2018-02-24
      • 2016-06-25
      • 2018-10-29
      • 2017-04-29
      • 1970-01-01
      • 2017-08-26
      • 2016-03-22
      相关资源
      最近更新 更多