Skip to content

Commit 1a02caf

Browse files
committed
NamespaceHttpAnonymousTests groovy->java
Issue: gh-4939
1 parent fe40e6d commit 1a02caf

File tree

2 files changed

+209
-129
lines changed

2 files changed

+209
-129
lines changed

config/src/test/groovy/org/springframework/security/config/annotation/web/configurers/NamespaceHttpAnonymousTests.groovy

-129
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.config.annotation.web.configurers;
17+
18+
import java.util.Optional;
19+
20+
import org.junit.Rule;
21+
import org.junit.Test;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.security.authentication.AnonymousAuthenticationToken;
25+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
26+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
27+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
28+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
29+
import org.springframework.security.config.test.SpringTestRule;
30+
import org.springframework.security.core.context.SecurityContext;
31+
import org.springframework.security.core.context.SecurityContextHolder;
32+
import org.springframework.security.core.userdetails.PasswordEncodedUser;
33+
import org.springframework.test.web.servlet.MockMvc;
34+
import org.springframework.web.bind.annotation.GetMapping;
35+
import org.springframework.web.bind.annotation.RestController;
36+
37+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
38+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
39+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
40+
41+
/**
42+
* Tests to verify that all the functionality of <anonymous> attributes is present
43+
*
44+
* @author Rob Winch
45+
* @author Josh Cummings
46+
*
47+
*/
48+
public class NamespaceHttpAnonymousTests {
49+
50+
@Autowired
51+
MockMvc mvc;
52+
53+
@Rule
54+
public final SpringTestRule spring = new SpringTestRule();
55+
56+
@Test
57+
public void anonymousRequestWhenUsingDefaultAnonymousConfigurationThenUsesAnonymousAuthentication()
58+
throws Exception {
59+
this.spring.register(AnonymousConfig.class, AnonymousController.class).autowire();
60+
this.mvc.perform(get("/type"))
61+
.andExpect(content().string(AnonymousAuthenticationToken.class.getSimpleName()));
62+
}
63+
64+
@EnableWebSecurity
65+
static class AnonymousConfig extends WebSecurityConfigurerAdapter {
66+
@Override
67+
protected void configure(HttpSecurity http) throws Exception {
68+
// @formatter:off
69+
http
70+
.authorizeRequests()
71+
.antMatchers("/type").anonymous()
72+
.anyRequest().denyAll();
73+
// @formatter:on
74+
}
75+
}
76+
77+
@Test
78+
public void anonymousRequestWhenDisablingAnonymousThenDenies()
79+
throws Exception {
80+
this.spring.register(AnonymousDisabledConfig.class, AnonymousController.class).autowire();
81+
this.mvc.perform(get("/type"))
82+
.andExpect(status().isForbidden());
83+
}
84+
85+
@EnableWebSecurity
86+
static class AnonymousDisabledConfig extends WebSecurityConfigurerAdapter {
87+
@Override
88+
protected void configure(HttpSecurity http) throws Exception {
89+
// @formatter:off
90+
http
91+
.authorizeRequests()
92+
.anyRequest().permitAll()
93+
.and()
94+
.anonymous().disable();
95+
// @formatter:on
96+
}
97+
98+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
99+
// @formatter:off
100+
auth
101+
.inMemoryAuthentication()
102+
.withUser(PasswordEncodedUser.user())
103+
.withUser(PasswordEncodedUser.admin());
104+
// @formatter:on
105+
}
106+
}
107+
108+
@Test
109+
public void requestWhenAnonymousThenSendsAnonymousConfiguredAuthorities()
110+
throws Exception {
111+
this.spring.register(AnonymousGrantedAuthorityConfig.class, AnonymousController.class).autowire();
112+
this.mvc.perform(get("/type"))
113+
.andExpect(content().string(AnonymousAuthenticationToken.class.getSimpleName()));
114+
}
115+
116+
@EnableWebSecurity
117+
static class AnonymousGrantedAuthorityConfig extends WebSecurityConfigurerAdapter {
118+
@Override
119+
protected void configure(HttpSecurity http) throws Exception {
120+
// @formatter:off
121+
http
122+
.authorizeRequests()
123+
.antMatchers("/type").hasRole("ANON")
124+
.anyRequest().denyAll()
125+
.and()
126+
.anonymous()
127+
.authorities("ROLE_ANON");
128+
// @formatter:on
129+
}
130+
}
131+
132+
@Test
133+
public void anonymousRequestWhenAnonymousKeyConfiguredThenKeyIsUsed() throws Exception {
134+
this.spring.register(AnonymousKeyConfig.class, AnonymousController.class).autowire();
135+
this.mvc.perform(get("/key"))
136+
.andExpect(content().string(String.valueOf("AnonymousKeyConfig".hashCode())));
137+
}
138+
139+
@EnableWebSecurity
140+
static class AnonymousKeyConfig extends WebSecurityConfigurerAdapter {
141+
@Override
142+
protected void configure(HttpSecurity http) throws Exception {
143+
// @formatter:off
144+
http
145+
.authorizeRequests()
146+
.antMatchers("/key").anonymous()
147+
.anyRequest().denyAll()
148+
.and()
149+
.anonymous().key("AnonymousKeyConfig");
150+
// @formatter:on
151+
}
152+
}
153+
154+
@Test
155+
public void anonymousRequestWhenAnonymousUsernameConfiguredThenUsernameIsUsed() throws Exception {
156+
this.spring.register(AnonymousUsernameConfig.class, AnonymousController.class).autowire();
157+
this.mvc.perform(get("/principal"))
158+
.andExpect(content().string("AnonymousUsernameConfig"));
159+
}
160+
161+
@EnableWebSecurity
162+
static class AnonymousUsernameConfig extends WebSecurityConfigurerAdapter {
163+
@Override
164+
protected void configure(HttpSecurity http) throws Exception {
165+
// @formatter:off
166+
http
167+
.authorizeRequests()
168+
.antMatchers("/principal").anonymous()
169+
.anyRequest().denyAll()
170+
.and()
171+
.anonymous().principal("AnonymousUsernameConfig");
172+
// @formatter:on
173+
}
174+
}
175+
176+
@RestController
177+
static class AnonymousController {
178+
@GetMapping("/type")
179+
String type() {
180+
return anonymousToken()
181+
.map(AnonymousAuthenticationToken::getClass)
182+
.map(Class::getSimpleName)
183+
.orElse(null);
184+
}
185+
186+
@GetMapping("/key")
187+
String key() {
188+
return anonymousToken()
189+
.map(AnonymousAuthenticationToken::getKeyHash)
190+
.map(String::valueOf)
191+
.orElse(null);
192+
}
193+
194+
@GetMapping("/principal")
195+
String principal() {
196+
return anonymousToken()
197+
.map(AnonymousAuthenticationToken::getName)
198+
.orElse(null);
199+
}
200+
201+
Optional<AnonymousAuthenticationToken> anonymousToken() {
202+
return Optional.of(SecurityContextHolder.getContext())
203+
.map(SecurityContext::getAuthentication)
204+
.filter(a -> a instanceof AnonymousAuthenticationToken)
205+
.map(AnonymousAuthenticationToken.class::cast);
206+
}
207+
}
208+
209+
}

0 commit comments

Comments
 (0)