一次性令牌登录

Spring Security 通过 DSL 支持一次性令牌 (OTT) 身份验证。 在深入研究实施细节之前,请务必阐明框架中 OTT 功能的范围,重点介绍支持和不支持的功能。oneTimeTokenLogin()spring-doc.cn

了解一次性令牌与一次性密码

将一次性令牌 (OTT) 与一次性密码 (OTP) 混淆是很常见的,但在 Spring Security 中,这些概念在几个关键方面有所不同。 为清楚起见,我们假设 OTP 是指 TOTP(基于时间的一次性密码)或 HOTP(基于 HMAC 的一次性密码)。spring-doc.cn

设置要求

  • OTT:不需要初始设置。用户无需提前配置任何内容。spring-doc.cn

  • OTP:通常需要设置,例如生成密钥并与外部工具共享密钥以生成一次性密码。spring-doc.cn

Token Delivery

代币生成

总之,一次性令牌 (OTT) 提供了一种无需额外帐户设置即可对用户进行身份验证的方法,将其与一次性密码 (OTP) 区分开来,后者通常涉及更复杂的设置过程,并依赖外部工具生成令牌。spring-doc.cn

一次性令牌登录分两个主要步骤进行。spring-doc.cn

  1. 用户通过提交其用户标识符(通常是用户名)来请求令牌,令牌通常以 Magic Link 的形式通过电子邮件、短信等方式发送给他们。spring-doc.cn

  2. 用户将令牌提交到一次性令牌登录终端节点,如果有效,则用户登录。spring-doc.cn

在以下部分中,我们将探讨如何根据您的需求配置 OTT 登录。spring-doc.cn

默认登录页面和默认一次性令牌提交页面

DSL 可以与 结合使用,后者将在默认生成的登录页面中生成一个额外的一次性令牌请求表单。 它还将设置 DefaultOneTimeTokenSubmitPageGeneratingFilter 以生成默认的一次性令牌提交页面。oneTimeTokenLogin()formLogin()spring-doc.cn

将 Token 发送给用户

Spring Security 无法合理地确定应将令牌交付给用户的方式。 因此,必须提供自定义的 OneTimeTokenGenerationSuccessHandler,以便根据您的需求将令牌交付给用户。 最常见的交付策略之一是 Magic Link,通过电子邮件、短信等。 在以下示例中,我们将创建一个 magic link 并将其发送到用户的电子邮件。spring-doc.cn

一次性令牌登录配置
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) {
        http
            // ...
            .formLogin(Customizer.withDefaults())
            .oneTimeTokenLogin(Customizer.withDefaults());
        return http.build();
    }

}

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

@Component (1)
public class MagicLinkOneTimeTokenGenerationSuccessHandler implements OneTimeTokenGenerationSuccessHandler {

    private final MailSender mailSender;

    private final OneTimeTokenGenerationSuccessHandler redirectHandler = new RedirectOneTimeTokenGenerationSuccessHandler("/ott/sent");

    // constructor omitted

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, OneTimeToken oneTimeToken) throws IOException, ServletException {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(UrlUtils.buildFullRequestUrl(request))
                .replacePath(request.getContextPath())
                .replaceQuery(null)
                .fragment(null)
                .path("/login/ott")
                .queryParam("token", oneTimeToken.getTokenValue()); (2)
        String magicLink = builder.toUriString();
        String email = getUserEmail(oneTimeToken.getUsername()); (3)
        this.mailSender.send(email, "Your Spring Security One Time Token", "Use the following link to sign in into the application: " + magicLink); (4)
        this.redirectHandler.handle(request, response, oneTimeToken); (5)
    }

    private String getUserEmail() {
        // ...
    }

}

@Controller
class PageController {

    @GetMapping("/ott/sent")
    String ottSent() {
        return "my-template";
    }

}
@Configuration
@EnableWebSecurity
class SecurityConfig {

        @Bean
        open fun filterChain(http: HttpSecurity): SecurityFilterChain {
            http{
                formLogin {}
                oneTimeTokenLogin {  }
            }
            return http.build()
        }
}

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

