'How to get ID from current logged in user to thymeleaf and use it in link th:ref
im trying to acess my authenticated users details, and show a custom control panel with all details regarding that user, the problem im facing is passing an Id to the thymleaf th:href with something similiar to this e.g:
href="@{/updateUser/{id}(id=${getUserById})}">Attend
Iv tried several methods to acess the userDetails, which iv made work, i can now for example through my "UserDetails" acess certain methods as fullname, age .. and anything from my entity. Even the id, which i can print out through thymeleaf to my home screen, but what im really trying here is to acess the id, passe it to thymeleaf th:href link so everytime any account is logged in (i have user, and employee) it can use that id and know which user to get to.
Controller:
@GetMapping("/account")
public String viewUserAccountForm(
@AuthenticationPrincipal CustomUserDetails userDetails,
Model model) {
model.addAttribute("user", userDetails.getIdFromUser());
return "layout";
}
User Details public class CustomUserDetails implements UserDetails { private User user;
public CustomUserDetails(User user) {
this.user = user;
}
//Vänta på Roles implementering i User.
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Set<Role> roles = user.getRoles();
List<SimpleGrantedAuthority> authorities = new ArrayList<>();
for (Role role : roles){
authorities.add(new SimpleGrantedAuthority(role.getName()));
}
return authorities;
}
@Override
public String getPassword() {
return user.getPassword();
}
@Override
public String getUsername() {
return user.getEmail();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
public String getFullName(){
return user.getName();
}
public Integer getIdFromUser(){
return user.getId();
}
}
**Layout / Header **
<div class="nav-item" th:switch="${#authentication}? ${#authorization.expression('isAuthenticated()')} : ${false}">
<div th:case="${false}">
<a th:href="@{/signIn}">
<button class="loginButton">Sign in</button>
</a>
</div>
<div th:case="${true}">
<ul class="navbar-nav">
<li>
<img class="nav-link dropdown-toggle" href="#" id="navbarDropdown2" role="button"
data-bs-toggle="dropdown"
aria-haspopup="true" aria-expanded="false" th:src="@{/images/online.png}"/>
<div class="dropdown-menu" id="d1" aria-labelledby="navbarDropdown2">
<a class="dropdown-item" href="#">My Account</a>
<a class="dropdown-item" th:href="@{/logout}">Logout</a>
<a class="dropdown-item" th:href="@{/updateCustomer/{id}}">Logout</a>
<a class="dropdown-item" th:each="user : ${user}" th:href="@{/userList/{id}(id=${getUserById})}">Attend</a>
</div>
</li>
</ul>
</div>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
