【发布时间】:2019-05-29 10:16:14
【问题描述】:
我想在 Spring 中创建一个页面,其中包含 url
http://myapp.com/sign-in?email=myemail@provider.com&pw=password
password 是一次性密码,用户每次想要登录时都会通过电子邮件收到该密码。
每当用户访问此页面时,我希望发生两件事:
- 检查提供的凭据是否正确。
- 如果是,则显示页面的 HTML 内容。
我已经完成了第一部分:
@Autowired
private var userRepository: UserRepository? = null
@GetMapping
fun signIn(@RequestParam email:String, @RequestParam(name="pw") password:String): RedirectView {
// Is the password correct?
// TODO: Read password hash of the user
val existingUser: Optional<UserInfo>? = userRepository?.findById(email)
if (existingUser == null) {
return redirectToErrorPage("Please register with your e-mail address first")
}
if (!existingUser.isPresent) {
return redirectToErrorPage("Please register with your e-mail address first")
}
val hashInDb = existingUser.get().passwordHash
val hashInParam = PasswordHashCalculator.calculateHash(password)
if (!hashInDb.equals(hashInParam)) {
return redirectToErrorPage("Invalid user name and/or password")
}
// TODO: Display the main page
return null
}
如何更改代码以显示主页(src/main/resources/static 中的 HTML 文件),但前提是身份验证检查通过?
更新 1: 按照建议使用 return ClassPathResource("main.html") here 没有帮助。
【问题讨论】:
标签: java spring spring-boot authentication kotlin