如何使用 Spring 安全性在 JSF 中使用令牌进行自定义登录

how to make custom login with token in JSF with Spring Security

我要求使用用户名和密码登录,然后根据有效凭据在他的邮件中发送令牌并要求他输入令牌。

我将 Login.xhtml 页面设置为登录页面,如下所示,但结果是我提交的任何表单都会进入授权过滤器而不是 bean,我需要它来捕获托管 bean 中的操作首先验证输入,然后向他发送令牌并验证他的输入。

http.authorizeRequests()
            .antMatchers("/", "/login.xhtml", "/finance.xhtml", "/WEB-INF/errorpages/**",
                    "/javax.faces.resource/**")
            .permitAll().anyRequest().fullyAuthenticated().and().formLogin().loginPage("/login.xhtml")
            .defaultSuccessUrl("/index.xhtml").failureUrl("/login.xhtml?authfailed=true").permitAll().and().logout()
            .logoutSuccessUrl("/login.xhtml").logoutUrl("/j_spring_security_logout").and().csrf().disable();

我将 loginprocessingurl 更改为假 url 并保留了登录页面,因此如果未登录,它会重定向到登录页面,然后我会做一些正常的事情来验证用户然后使用 authmanger验证,它工作正常但不是最佳特别是我正在显式重定向到主页(不是用户访问的页面)***Authentication auth = authenticationManager .authenticate(new UsernamePasswordAuthenticationToken(email,token)); SecurityContextHolder.getContext().setAuthentication(auth); Faces.redirect("/index.xhtml");