my problem is that i want to access all the user details on the profile-page and allow editing them later on. But currently, i struggle to access information of the logged in user beyond what the authentication allows to retrieve. In the Profile-Page i have 1 Line which accesses the "user" object (Failing with a ClassCastException) and below that the authentication which would deliver the Username, if the other Line was commented out. I've read many Stackoverflow posts on this topic and thus implemented UserDetails in my class and tried to adhere to the accepted answer of this post: "How to get active users userdetails". I either get the CastException or my object is null if I followed some other tipps to add my User to the model. I hope you can help my case, I (as many others with this question) am also just a few days into this technology.
my User class
@Entity public class User implements UserDetails/* extends AbstractAggregateRoot<SalespointIdentifier>*/{
/*
* UserAccount enthält Name, passwort, Mail und Rolle("Role")
*/
static enum Role{
USER, EMPLOYEE, BOSS;
}
@GeneratedValue(strategy = GenerationType.AUTO)
@Id private int ID;
@OneToOne private UserAccount account;
public Role USER_ROLE = Role.USER;
private String street;
private String PLZ;
private String phoneNumber;
private String password;
public User(UserAccount account, String street, String PLZ, String phoneNumber, String password) {
this.account = account;
this.street = street;
this.PLZ = PLZ;
this.phoneNumber= phoneNumber;
this.password = password;
}
/*________________GETTER_________________________*/
public String getName() {
return account.getUsername();
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return this.getName();
}
public int getId() {
return ID;
}
public UserAccount getAccount() {
return account;
}
public String getStreet() {
return street;
}
public String getPLZ() {
return PLZ;
}
public String getPhoneNumber() {
return phoneNumber;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
}
/*________________SETTER________________*/
public void setRole(Role role) {
USER_ROLE = role;
}
public void setSetreet(String street) {
this.street = street;
}
public void setPLZ(String PLZ) {
this.PLZ = PLZ;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public String toString() {
return "User [account= " + account + "\n"
+ ", ID= " + ID + "\n"
+ ", phoneNumber= " + phoneNumber + "\n"
+ ", PLZ= " + PLZ + "\n"
+ ", street= "+ street + "\n"
+ ", USER_ROLE= " + USER_ROLE + "\n"
+ ", getAccount()= " + getAccount()
+ ", getId()= " + getId() + "\n"
+ ", getPhoneNumber()= " + getPhoneNumber() + "\n"
+ ", getPLZ()= \n" + getPLZ() + "\n"
+ ", getStreet()= " + "\n" + getStreet()+ "]";
}
Controller:
@Controller
public class UserController {
@Autowired private UserManagement UserManager;
@Autowired private UserRepository Users;
@Autowired private AuthenticationManager auth;
@GetMapping("/")
public String register(Model model, RegistrationForm form) {
System.out.println("Homepage");
model.addAttribute("form", form);
return "register";
}
@GetMapping("/profile")
public String profile(Model model) {
User user = (User) SecurityContextHolder.getContext()
.getAuthentication()
.getPrincipal() ;
if(user != null) {
model.addAttribute(user);
}
else {
System.out.println("User ist null\n");
}
return "profile";
}
}
the profile page:
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" th:href="@{/resources/css/style.css}" href="../static/resources/css/style.css"/>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
<title>profile</title>
</head>
<body>
<header id ="mainHeader">
<div class ="container">
<h1>ClownCollege</h1>
</div>
</header>
<div class="container">
<nav class="navbar navbar-expand-lg bg-primary">
<div class="col-sm">
<div class="container-fluid">
<a class="navbar-brand" href="./startpage">Startseite</a>
</div>
</div>
<div class="col-sm">
<div class="container-fluid">
<a class="navbar-brand" href="#">Angebote</a>
</div>
</div>
<div class="col-sm">
<div class="container-fluid">
<a href="./profile">Mein Profil</a>
</div>
</div>
<div class="col-sm">
<div class="container-fluid">
<a class="navbar-brand" href="./login">Login</a>
</div>
</div>
</nav>
</div>
<h1>Ihr Profil</h1>
<div class="row mb-3">
<div class="col-sm-10">
<input class="form-control" th:text=${user.getUsername()} aria-label="Disabled input example" disabled readonly>
</div>
</div>
<div class="row mb-3">
<div sec:authorize="isAuthenticated()"> Authenticated as <span sec:authentication="name"></span></div>
</body>
</html>