java - 如果 URL 中提供了某些参数,如何仅显示页面?

标签 java spring spring-boot authentication kotlin

我想在 Spring 中创建一个页面,其中包含 url

http://myapp.com/sign-in?email=myemail@provider.com&pw=password

password 是用户每次登录时通过电子邮件收到的一次性密码。

每当用户访问此页面时,我希望发生两件事:

  1. 检查所提供的凭据是否正确。
  2. 如果是,则显示页面的 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没有帮助。

最佳答案

return ClassPathResource("static/main.html") should answer your question, don't forget to specify `static` folder at the beginning as `ClassPathResource` points to the `resources` folder

关于java - 如果 URL 中提供了某些参数,如何仅显示页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56358199/

相关文章:

java - Spring简单示例: where create list of beans in JavaConfig?

java - 使用 Spring Boot 处理单个 Thymeleaf 片段

java - 创建了 Spring Boot Jar。运行但失败 `DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class` IntelliJ

java - 如何在运行时构建 kmodule.xml 以更新 drl 文件中所做的更改

java - 用于存储按字母顺序排列的单词列表的最有效数据结构

java - 关闭连接 : java. sql.SQLRecoverableException:关闭连接 jboss 7

java - Spring 。在我的 CustomDataSource getConnection 方法中访问 session Bean

spring - 重载发布请求映射

java - 无法在Jersey中进行基本的HTTP身份验证

java - TextView 文本超出范围