Fix: Passwort-Änderung wirft Whitelabel-Error

ProfileController.changePassword gab "profile" zurück, ohne das von
profile.html benötigte Model-Attribut `user` zu setzen -> Template-Rendering
schlug fehl (500/Whitelabel). Zudem lag das Formular auf der settings-Seite.

- changePassword nutzt jetzt PRG: Redirect auf /profile/settings mit
  Flash-Attributen (message/error) statt direktem View-Return.
- settings.html zeigt die Flash-Meldungen (Erfolg/Fehler) an.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
AliceDarkest
2026-06-26 15:07:53 +02:00
parent 9022961762
commit 7cb6621d1b
2 changed files with 16 additions and 7 deletions
@@ -10,6 +10,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@@ -133,25 +134,25 @@ public class ProfileController {
public String changePassword(@RequestParam("oldPassword") String oldPassword,
@RequestParam("newPassword") String newPassword,
Principal principal,
Model model) {
RedirectAttributes redirectAttributes) {
String username = principal.getName();
User user = userService.findByUsername(username);
if (user == null) {
model.addAttribute("error", "Benutzer nicht gefunden.");
return "profile";
redirectAttributes.addFlashAttribute("error", "Benutzer nicht gefunden.");
return "redirect:/profile/settings";
}
if (!passwordEncoder.matches(oldPassword, user.getPassword())) {
model.addAttribute("error", "Das aktuelle Passwort ist falsch.");
return "profile";
redirectAttributes.addFlashAttribute("error", "Das aktuelle Passwort ist falsch.");
return "redirect:/profile/settings";
}
user.setPassword(passwordEncoder.encode(newPassword));
userService.save(user);
model.addAttribute("message", "Passwort erfolgreich geändert.");
return "profile";
redirectAttributes.addFlashAttribute("message", "Passwort erfolgreich geändert.");
return "redirect:/profile/settings";
}
@PostMapping("/profile/delete")