小编Woj*_*zuk的帖子

Spring Boot/Spring Security、登录表单、密码检查

我有一个可能很容易的问题,但我不明白。

我对 Spring Boot 不是很熟悉,很多事情在这里自动发生。我想检查数据库中是否存在在表单中写入用户名和密码的人[并且他的帐户已激活]。用户数据存储在 application.properties 中配置的 MySQL 数据库中。我想检查“用户”表中是否存在提供用户名的人,并检查提供的密码是否等于数据库中的用户密码。目前我可以从数据库中输入任何用户名,密码可以是随机的(这对我来说很明显,因为我不会在任何地方检查它,而且很奇怪,因为我觉得周围的一切都说它工作正常)。这对我来说听起来很简单,但我在 StackOverflow 或教程上找不到任何合适的解决方案。

我的一般问题是 - 我应该在哪里以及如何从登录表单中检查密码?它是自动完成的(但它以某种方式不起作用),还是应该编写我的自定义控制器/service/method 来做到这一点?如果需要自定义控制器,那么我解决问题的方向应该是什么?

目前我不知道该去哪里。我希望与我的问题相关的所有剩余代码都粘贴在这里。预先感谢您提供的所有提示和评论。

代码:

ApplicationSecurityAdapter 类:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class ApplicationSecurityAdapter extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserService userService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/user/register").permitAll()
            .antMatchers("/user/activate").permitAll()
            .antMatchers("/user/activation-send").permitAll()
            .antMatchers("/user/reset-password").permitAll()
            .antMatchers("/user/reset-password-change").permitAll()
            .antMatchers("/user/autologin").access("hasRole('ROLE_ADMIN')")
            .antMatchers("/user/delete").access("hasRole('ROLE_ADMIN')")
            .antMatchers("/img/**").permitAll()
            .antMatchers("/images/**").permitAll()
            .antMatchers("/fonts/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin().loginPage("/login").failureUrl("/login?error").permitAll()
            .and()
            .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login").permitAll() // added permitAll()
            .and()
            .rememberMe().key(applicationSecret)
            .tokenValiditySeconds(31536000);
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder());
}
Run Code Online (Sandbox Code Playgroud)

用户服务类:

@Service
public class …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-boot

4
推荐指数
1
解决办法
9909
查看次数

标签 统计

java ×1

spring ×1

spring-boot ×1

spring-security ×1