Skip to content

Commit 870d83e

Browse files
committed
PermitAllSupportTests groovy->java
Issue gh-4939
1 parent 1630b3b commit 870d83e

File tree

2 files changed

+93
-69
lines changed

2 files changed

+93
-69
lines changed

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

-69
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
* https://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 org.junit.Rule;
19+
import org.junit.Test;
20+
21+
import org.springframework.beans.factory.BeanCreationException;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
24+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
25+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
26+
import org.springframework.security.config.test.SpringTestRule;
27+
import org.springframework.test.web.servlet.MockMvc;
28+
29+
import static org.assertj.core.api.Assertions.assertThatCode;
30+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
31+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
32+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
33+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
34+
35+
/**
36+
* @author Rob Winch
37+
* @author Josh Cummings
38+
*
39+
*/
40+
public class PermitAllSupportTests {
41+
42+
@Rule
43+
public final SpringTestRule spring = new SpringTestRule();
44+
45+
@Autowired
46+
private MockMvc mvc;
47+
48+
@Test
49+
public void performWhenUsingPermitAllExactUrlRequestMatcherThenMatchesExactUrl() throws Exception {
50+
this.spring.register(PermitAllConfig.class).autowire();
51+
52+
this.mvc.perform(get("/app/xyz").contextPath("/app"))
53+
.andExpect(status().isNotFound());
54+
this.mvc.perform(get("/app/xyz?def").contextPath("/app"))
55+
.andExpect(status().isFound());
56+
this.mvc.perform(post("/app/abc?def").with(csrf()).contextPath("/app"))
57+
.andExpect(status().isNotFound());
58+
this.mvc.perform(get("/app/abc").with(csrf()).contextPath("/app"))
59+
.andExpect(status().isFound());
60+
}
61+
62+
@EnableWebSecurity
63+
static class PermitAllConfig extends WebSecurityConfigurerAdapter {
64+
@Override
65+
protected void configure(HttpSecurity http) throws Exception {
66+
http
67+
.authorizeRequests()
68+
.anyRequest().authenticated()
69+
.and()
70+
.formLogin()
71+
.loginPage("/xyz").permitAll()
72+
.loginProcessingUrl("/abc?def").permitAll();
73+
}
74+
}
75+
76+
@Test
77+
public void configureWhenNotAuthorizeRequestsThenException() {
78+
assertThatCode(() -> this.spring.register(NoAuthorizedUrlsConfig.class).autowire())
79+
.isInstanceOf(BeanCreationException.class)
80+
.hasMessageContaining("permitAll only works with HttpSecurity.authorizeRequests");
81+
}
82+
83+
@EnableWebSecurity
84+
static class NoAuthorizedUrlsConfig extends WebSecurityConfigurerAdapter {
85+
86+
@Override
87+
protected void configure(HttpSecurity http) throws Exception {
88+
http
89+
.formLogin()
90+
.permitAll();
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)