Answer the question
In order to leave comments, you need to log in
URL to the user's personal account?
Stack:
Spring boot + srping security + thymeleaf
+
postgresql this address is /profile/{id}(id=${user.id}) , but I can’t figure out how to pass the id of the current user to
the header to pull out the id
Spring I study myself, and not so long ago, please help me.
The structure of the
html project:
Answer the question
In order to leave comments, you need to log in
Spring Security provides the SecurityContextHolder class which allows you to search for the current authenticated user via:
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
getPrincipal()
getCredentials()
getAuthorities()
getDetails()
public String foo(Model model) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = user.getUsername();
model.addAttribute("username", username);
return "your_view";
}
<td th:each="user : ${user}">
<a th:if="${username == user.username}" th:href="@{/profile/{id}(id=${user.id}}">Profile</a>
</td>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question