【问题标题】:Springboot/Thymeleaf check current user from model on every pageSpringboot/Thymeleaf 从每个页面的模型中检查当前用户
【发布时间】:2017-06-26 16:53:17
【问题描述】:

我正在尝试为每个页面提供以下模型,以便我可以检查当前用户是否已登录,从而控制每个页面的标题菜单项,但我不想总是返回 index.html。有没有办法让自己回归?

@RequestMapping(value = "/**", method = RequestMethod.GET)
String index(Model model) {
 Users user = usersRepository.findOne(1 L);

 if (user != null) {
  String currentRole = user.getRole().toString();
  model.addAttribute("currentRole", currentRole);
  // System.out.println("Current Role: " + currentRole);
 } else {
  model.addAttribute("currentRole", "ANONYMOUS");
 }

 return "index";
}

我的菜单:

<li th:switch="${currentRole}">
   <!-- Is Employee, so show "My Requests" -->
   <div th:case="EMPLOYEE">
      <a href="#" th:href="@{/requests}">
         <div style="float: left; margin-right: 10px;">My Requests</div>
         <!-- Handle notifications here -->
         <div style="overflow: hidden" id="circle">1</div>
      </a>
   </div>
   <!-- Is Manager, so show "My Approvals" -->
   <div th:case="MANAGER">
      <a href="#" th:href="@{/requests}">
         <div style="float: left; margin-right: 10px;">My Approvals</div>
         <!-- Handle notifications here -->
         <div style="overflow: hidden" id="circle">1</div>
      </a>
   </div>
</li>
<li><a href="#" th:href="@{/team}">My Team</a></li>
<!-- Is logged in, so don't show "Log In" -->
<li th:unless="${currentRole}">
   <a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</li>
</ul>

【问题讨论】:

    标签: java html spring spring-boot thymeleaf


    【解决方案1】:

    当您使用Spring 时,您可以创建@ControllerAdvice 类并在那里放置所需的逻辑,类似这样(未测试)

    @ControllerAdvice
    public class UserRoleAdvice {
    
        // ... autowire needed services
    
        @ModelAttribute("currentRole")
        public String currentRole() {
            Users user = usersRepository.findOne(1 L);
            if (user != null) {
                return user.getRole().toString();
            } else {
                return "EMPLOYEE";
            }
        }
    }
    

    这将为所有页面添加此参数...

    【讨论】:

    • 谢谢。我将这个类相对于根目录放在哪里重要吗?
    • 你只需要把这个类放在自动扫描的目录下,或者简单地和控制器一起放置
    • 酷。我试试看。
    • 这似乎不起作用。我在某些页面上得到了参数,但在其他页面上没有。它似乎只适用于已经获取 User 对象的页面。不知道为什么。
    • 什么意思?那个参数null在某些页面上?
    【解决方案2】:

    如果您使用 Spring 安全性,Thymeleaf 有一个集成模块可以直接在您的模板中处理此问题,因此您可能甚至不需要模型中的用户名。将依赖项添加到适当版本的org.thymeleaf.extras:thymeleaf-extras-springsecurity4

    然后将xmlns添加到你的html中

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

    你可以做到

    <div th:text="${#authentication.name}">
        The value of the "name" property of the authentication object should appear here.
    </div>
    

    更多关于Spring Security Integration module的信息

    【讨论】:

      猜你喜欢
      • 2014-10-24
      • 1970-01-01
      • 2023-03-16
      • 2021-08-11
      • 2015-04-14
      • 2020-12-25
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      相关资源
      最近更新 更多