首页 > 解决方案 > 如何在春季启动中检查帐户活动邮件?

问题描述

我在 Spring Boot 中使用 smtp 邮件进行注册,但是登录时我无法检查帐户是否处于活动状态,我将 AuthenticationSuccessHandler 添加到 Spring Security 但是当帐户处于活动状态时我无法删除 JSESSIONID,这是我的代码:

public class CustomAuthenticationSuccessHandler
        implements AuthenticationSuccessHandler {

    @Autowired
    UserServiceImpl userService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        String username = authentication.getName();
        User user = userService.findUserActiveByUsername(username);
        if(user!=null){
            httpServletResponse.sendRedirect("/home");
        }
        else {
            Cookie cookie = new Cookie("JSESSIONID", null);
            cookie.setMaxAge(0);
            cookie.setSecure(true);
            cookie.setHttpOnly(true);
            cookie.setPath("/");
            httpServletResponse.addCookie(cookie);
            httpServletResponse.sendRedirect("/login?message=error1");
        }
    }
}

弹簧安全:

http.authorizeRequests().antMatchers("/confirm-account**","/registration**", "/js/**", "/css/**", "/img/**", "/webjars/**", "/icons/**", "/dist/**",
                "/linechar/**", "/plugins/**", "/bootstrap/**","/apis/**","/cart**","/product**","/products**","/images/**","/**").permitAll()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .and().formLogin()
                .loginPage("/login")
//                .defaultSuccessUrl("/home")//
                .successHandler(authenticationFailureHandler())
                .failureUrl("/login?message=error")//
                .permitAll().and()
                .logout().invalidateHttpSession(true).clearAuthentication(true)
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login?message=logout")
                .permitAll();

存储库:

@Query(value = "select * from user where username=?1 and enabled=1",nativeQuery = true)
    User getUserByUserActiveName(String username);

标签: spring-bootspring-securitythymeleafhandler

解决方案


推荐阅读