Skip to content

Commit 212e6fe

Browse files
chrisoberlepivovarit
authored andcommitted
BAEL-1969 spring security for spring boot integration tests (eugenp#5074)
* BAEL-1969 spring security for spring boot integration tests * BAEL-1969 spring security for spring boot integration tests
1 parent b7aa6a2 commit 212e6fe

9 files changed

Lines changed: 248 additions & 0 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.integrationtesting;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
5+
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;
6+
7+
@Configuration
8+
@EnableGlobalMethodSecurity(
9+
prePostEnabled = true,
10+
securedEnabled = true)
11+
public class MethodSecurityConfigurer extends GlobalMethodSecurityConfiguration {
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.integrationtesting;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SecuredApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SecuredApplication.class, args);
11+
}
12+
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.integrationtesting;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class SecuredController {
10+
11+
@GetMapping("/public/hello")
12+
public List<String> publicHello() {
13+
return Arrays.asList("Hello", "World", "from", "Public");
14+
}
15+
16+
@GetMapping("/private/hello")
17+
public List<String> privateHello() {
18+
return Arrays.asList("Hello", "World", "from", "Private");
19+
}
20+
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.integrationtesting;
2+
3+
import org.springframework.security.access.prepost.PreAuthorize;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class SecuredService {
8+
9+
@PreAuthorize("authenticated")
10+
public String sayHelloSecured() {
11+
return "Hello user.";
12+
}
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.integrationtesting;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
5+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
7+
8+
@Configuration
9+
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
10+
11+
@Override
12+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
13+
auth.inMemoryAuthentication()
14+
.withUser("spring")
15+
.password("secret")
16+
.roles("USER");
17+
}
18+
19+
@Override
20+
protected void configure(HttpSecurity http) throws Exception {
21+
http.authorizeRequests()
22+
.antMatchers("/private/**")
23+
.hasRole("USER")
24+
.antMatchers("/public/**")
25+
.permitAll()
26+
.and()
27+
.httpBasic();
28+
}
29+
30+
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.integrationtesting;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
9+
import org.springframework.boot.test.web.client.TestRestTemplate;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.http.ResponseEntity;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
14+
@RunWith(SpringRunner.class)
15+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
16+
public class SecuredControllerRestTemplateIntegrationTest {
17+
18+
@Autowired
19+
private TestRestTemplate template;
20+
21+
@Test
22+
public void givenRequestOnPrivateService_shouldFailWith401() throws Exception {
23+
ResponseEntity<String> result = template.getForEntity("/private/hello", String.class);
24+
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());
25+
}
26+
27+
@Test
28+
public void givenAuthRequestOnPrivateService_shouldSucceedWith200() throws Exception {
29+
ResponseEntity<String> result = template.withBasicAuth("spring", "secret")
30+
.getForEntity("/private/hello", String.class);
31+
assertEquals(HttpStatus.OK, result.getStatusCode());
32+
}
33+
34+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.integrationtesting;
2+
3+
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
4+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
13+
import org.springframework.http.MediaType;
14+
import org.springframework.security.test.context.support.WithMockUser;
15+
import org.springframework.test.context.junit4.SpringRunner;
16+
import org.springframework.test.web.servlet.MockMvc;
17+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
18+
import org.springframework.web.context.WebApplicationContext;
19+
20+
@RunWith(SpringRunner.class)
21+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
22+
public class SecuredControllerSpringBootIntegrationTest {
23+
24+
@Autowired
25+
private WebApplicationContext context;
26+
27+
private MockMvc mvc;
28+
29+
@Before
30+
public void setup() {
31+
mvc = MockMvcBuilders
32+
.webAppContextSetup(context)
33+
.apply(springSecurity())
34+
.build();
35+
}
36+
37+
@Test
38+
public void givenRequestOnPrivateService_shouldFailWith401() throws Exception {
39+
mvc.perform(get("/private/hello")
40+
.contentType(MediaType.APPLICATION_JSON))
41+
.andExpect(status().isUnauthorized());
42+
}
43+
44+
@WithMockUser("spring")
45+
@Test
46+
public void givenAuthRequestOnPrivateService_shouldSucceedWith200() throws Exception {
47+
mvc.perform(get("/private/hello")
48+
.contentType(MediaType.APPLICATION_JSON))
49+
.andExpect(status().isOk());
50+
}
51+
52+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.integrationtesting;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
5+
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
10+
import org.springframework.http.MediaType;
11+
import org.springframework.security.test.context.support.WithMockUser;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
import org.springframework.test.web.servlet.MockMvc;
14+
15+
import com.baeldung.integrationtesting.SecuredController;
16+
17+
@RunWith(SpringRunner.class)
18+
@WebMvcTest(SecuredController.class)
19+
public class SecuredControllerWebMvcIntegrationTest {
20+
21+
@Autowired
22+
private MockMvc mvc;
23+
24+
@Test
25+
public void givenRequestOnPrivateService_shouldFailWith401() throws Exception {
26+
mvc.perform(get("/private/hello")
27+
.contentType(MediaType.APPLICATION_JSON))
28+
.andExpect(status().isUnauthorized());
29+
}
30+
31+
@WithMockUser(value = "spring")
32+
@Test
33+
public void givenAuthRequestOnPrivateService_shouldSucceedWith200() throws Exception {
34+
mvc.perform(get("/private/hello")
35+
.contentType(MediaType.APPLICATION_JSON))
36+
.andExpect(status().isOk());
37+
}
38+
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.integrationtesting;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
10+
import org.springframework.security.test.context.support.WithMockUser;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
13+
import com.baeldung.integrationtesting.SecuredService;
14+
15+
@RunWith(SpringRunner.class)
16+
@SpringBootTest
17+
public class SecuredMethodSpringBootIntegrationTest {
18+
19+
@Autowired
20+
private SecuredService service;
21+
22+
@Test(expected = AuthenticationCredentialsNotFoundException.class)
23+
public void givenUnauthenticated_whenCallService_thenThrowsException() {
24+
service.sayHelloSecured();
25+
}
26+
27+
@WithMockUser(username="spring")
28+
@Test
29+
public void givenAuthenticated_whenCallServiceWithSecured_thenOk() {
30+
assertThat(service.sayHelloSecured()).isNotBlank();
31+
}
32+
}

0 commit comments

Comments
 (0)