我的数据库中有一个用户,我想编辑他们的信息。我正在尝试添加他们的性别和身高。问题是,更新用户后,我无法再次登录。该错误一直持续存在,并且总是在更新用户后发生。
这是我的 HTML 代码:
<form th:action="@{/editprofile/save}" th:object="${user}" method="post">
还有我的 Controller 代码:
@PostMapping("/editprofile/save")
public String save(@Valid User user, BindingResult result, RedirectAttributes redirect, Principal principal) {
if(result.hasErrors()) {
return "views/success";
}
System.out.println(user.getEmail());
System.out.println(user.getPassword());
System.out.println(user.getRepassword());
userService.save(user);
redirect.addFlashAttribute("success", "Saved employee successfully!");
return "redirect:/editprofile";
}
答案 0 :(得分:0)
是的,这都是表格:
Rails.application.routes.url_helpers.generate_csv_admin_event_path(resource, format: :csv)
这是我的控制器保存:
<form th:action="@{/editprofile/save}" th:object="${user}" method="post">
<div class="form-group row">
<label for="name"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Imię: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text" th:field="*{name}" id="name">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('name')}"-->
<!--th:errors="*{name}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Nazwisko: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{surname}" id="surname">
<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"
th:errors="*{surname}"></div>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 pr-0 pl-0 text-center col-form-label">Adres
Email: </label>
<div class="col-sm-10 cl-input">
<input type="email" class="form-control form-text " th:field="*{email}" id="email">
<div class="text col-sm-12 error" th:if="${#fields.hasErrors('email')}"
th:errors="*{email}"></div>
</div>
</div>
<div class="form-group row">
<label for="password"
class="col-sm-2 pr-0 pl-0 text-center hidden col-form-label">Hasło: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " value="elko" th:value="${user.getPassword()}" th:name="password" id="password">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('password')}"-->
<!--th:errors="*{password}">-->
<!--</div>-->
</div>
</div>
<div class="form-group row">
<label for="repassword" class="col-sm-2 pr-0 pl-0 text-center col-form-label">Powtórz
Hasło: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:value="${user.getRepassword()}" th:name="repassword" id="repassword">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('repassword')}"-->
<!--th:errors="*{repassword}">-->
<!--</div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Płec: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{gender}" id="gender">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Data urodzenia: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{birthdate}" id="birthdate">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Waga: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{weight}" id="weight">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Wzrost: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{growth}" id="growth">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="col-lg-12 ">
<div class="row">
<div class="col-lg-2"></div>
<input type="submit" value="Zapisz" class="col-lg-10 btn btn-primary"/>
</div>
</div>
</form>
用户服务:
@GetMapping("/editprofile")
public String editeProfile( Model model, Principal principal) {
String email = principal.getName();
User user = userService.findOne(email);
model.addAttribute("user", user);
return "views/editprofile";
}
@GetMapping("/editprofile/{email}")
public String editProfile( @PathVariable String email, Model model,Principal principal) {
model.addAttribute("user", userService.findOne(email));
return "views/editprofile";
}
@PostMapping("/editprofile/save")
public String save(@Valid User user, BindingResult result, RedirectAttributes redirect) {
if(result.hasErrors()) {
return "views/success";
}
System.out.println(user.getEmail());
System.out.println(user.getPassword());
System.out.println(user.getRepassword());
userService.save(user);
redirect.addFlashAttribute("success", "Saved employee successfully!");
return "redirect:/editprofile";
}
答案 1 :(得分:0)
感谢您遵循我指导的方法 https://stackoverflow.com/a/53371025/10232467
在给定的源代码中未定义身份验证和授权过程。我们应该使用Spring Security来完成这项工作。请导入spring安全依赖项,并按如下所示创建Security配置类
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
// .loginPage("/login") in case if custom login page is required
.permitAll()
.and()
.logout()
.permitAll()
// add ant matchers and require secure in case if certain url has to be on https
;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
// for in memory authentication
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(passwordEncoder())
.withUser("user")
.password(passwordEncoder().encode("password"))
.roles("USER");
}
}
您可以通过以下代码随时在控制器中登录用户详细信息
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String name = auth.getName(); //get logged in username