Skip to content

Commit b64aea3

Browse files
committed
Add a test for the AuthenticationPrincipal support
1 parent a715e72 commit b64aea3

File tree

3 files changed

+150
-3
lines changed

3 files changed

+150
-3
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2010-2014 Ralph Schaer <[email protected]>
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 ch.ralscha.extdirectspring_itest;
17+
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
20+
import org.springframework.security.core.context.SecurityContextHolder;
21+
import org.springframework.security.core.userdetails.UserDetails;
22+
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
23+
import org.springframework.stereotype.Controller;
24+
import org.springframework.web.bind.annotation.RequestMapping;
25+
import org.springframework.web.bind.annotation.ResponseBody;
26+
27+
@Controller
28+
public class LoginController {
29+
30+
private final InMemoryUserDetailsManager userManager;
31+
32+
@Autowired
33+
public LoginController(InMemoryUserDetailsManager userManager) {
34+
this.userManager = userManager;
35+
}
36+
37+
@RequestMapping("/login")
38+
@ResponseBody
39+
public void login() {
40+
UserDetails ud = userManager.loadUserByUsername("jimi");
41+
if (ud != null) {
42+
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
43+
ud, null, ud.getAuthorities());
44+
SecurityContextHolder.getContext().setAuthentication(token);
45+
}
46+
}
47+
48+
}

src/test/java/ch/ralscha/extdirectspring_itest/SecuredService.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import org.springframework.format.annotation.DateTimeFormat;
2323
import org.springframework.security.access.prepost.PreAuthorize;
24+
import org.springframework.security.core.userdetails.UserDetails;
25+
import org.springframework.security.web.bind.annotation.AuthenticationPrincipal;
2426
import org.springframework.stereotype.Service;
2527

2628
import ch.ralscha.extdirectspring.annotation.ExtDirectMethod;
@@ -30,9 +32,16 @@ public class SecuredService {
3032

3133
@ExtDirectMethod(group = "secured")
3234
@PreAuthorize("isAnonymous()")
33-
public String setDate(String id, @DateTimeFormat(pattern = "dd/MM/yyyy") Date date) {
35+
public String setDate(String id, @DateTimeFormat(pattern = "dd/MM/yyyy") Date date,
36+
@AuthenticationPrincipal UserDetails ud) {
3437
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
35-
return id + "," + dateFormat.format(date);
38+
return id + "," + dateFormat.format(date) + ","
39+
+ (ud != null ? ud.getUsername() : "");
3640
}
3741

42+
@ExtDirectMethod(group = "secured")
43+
@PreAuthorize("isAuthenticated()")
44+
public String secret(String param, @AuthenticationPrincipal UserDetails ud) {
45+
return param.toUpperCase() + "," + (ud != null ? ud.getUsername() : "");
46+
}
3847
}

src/test/java/ch/ralscha/extdirectspring_itest/SecuredServiceTest.java

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.commons.io.IOUtils;
2424
import org.apache.http.HttpEntity;
2525
import org.apache.http.client.methods.CloseableHttpResponse;
26+
import org.apache.http.client.methods.HttpGet;
2627
import org.apache.http.client.methods.HttpPost;
2728
import org.apache.http.entity.StringEntity;
2829
import org.apache.http.impl.client.CloseableHttpClient;
@@ -65,7 +66,7 @@ public void callSetDate() throws IOException, JsonParseException,
6566
Map<String, Object> rootAsMap = mapper.readValue(
6667
responseString.substring(1, responseString.length() - 1), Map.class);
6768
assertThat(rootAsMap).hasSize(5);
68-
assertThat(rootAsMap.get("result")).isEqualTo("102,26.04.2012");
69+
assertThat(rootAsMap.get("result")).isEqualTo("102,26.04.2012,");
6970
assertThat(rootAsMap.get("method")).isEqualTo("setDate");
7071
assertThat(rootAsMap.get("type")).isEqualTo("rpc");
7172
assertThat(rootAsMap.get("action")).isEqualTo("securedService");
@@ -77,4 +78,93 @@ public void callSetDate() throws IOException, JsonParseException,
7778
}
7879
}
7980

