88import java .util .List ;
99import java .util .Map ;
1010
11+ import org .slf4j .Logger ;
12+ import org .slf4j .LoggerFactory ;
1113import org .springframework .boot .context .properties .EnableConfigurationProperties ;
1214import org .springframework .context .annotation .Bean ;
1315import org .springframework .context .annotation .Configuration ;
1416import org .springframework .security .config .Customizer ;
1517import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
1618import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
19+ import org .springframework .security .core .userdetails .User ;
20+ import org .springframework .security .core .userdetails .UserDetailsService ;
21+ import org .springframework .security .provisioning .InMemoryUserDetailsManager ;
1722import org .springframework .security .web .SecurityFilterChain ;
1823import org .springframework .security .web .header .writers .ReferrerPolicyHeaderWriter ;
1924import org .springframework .security .web .savedrequest .NullRequestCache ;
2833@ EnableConfigurationProperties (AdditionalOAuth2ClientProperties .class )
2934public class MainWebSecurity
3035{
31- @ Bean (name = "mainSecurityFilterChainBean" )
32- public SecurityFilterChain configure (
36+ private static final Logger LOG = LoggerFactory .getLogger (MainWebSecurity .class );
37+
38+ protected void customizeLogin (
3339 final HttpSecurity http ,
3440 final AdditionalOAuth2ClientProperties additionalOAuth2ClientProperties ) throws Exception
3541 {
36- http .with (new AdvancedLoginPageAdapter <>(http ), c -> c
42+ http .with (
43+ new AdvancedLoginPageAdapter <>(http ), c -> c
3744 .customizePages (p -> p
3845 // No remote communication -> Use local resources
3946 .setHeaderElements (List .of (
@@ -60,18 +67,48 @@ public SecurityFilterChain configure(
6067 + " href='https://xdev.software' target='_blank'>"
6168 + " XDEV Software"
6269 + " </a>"
63- + "</p>" )))
70+ + "</p>" )));
71+ }
72+
73+ @ Bean (name = "mainSecurityFilterChainBean" )
74+ public SecurityFilterChain configure (
75+ final HttpSecurity http ,
76+ final AdditionalOAuth2ClientProperties additionalOAuth2ClientProperties ) throws Exception
77+ {
78+ this .customizeLogin (http , additionalOAuth2ClientProperties );
79+
80+ http
6481 .headers (h -> h
6582 .referrerPolicy (r -> r .policy (ReferrerPolicyHeaderWriter .ReferrerPolicy .SAME_ORIGIN ))
6683 .contentSecurityPolicy (csp -> csp .policyDirectives (this .getCSP ())))
6784 .formLogin (Customizer .withDefaults ())
85+ .oneTimeTokenLogin (c -> c .tokenGenerationSuccessHandler (
86+ (request , response , oneTimeToken ) ->
87+ LOG .info (
88+ "OneTimeToken should be sent for {} with value {}" ,
89+ oneTimeToken .getUsername (),
90+ oneTimeToken .getTokenValue ())))
91+ .webAuthn (c -> c .rpName ("Spring Security Localhost Relying Party" )
92+ .rpId ("localhost" )
93+ .allowedOrigins ("http://localhost:8080" ))
6894 .oauth2Login (c -> c .defaultSuccessUrl ("/" ))
6995 .authorizeHttpRequests (urlRegistry -> urlRegistry .anyRequest ().authenticated ())
7096 .requestCache (c -> c .requestCache (new NullRequestCache ()));
7197
7298 return http .build ();
7399 }
74100
101+ @ Bean
102+ @ SuppressWarnings ({"java:S6437" , "deprecation" })
103+ public UserDetailsService userDetailsService ()
104+ {
105+ return new InMemoryUserDetailsManager (User .withDefaultPasswordEncoder ()
106+ .username ("test" )
107+ .password ("test" )
108+ .roles ("USER" )
109+ .build ());
110+ }
111+
75112 // Example CSP
76113 protected String getCSP ()
77114 {
0 commit comments