Skip to content

Commit da0f969

Browse files
committed
NamespaceExpressionHandlerTests groovy->java
Issue: spring-projectsgh-4939
1 parent 9642d33 commit da0f969

File tree

2 files changed

+113
-56
lines changed

2 files changed

+113
-56
lines changed

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

-56
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
19+
import java.security.Principal;
20+
21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.expression.ExpressionParser;
28+
import org.springframework.expression.spel.standard.SpelExpressionParser;
29+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
30+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
31+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
32+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
33+
import org.springframework.security.config.test.SpringTestRule;
34+
import org.springframework.security.test.context.annotation.SecurityTestExecutionListeners;
35+
import org.springframework.security.test.context.support.WithMockUser;
36+
import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler;
37+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
38+
import org.springframework.test.web.servlet.MockMvc;
39+
import org.springframework.web.bind.annotation.GetMapping;
40+
import org.springframework.web.bind.annotation.RestController;
41+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
42+
43+
import static org.mockito.Mockito.spy;
44+
import static org.mockito.Mockito.verify;
45+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
46+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
47+
48+
/**
49+
* Tests to verify that all the functionality of <expression-handler> attributes is present
50+
*
51+
* @author Rob Winch
52+
* @author Josh Cummings
53+
*
54+
*/
55+
@RunWith(SpringJUnit4ClassRunner.class)
56+
@SecurityTestExecutionListeners
57+
public class NamespaceHttpExpressionHandlerTests {
58+
59+
@Rule
60+
public final SpringTestRule spring = new SpringTestRule();
61+
62+
@Autowired
63+
MockMvc mvc;
64+
65+
@Test
66+
@WithMockUser
67+
public void getWhenHasCustomExpressionHandlerThenMatchesNamespace() throws Exception {
68+
this.spring.register(ExpressionHandlerController.class, ExpressionHandlerConfig.class).autowire();
69+
this.mvc.perform(get("/whoami")).andExpect(content().string("user"));
70+
verifyBean("expressionParser", ExpressionParser.class).parseExpression("hasRole('USER')");
71+
}
72+
73+
@EnableWebMvc
74+
@EnableWebSecurity
75+
private static class ExpressionHandlerConfig extends WebSecurityConfigurerAdapter {
76+
public ExpressionHandlerConfig() {}
77+
78+
@Override
79+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
80+
auth
81+
.inMemoryAuthentication()
82+
.withUser("rod").password("password").roles("USER", "ADMIN");
83+
}
84+
85+
@Override
86+
protected void configure(HttpSecurity http) throws Exception {
87+
DefaultWebSecurityExpressionHandler handler = new DefaultWebSecurityExpressionHandler();
88+
handler.setExpressionParser(expressionParser());
89+
90+
http
91+
.authorizeRequests()
92+
.expressionHandler(handler)
93+
.anyRequest().access("hasRole('USER')");
94+
}
95+
96+
@Bean
97+
ExpressionParser expressionParser() {
98+
return spy(new SpelExpressionParser());
99+
}
100+
}
101+
102+
@RestController
103+
private static class ExpressionHandlerController {
104+
@GetMapping("/whoami")
105+
String whoami(Principal user) {
106+
return user.getName();
107+
}
108+
}
109+
110+
private <T> T verifyBean(String beanName, Class<T> beanClass) {
111+
return verify(this.spring.getContext().getBean(beanName, beanClass));
112+
}
113+
}

0 commit comments

Comments
 (0)