81+
@Test
82+
public void callSecretNotLoggedIn() throws IOException, JsonParseException,
83+
JsonMappingException {
84+
85+
CloseableHttpClient client = HttpClientBuilder.create().build();
86+
CloseableHttpResponse response = null;
87+
try {
88+
89+
HttpPost post = new HttpPost("http://localhost:9998/controller/router");
90+
91+
StringEntity postEntity = new StringEntity(
92+
"{\"action\":\"securedService\",\"method\":\"secret\",\"data\":[\"ralph\"],\"type\":\"rpc\",\"tid\":1}",
93+
"UTF-8");
94+
95+
post.setEntity(postEntity);
96+
post.setHeader("Content-Type", "application/json; charset=UTF-8");
97+
98+
response = client.execute(post);
99+
HttpEntity entity = response.getEntity();
100+
assertThat(entity).isNotNull();
101+
String responseString = EntityUtils.toString(entity);
102+
103+
assertThat(responseString).isNotNull();
104+
assertThat(responseString.startsWith("[") && responseString.endsWith("]"))
105+
.isTrue();
106+
ObjectMapper mapper = new ObjectMapper();
107+
Map<String, Object> rootAsMap = mapper.readValue(
108+
responseString.substring(1, responseString.length() - 1), Map.class);
109+
assertThat(rootAsMap).hasSize(5);
110+
assertThat(rootAsMap.get("message")).isEqualTo("Server Error");
111+
assertThat(rootAsMap.get("method")).isEqualTo("secret");
112+
assertThat(rootAsMap.get("type")).isEqualTo("exception");
113+
assertThat(rootAsMap.get("action")).isEqualTo("securedService");
114+
assertThat(rootAsMap.get("tid")).isEqualTo(1);
115+
}
116+
finally {
117+
IOUtils.closeQuietly(response);
118+
IOUtils.closeQuietly(client);
119+
}
120+
}
121+
122+
@Test
123+
public void callSecretLoggedIn() throws IOException, JsonParseException,
124+
JsonMappingException {
125+
126+
CloseableHttpClient client = HttpClientBuilder.create().build();
127+
CloseableHttpResponse response = null;
128+
try {
129+
130+
HttpGet login = new HttpGet("http://localhost:9998/controller/login");
131+
response = client.execute(login);
132+
HttpEntity entity = response.getEntity();
133+
String responseString = EntityUtils.toString(entity);
134+
System.out.println(responseString);
135+
response.close();
136+
137+
HttpPost post = new HttpPost("http://localhost:9998/controller/router");
138+
139+
StringEntity postEntity = new StringEntity(
140+
"{\"action\":\"securedService\",\"method\":\"secret\",\"data\":[\"ralph\"],\"type\":\"rpc\",\"tid\":1}",
141+
"UTF-8");
142+
143+
post.setEntity(postEntity);
144+
post.setHeader("Content-Type", "application/json; charset=UTF-8");
145+
146+
response = client.execute(post);
147+
entity = response.getEntity();
148+
assertThat(entity).isNotNull();
149+
responseString = EntityUtils.toString(entity);
150+
151+
assertThat(responseString).isNotNull();
152+
assertThat(responseString.startsWith("[") && responseString.endsWith("]"))
153+
.isTrue();
154+
ObjectMapper mapper = new ObjectMapper();
155+
Map<String, Object> rootAsMap = mapper.readValue(
156+
responseString.substring(1, responseString.length() - 1), Map.class);
157+
assertThat(rootAsMap).hasSize(5);
158+
assertThat(rootAsMap.get("result")).isEqualTo("RALPH,jimi");
159+
assertThat(rootAsMap.get("method")).isEqualTo("secret");
160+
assertThat(rootAsMap.get("type")).isEqualTo("rpc");
161+
assertThat(rootAsMap.get("action")).isEqualTo("securedService");
162+
assertThat(rootAsMap.get("tid")).isEqualTo(1);
163+
}
164+
finally {
165+
IOUtils.closeQuietly(response);
166+
IOUtils.closeQuietly(client);
167+
}
168+
}
169+
80170
}

0 commit comments

Comments
 (0)