如果在 URL 中提供了某些参数,如何只显示一个页面?

我想在 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

    }

我需要如何更改代码才能显示主页(HTML 文件 in src/main/resources/static),但前提是通过身份验证检查?


更新 1:按照此处return ClassPathResource("main.html")的建议使用没有帮助。


慕哥6287543
浏览 155回答 3
3回答

POPMUISE

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

泛舟湖上清波郎朗

你真的不应该以这种方式来保护你的安全。通过 http 以明文形式发送密码不是好的做法。这里有一些使用 spring security 进行基本身份验证的示例。https://www.baeldung.com/spring-security-basic-authenticationhttps://www.baeldung.com/securing-a-restful-web-service-with-spring-security如果您按照本教程进行操作,那么您可以做的是为初学者分配一个内存用户。然后您可以将您的身份验证详细信息进行 Base64Encode 以提供给用户。然后,对于每个用户,您可以发送身份验证详细信息,并且当用户名和密码通过网络时,没有人可以窥探用户名和密码,并且您的请求在到达您的控制器之前得到处理。这样,您就可以将业务逻辑与身份验证分离。这至少是一个开始。希望这可以帮助。

慕勒3428872

你不应该像这样处理授权。您可以做的是启用 spring-security 并使用 spring security 注释来处理这种情况。使用以下依赖项并设置基本的 spring 安全设置。@PreAuthorize()然后可以使用诸如此类的注释在执行方法之前验证用户权限。如果您坚持,您甚至可以将此注释添加到控制器方法以在为每个请求提供服务之前进行验证。您可以设置和LDAP 服务器或 Oauth 甚至使用数据库进行身份验证(如果您正在处理演示或其他内容)。&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.security</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-security-web</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${spring-security.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.security</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-security-config</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${spring-security.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>使用如下配置类来配置安全性:@Configuration@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; DataSource dataSource;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {&nbsp; &nbsp; &nbsp; auth.jdbcAuthentication().dataSource(dataSource)&nbsp; &nbsp; &nbsp; &nbsp; .usersByUsernameQuery(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "select username,password, enabled from users where username=?")&nbsp; &nbsp; &nbsp; &nbsp; .authoritiesByUsernameQuery(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "select username, role from user_roles where username=?");&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void configure(HttpSecurity http) throws Exception {&nbsp; &nbsp; &nbsp; http.authorizeRequests()&nbsp; &nbsp; &nbsp; &nbsp; .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") //To check admin role permission&nbsp; &nbsp; &nbsp; &nbsp; .and()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .formLogin().loginPage("/login").failureUrl("/login?error") //provide failure url&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .usernameParameter("username").passwordParameter("password")&nbsp; &nbsp; &nbsp; &nbsp; .and()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .logout().logoutSuccessUrl("/login?logout")&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .and()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .exceptionHandling().accessDeniedPage("/403")&nbsp; &nbsp; &nbsp; &nbsp; .and()&nbsp; &nbsp; &nbsp; &nbsp; .csrf();&nbsp; &nbsp; }}github 中的这个示例项目提供了一个基本设置,您可以在其中使用 spring security:https://github.com/mohchi/spring-security-request-mapping参考使用: https ://www.mkyong.com/spring-security/spring-security-form-login-using-database/
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java