1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
import org .junit .Rule ;
19
19
import org .junit .Test ;
20
20
import org .springframework .beans .factory .annotation .Autowired ;
21
+ import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
21
22
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
22
23
import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
23
24
import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
24
25
import org .springframework .security .config .test .SpringTestRule ;
25
26
import org .springframework .security .core .annotation .AuthenticationPrincipal ;
27
+ import org .springframework .security .core .userdetails .PasswordEncodedUser ;
26
28
import org .springframework .test .web .servlet .MockMvc ;
27
29
import org .springframework .web .bind .annotation .GetMapping ;
28
30
import org .springframework .web .bind .annotation .RestController ;
29
31
import org .springframework .web .servlet .config .annotation .EnableWebMvc ;
30
32
33
+ import static org .springframework .security .config .Customizer .withDefaults ;
31
34
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
32
35
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
36
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
33
37
34
38
/**
35
39
* @author Rob Winch
@@ -44,7 +48,7 @@ public class AnonymousConfigurerTests {
44
48
45
49
@ Test
46
50
public void requestWhenAnonymousTwiceInvokedThenDoesNotOverride () throws Exception {
47
- this .spring .register (InvokeTwiceDoesNotOverride .class ).autowire ();
51
+ this .spring .register (InvokeTwiceDoesNotOverride .class , PrincipalController . class ).autowire ();
48
52
49
53
this .mockMvc .perform (get ("/" ))
50
54
.andExpect (content ().string ("principal" ));
@@ -63,13 +67,99 @@ protected void configure(HttpSecurity http) throws Exception {
63
67
.and ()
64
68
.anonymous ();
65
69
}
70
+ }
71
+
72
+ @ Test
73
+ public void requestWhenAnonymousPrincipalInLambdaThenPrincipalUsed () throws Exception {
74
+ this .spring .register (AnonymousPrincipalInLambdaConfig .class , PrincipalController .class ).autowire ();
75
+
76
+ this .mockMvc .perform (get ("/" ))
77
+ .andExpect (content ().string ("principal" ));
78
+ }
79
+
80
+ @ EnableWebSecurity
81
+ @ EnableWebMvc
82
+ static class AnonymousPrincipalInLambdaConfig extends WebSecurityConfigurerAdapter {
83
+
84
+ @ Override
85
+ protected void configure (HttpSecurity http ) throws Exception {
86
+ // @formatter:off
87
+ http
88
+ .anonymous (anonymous ->
89
+ anonymous
90
+ .principal ("principal" )
91
+ );
92
+ // @formatter:on
93
+ }
94
+ }
95
+
96
+ @ Test
97
+ public void requestWhenAnonymousDisabledInLambdaThenRespondsWithForbidden () throws Exception {
98
+ this .spring .register (AnonymousDisabledInLambdaConfig .class , PrincipalController .class ).autowire ();
99
+
100
+ this .mockMvc .perform (get ("/" ))
101
+ .andExpect (status ().isForbidden ());
102
+ }
103
+
104
+ @ EnableWebSecurity
105
+ static class AnonymousDisabledInLambdaConfig extends WebSecurityConfigurerAdapter {
106
+ @ Override
107
+ protected void configure (HttpSecurity http ) throws Exception {
108
+ // @formatter:off
109
+ http
110
+ .authorizeRequests (authorizeRequests ->
111
+ authorizeRequests
112
+ .anyRequest ().permitAll ()
113
+ )
114
+ .anonymous (AbstractHttpConfigurer ::disable );
115
+ // @formatter:on
116
+ }
117
+
118
+ protected void configure (AuthenticationManagerBuilder auth ) throws Exception {
119
+ // @formatter:off
120
+ auth
121
+ .inMemoryAuthentication ()
122
+ .withUser (PasswordEncodedUser .user ());
123
+ // @formatter:on
124
+ }
125
+ }
126
+
127
+ @ Test
128
+ public void requestWhenAnonymousWithDefaultsInLambdaThenRespondsWithOk () throws Exception {
129
+ this .spring .register (AnonymousWithDefaultsInLambdaConfig .class , PrincipalController .class ).autowire ();
130
+
131
+ this .mockMvc .perform (get ("/" ))
132
+ .andExpect (status ().isOk ());
133
+ }
134
+
135
+ @ EnableWebSecurity
136
+ static class AnonymousWithDefaultsInLambdaConfig extends WebSecurityConfigurerAdapter {
137
+ @ Override
138
+ protected void configure (HttpSecurity http ) throws Exception {
139
+ // @formatter:off
140
+ http
141
+ .authorizeRequests (authorizeRequests ->
142
+ authorizeRequests
143
+ .anyRequest ().permitAll ()
144
+ )
145
+ .anonymous (withDefaults ());
146
+ // @formatter:on
147
+ }
148
+
149
+ protected void configure (AuthenticationManagerBuilder auth ) throws Exception {
150
+ // @formatter:off
151
+ auth
152
+ .inMemoryAuthentication ()
153
+ .withUser (PasswordEncodedUser .user ());
154
+ // @formatter:on
155
+ }
156
+ }
66
157
67
- @ RestController
68
- static class PrincipalController {
69
- @ GetMapping ("/" )
70
- String principal (@ AuthenticationPrincipal String principal ) {
71
- return principal ;
72
- }
158
+ @ RestController
159
+ static class PrincipalController {
160
+ @ GetMapping ("/" )
161
+ String principal (@ AuthenticationPrincipal String principal ) {
162
+ return principal ;
73
163
}
74
164
}
75
165
}
0 commit comments