为啥更新数据库中的行后出现登录问题?
Posted
技术标签:
【中文标题】为啥更新数据库中的行后出现登录问题?【英文标题】:Why do I have a login issue after updating rows in my database?为什么更新数据库中的行后出现登录问题? 【发布时间】:2019-04-24 07:32:59 【问题描述】:我的数据库中有一个用户,我想编辑他们的信息。我正在尝试添加他们的性别和身高。问题是,更新我的用户后,我无法再次登录。此错误一直持续存在,并且总是在更新我的用户后发生。
这是我的 HTML 代码:
<form th:action="@/editprofile/save" th:object="$user" method="post">
还有我的控制器代码:
@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";
【问题讨论】:
能否添加整个 html 表单和模型代码? 显示save
的UserService
方法。
我在下面添加了完整代码
【参考方案1】:
是的,这就是所有形式:
<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";
用户服务:
public void save(User user)
userRepository.save(user);
【讨论】:
【参考方案2】:感谢您遵循我指导的方法 https://***.com/a/53371025/10232467
您给定的源代码中未定义身份验证和授权过程。我们应该使用 Spring Security 来完成这项工作。请导入 spring 安全依赖项并制作安全配置类,如下所示
@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
【讨论】:
以上是关于为啥更新数据库中的行后出现登录问题?的主要内容,如果未能解决你的问题,请参考以下文章
可编辑的网格 ExtJS,如何在网格上编辑行后更新数据库中的行
删除 TableView 中的行后如何更新 indexPath?