@Component (1)
class MagicLinkOneTimeTokenGenerationSuccessHandler(
    private val mailSender: MailSender,
    private val redirectHandler: OneTimeTokenGenerationSuccessHandler = RedirectOneTimeTokenGenerationSuccessHandler("/ott/sent")
) : OneTimeTokenGenerationSuccessHandler {

    override fun handle(request: HttpServletRequest, response: HttpServletResponse, oneTimeToken: OneTimeToken) {
        val builder = UriComponentsBuilder.fromHttpUrl(UrlUtils.buildFullRequestUrl(request))
            .replacePath(request.contextPath)
            .replaceQuery(null)
            .fragment(null)
            .path("/login/ott")
            .queryParam("token", oneTimeToken.getTokenValue()) (2)
        val magicLink = builder.toUriString()
        val email = getUserEmail(oneTimeToken.getUsername()) (3)
        this.mailSender.send(email, "Your Spring Security One Time Token", "Use the following link to sign in into the application: $magicLink")(4)
        this.redirectHandler.handle(request, response, oneTimeToken) (5)
    }

    private fun getUserEmail(): String {
        // ...
    }
}

@Controller
class PageController {

    @GetMapping("/ott/sent")
    fun ottSent(): String {
        return "my-template"
    }
}
1 制作 Spring beanMagicLinkOneTimeTokenGenerationSuccessHandler
2 使用 as a query 参数创建登录处理 URLtoken
3 根据用户名检索用户的电子邮件
4 使用 API 通过神奇链接将电子邮件发送给用户JavaMailSender
5 使用 执行重定向到所需的 URLRedirectOneTimeTokenGenerationSuccessHandler

电子邮件内容将类似于:spring-doc.cn

使用以下链接登录应用程序:http://localhost:8080/login/ott?token=a830c444-29d8-4d98-9b46-6aba7b22fe5bspring-doc.cn

默认提交页面将检测到 URL 具有 query 参数,并将自动使用 token 值填充 form 字段。tokenspring-doc.cn

更改一次性令牌生成 URL

默认情况下,GenerateOneTimeTokenFilter 侦听请求。 可以使用 DSL 方法更改该 URL:POST /ott/generategenerateTokenUrl(String)spring-doc.cn

更改生成 URL
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) {
        http
            // ...
            .formLogin(Customizer.withDefaults())
            .oneTimeTokenLogin((ott) -> ott
                .generateTokenUrl("/ott/my-generate-url")
            );
        return http.build();
    }

}

@Component
public class MagicLinkOneTimeTokenGenerationSuccessHandler implements OneTimeTokenGenerationSuccessHandler {
    // ...
}
@Configuration
@EnableWebSecurity
class SecurityConfig {

        @Bean
        open fun filterChain(http: HttpSecurity): SecurityFilterChain {
            http {
                //...
                formLogin { }
                oneTimeTokenLogin {
                    generateTokenUrl = "/ott/my-generate-url"
                }
            }
            return http.build()
        }
}

@Component
class MagicLinkOneTimeTokenGenerationSuccessHandler : OneTimeTokenGenerationSuccessHandler {
     // ...
}

更改默认提交页面 URL

默认的一次性令牌提交页面由 DefaultOneTimeTokenSubmitPageGeneratingFilter 生成,并侦听 . URL 也可以更改,如下所示:GET /login/ottspring-doc.cn

配置默认提交页面 URL
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) {
        http
            // ...
            .formLogin(Customizer.withDefaults())
            .oneTimeTokenLogin((ott) -> ott
                .submitPageUrl("/ott/submit")
            );
        return http.build();
    }

}

@Component
public class MagicLinkGenerationSuccessHandler implements OneTimeTokenGenerationSuccessHandler {
    // ...
}
@Configuration
@EnableWebSecurity
class SecurityConfig {

        @Bean
        open fun filterChain(http: HttpSecurity): SecurityFilterChain {
            http {
                //...
                formLogin { }
                oneTimeTokenLogin {
                    submitPageUrl = "/ott/submit"
                }
            }
            return http.build()
        }
}

@Component
class MagicLinkOneTimeTokenGenerationSuccessHandler : OneTimeTokenGenerationSuccessHandler {
     // ...
}

禁用默认提交页面

如果您想使用自己的一次性令牌提交页面,您可以禁用默认页面,然后提供自己的终端节点。spring-doc.cn

