8
8
import java .util .List ;
9
9
import java .util .Map ;
10
10
11
+ import org .slf4j .Logger ;
12
+ import org .slf4j .LoggerFactory ;
11
13
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
12
14
import org .springframework .context .annotation .Bean ;
13
15
import org .springframework .context .annotation .Configuration ;
14
16
import org .springframework .security .config .Customizer ;
15
17
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
16
18
import 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 ;
17
22
import org .springframework .security .web .SecurityFilterChain ;
18
23
import org .springframework .security .web .header .writers .ReferrerPolicyHeaderWriter ;
19
24
import org .springframework .security .web .savedrequest .NullRequestCache ;
28
33
@ EnableConfigurationProperties (AdditionalOAuth2ClientProperties .class )
29
34
public class MainWebSecurity
30
35
{
31
- @ Bean (name = "mainSecurityFilterChainBean" )
32
- public SecurityFilterChain configure (
36
+ private static final Logger LOG = LoggerFactory .getLogger (MainWebSecurity .class );
37
+
38
+ protected void customizeLogin (
33
39
final HttpSecurity http ,
34
40
final AdditionalOAuth2ClientProperties additionalOAuth2ClientProperties ) throws Exception
35
41
{
36
- http .with (new AdvancedLoginPageAdapter <>(http ), c -> c
42
+ http .with (
43
+ new AdvancedLoginPageAdapter <>(http ), c -> c
37
44
.customizePages (p -> p
38
45
// No remote communication -> Use local resources
39
46
.setHeaderElements (List .of (
@@ -60,18 +67,48 @@ public SecurityFilterChain configure(
60
67
+ " href='https://xdev.software' target='_blank'>"
61
68
+ " XDEV Software"
62
69
+ " </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
64
81
.headers (h -> h
65
82
.referrerPolicy (r -> r .policy (ReferrerPolicyHeaderWriter .ReferrerPolicy .SAME_ORIGIN ))
66
83
.contentSecurityPolicy (csp -> csp .policyDirectives (this .getCSP ())))
67
84
.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" ))
68
94
.oauth2Login (c -> c .defaultSuccessUrl ("/" ))
69
95
.authorizeHttpRequests (urlRegistry -> urlRegistry .anyRequest ().authenticated ())
70
96
.requestCache (c -> c .requestCache (new NullRequestCache ()));
71
97
72
98
return http .build ();
73
99
}
74
100
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
+
75
112
// Example CSP
76
113
protected String getCSP ()
77
114
{
0 commit comments