带有Spring和Thymeleaf的Java项目标题部分中的用户图片

带有Spring和Thymeleaf的Java项目标题部分中的用户图片,java,spring,authentication,thymeleaf,Java,Spring,Authentication,Thymeleaf,我正在用Spring和Thymeleaf开发一个Java项目。 我已经为用户配置了一个工作类,该类存储了存储在服务器上的图片字符串。 它工作得很好,我在主块中成功地调用了它。在我的项目的上半部分,我有一个导航面板,它在html的标题部分中描述。完整的html如下所示: layout.html: <!DOCTYPE HTML> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org

我正在用Spring和Thymeleaf开发一个Java项目。 我已经为用户配置了一个工作类,该类存储了存储在服务器上的图片字符串。 它工作得很好,我在主块中成功地调用了它。在我的项目的上半部分,我有一个导航面板,它在html的标题部分中描述。完整的html如下所示: layout.html:

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

<head th:include="fragments/head" th:with="pageTitle='Java Project'"></head>

<body>

<header th:include="fragments/header"></header>

<main th:include="${view}"></main>

<footer th:include="fragments/footer"></footer>

<span th:include="fragments/scripts-bundle"></span>

</body>
</html>
一切正常

现在我想调用与视图中相同的代码,但要在描述导航面板的片段/标题部分中调用。 我不能成功地做到这一点。 据我所知,我认为有两种方法可以做到这一点:

1/在thymeleaf视图中通过控制器代码注入用户: 尝试了类似于:

@GetMapping("/fragments/header")
@PreAuthorize("isAuthenticated()")
public String setPicHead(Model model){
    UserDetails principal = (UserDetails) SecurityContextHolder.getContext()
            .getAuthentication()
            .getPrincipal();

    User user = this.userRepository.findByEmail(principal.getUsername());

    model.addAttribute("user", user);
    model.addAttribute("fragments/header", user);

    return "fragments/header";
}

In the fragments/header html I added:
<li sec:authorize="isAuthenticated()">
    <img class="img-circle" th:src="@{/images/users/(picture=${user.picture})}" height="43" width="43"/>
</li>
@GetMapping(“/fragments/header”)
@预授权(“isAuthenticated()”)
公共字符串setPicHead(模型){
UserDetails主体=(UserDetails)SecurityContextHolder.getContext()
.getAuthentication()
.getPrincipal();
User User=this.userRepository.findByEmail(principal.getUsername());
model.addAttribute(“用户”,用户);
model.addAttribute(“片段/标题”,用户);
返回“片段/标题”;
}
在我添加的片段/标题html中:
  • 完全不起作用并锁定整个项目

    2/我想第二种方法是使用Thymeleaf功能独立地调用用户。我猜 这是可以做到的,因为Thymeleaf具有完整的引擎功能

    谁能帮我做这个吗? 提前谢谢。

    我找到了解决方案:

    在此处注射:

    @GetMapping("/")
    public String index(Model model) {
    
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    
        User user = this.userRepository.findByEmail(principal.getUsername());
    
        model.addAttribute("user", user);
        model.addAttribute("view", "home/index");
    
        return "layout";
    }
    
    在标题部分,它是这样的:

    <li sec:authorize="isAuthenticated()">
                            <img class="img-circle" th:src="@{/images/users/{one}(one=${user.picture})}" height="49" width="49"/>
                        </li>
    
  • 不过,如果有人在Thymeleaf中与直接身份验证共享解决方案,我会很高兴

    @GetMapping("/fragments/header")
    @PreAuthorize("isAuthenticated()")
    public String setPicHead(Model model){
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
    
        User user = this.userRepository.findByEmail(principal.getUsername());
    
        model.addAttribute("user", user);
        model.addAttribute("fragments/header", user);
    
        return "fragments/header";
    }
    
    In the fragments/header html I added:
    <li sec:authorize="isAuthenticated()">
        <img class="img-circle" th:src="@{/images/users/(picture=${user.picture})}" height="43" width="43"/>
    </li>
    
    @GetMapping("/")
    public String index(Model model) {
    
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    
        User user = this.userRepository.findByEmail(principal.getUsername());
    
        model.addAttribute("user", user);
        model.addAttribute("view", "home/index");
    
        return "layout";
    }
    
    <li sec:authorize="isAuthenticated()">
                            <img class="img-circle" th:src="@{/images/users/{one}(one=${user.picture})}" height="49" width="49"/>
                        </li>