我遇到了SecurityContextHolder的问题。
给定一个Thymeleaf模板,其中可以更改和提交用户详细信息。作为管理员,我正在进入/编辑控制器。到目前为止一切都很好:
@RequestMapping(value="/user/{username}/edit", method= RequestMethod.GET)
public ModelAndView edit(@PathVariable String username, ModelMap model) {
User user = userService.findUser(username);
User loggedInUser = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
....
}
在loggedInUser变量中,我看到了admin用户。这按预期工作:
提交表单时。我再次检查了主要对象。现在它突然变为我正在编辑的用户。
表单摘要
<form class="form-horizontal" role="form" th:action="@{/users/save}" method="post" th:object="${user}">
<input type="hidden" id="old_username" name="old_username" th:value="${user.username}" required>
<input type="text" class="form-control" id="username" th:field="${user.username}" required>
....
</form
接收POST控制器:
@RequestMapping(value="/users/save", method= RequestMethod.POST)
public String save(User user, HttpServletRequest request, RedirectAttributes redir) {
User loggedInUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
....
发生了什么事?