为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕哥6287543 2022-12-21 14:36:49
我想在 Spring 中创建一个页面,其中包含 urlhttp://myapp.com/sign-in?email=myemail@provider.com&pw=passwordpassword是用户每次要登录时通过电子邮件收到的一次性密码。每当用户访问此页面时,我希望发生两件事:检查提供的凭据是否正确。如果是,则显示页面的 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")的建议使用没有帮助。
查看完整描述

3 回答

?
POPMUISE

TA贡献1765条经验 获得超5个赞

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



查看完整回答
反对 回复 2022-12-21
?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

你真的不应该以这种方式来保护你的安全。通过 http 以明文形式发送密码不是好的做法。

这里有一些使用 spring security 进行基本身份验证的示例。

https://www.baeldung.com/spring-security-basic-authentication

https://www.baeldung.com/securing-a-restful-web-service-with-spring-security

如果您按照本教程进行操作,那么您可以做的是为初学者分配一个内存用户。然后您可以将您的身份验证详细信息进行 Base64Encode 以提供给用户。然后,对于每个用户,您可以发送身份验证详细信息,并且当用户名和密码通过网络时,没有人可以窥探用户名和密码,并且您的请求在到达您的控制器之前得到处理。这样,您就可以将业务逻辑与身份验证分离。

这至少是一个开始。希望这可以帮助。


查看完整回答
反对 回复 2022-12-21
?
慕勒3428872

TA贡献1848条经验 获得超6个赞

你不应该像这样处理授权。您可以做的是启用 spring-security 并使用 spring security 注释来处理这种情况。使用以下依赖项并设置基本的 spring 安全设置。


@PreAuthorize()然后可以使用诸如此类的注释在执行方法之前验证用户权限。如果您坚持,您甚至可以将此注释添加到控制器方法以在为每个请求提供服务之前进行验证。


您可以设置和LDAP 服务器或 Oauth 甚至使用数据库进行身份验证(如果您正在处理演示或其他内容)。


        <dependency>

            <groupId>org.springframework.security</groupId>

            <artifactId>spring-security-web</artifactId>

            <version>${spring-security.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework.security</groupId>

            <artifactId>spring-security-config</artifactId>

            <version>${spring-security.version}</version>

        </dependency>

使用如下配置类来配置安全性:


@Configuration

@EnableWebSecurity

public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired

    DataSource dataSource;


    @Autowired

    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {


      auth.jdbcAuthentication().dataSource(dataSource)

        .usersByUsernameQuery(

            "select username,password, enabled from users where username=?")

        .authoritiesByUsernameQuery(

            "select username, role from user_roles where username=?");

    }   


    @Override

    protected void configure(HttpSecurity http) throws Exception {


      http.authorizeRequests()

        .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") //To check admin role permission

        .and()

          .formLogin().loginPage("/login").failureUrl("/login?error") //provide failure url

          .usernameParameter("username").passwordParameter("password")

        .and()

          .logout().logoutSuccessUrl("/login?logout") 

        .and()

          .exceptionHandling().accessDeniedPage("/403")

        .and()

        .csrf();

    }

}

github 中的这个示例项目提供了一个基本设置,您可以在其中使用 spring security:


https://github.com/mohchi/spring-security-request-mapping


参考使用: https ://www.mkyong.com/spring-security/spring-security-form-login-using-database/


查看完整回答
反对 回复 2022-12-21
  • 3 回答
  • 0 关注
  • 154 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号