One Time Token with default configuration

Here is the example of the one time token with default configuration in spring boot.

Steps

  1. User can access any public pages.

  2. User request for login page or try to access any protected page, then login page comes.

  3. User can directly enter username and password and do login or can use one time login.

  4. User enters his username/email and requests for one-time token in the login page .

  5. Spring security validates the user's existence, generates token and sends a one-time token login link to their corresponding email or phone number and shows sent success page.

  6. The user receives the link and by clicking it, he redirects to the link, where he does login with the token provided.

  7. The system validates the token, do the authentication process and allows him to view the secured content.

Security Configuration


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    // to encode/ decode password with Bycrypt
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public SecurityFilterChain configure(HttpSecurity security) throws Exception {

        return security.authorizeHttpRequests(request ->
                        request
                                .requestMatchers("/public").permitAll()
                                .requestMatchers(HttpMethod.GET, "/ott/sent").permitAll()
                                .anyRequest().authenticated()
                )
                .formLogin(Customizer.withDefaults())
                .oneTimeTokenLogin(Customizer.withDefaults()) // with default setup
                .build();

    }


    // in memory user details service
    @Bean
    public InMemoryUserDetailsManager userDetailsManager() {
        var user = User.withUsername("srv").password(this.passwordEncoder().encode("12345")).build();
        return new InMemoryUserDetailsManager(user);
    }

}
  • /public page is the page that is available to all.

  • /ott/sent is the controller url which will get invoked after successfully token generation.

We also need to configure ott success handler .

One time token success handler

/ott/sent handler and page

sent.jte

pom.xml

application.properties

Last updated