禁用默认提交页面
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) {
        http
            .authorizeHttpRequests((authorize) -> authorize
                .requestMatchers("/my-ott-submit").permitAll()
                .anyRequest().authenticated()
            )
            .formLogin(Customizer.withDefaults())
            .oneTimeTokenLogin((ott) -> ott
                .showDefaultSubmitPage(false)
            );
        return http.build();
    }

}

@Controller
public class MyController {

    @GetMapping("/my-ott-submit")
    public String ottSubmitPage() {
        return "my-ott-submit";
    }

}

@Component
public class OneTimeTokenGenerationSuccessHandler implements OneTimeTokenGenerationSuccessHandler {
    // ...
}
@Configuration
@EnableWebSecurity
class SecurityConfig {

   @Bean
   open fun filterChain(http: HttpSecurity): SecurityFilterChain {
            http {
                authorizeHttpRequests {
                    authorize("/my-ott-submit", authenticated)
                    authorize(anyRequest, authenticated)
                }
                formLogin { }
                oneTimeTokenLogin {
                    showDefaultSubmitPage = false
                }
            }
            return http.build()
    }
}

@Controller
class MyController {

   @GetMapping("/my-ott-submit")
   fun ottSubmitPage(): String {
       return "my-ott-submit"
   }
}

@Component
class MagicLinkOneTimeTokenGenerationSuccessHandler : OneTimeTokenGenerationSuccessHandler {
     // ...
}

自定义如何生成和使用一次性令牌

定义用于生成和使用一次性令牌的常见操作的接口是 OneTimeTokenService。 Spring Security 使用InMemoryOneTimeTokenService作为该接口的默认实现(如果未提供)。 对于 生产环境 考虑使用 JdbcOneTimeTokenServicespring-doc.cn

自定义的一些最常见原因包括但不限于:OneTimeTokenServicespring-doc.cn

有两个选项可用于自定义 . 一种选择是将其作为 bean 提供,这样 DSL 就可以自动拾取它:OneTimeTokenServiceoneTimeTokenLogin()spring-doc.cn

将 OneTimeTokenService 作为 Bean 传递
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) {
        http
            // ...
            .formLogin(Customizer.withDefaults())
            .oneTimeTokenLogin(Customizer.withDefaults());
        return http.build();
    }

    @Bean
    public OneTimeTokenService oneTimeTokenService() {
        return new MyCustomOneTimeTokenService();
    }

}

@Component
public class MagicLinkOneTimeTokenGenerationSuccessHandler implements OneTimeTokenGenerationSuccessHandler {
    // ...
}
@Configuration
@EnableWebSecurity
class SecurityConfig {

    @Bean
    open fun filterChain(http: HttpSecurity): SecurityFilterChain {
        http {
            //...
            formLogin { }
            oneTimeTokenLogin { }
        }
        return http.build()
    }

    @Bean
    open fun oneTimeTokenService(): OneTimeTokenService {
        return MyCustomOneTimeTokenService()
    }
}

@Component
class MagicLinkOneTimeTokenGenerationSuccessHandler : OneTimeTokenGenerationSuccessHandler {
     // ...
}

第二个选项是将实例传递给 DSL,如果有多个实例,并且每个实例都需要不同的实例,这将非常有用。OneTimeTokenServiceSecurityFilterChainOneTimeTokenServicespring-doc.cn

使用 DSL 传递 OneTimeTokenService
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) {
        http
            // ...
            .formLogin(Customizer.withDefaults())
            .oneTimeTokenLogin((ott) -> ott
                .oneTimeTokenService(new MyCustomOneTimeTokenService())
            );
        return http.build();
    }

}

@Component
public class MagicLinkOneTimeTokenGenerationSuccessHandler implements OneTimeTokenGenerationSuccessHandler {
    // ...
}
@Configuration
@EnableWebSecurity
class SecurityConfig {

    @Bean
    open fun filterChain(http: HttpSecurity): SecurityFilterChain {
        http {
            //...
            formLogin { }
            oneTimeTokenLogin {
                oneTimeTokenService = MyCustomOneTimeTokenService()
            }
        }
        return http.build()
    }

}

@Component
class MagicLinkOneTimeTokenGenerationSuccessHandler : OneTimeTokenGenerationSuccessHandler {
     // ...
}