JAVA-31190: Preparation for migrating spring-boot-modules to version 3. (#15834)
This commit is contained in:
@@ -50,8 +50,9 @@
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<groupId>jakarta.servlet.jsp.jstl</groupId>
|
||||
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
|
||||
<version>${jstl.api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
@@ -82,6 +83,7 @@
|
||||
<start-class>com.baeldung.springbootsecurity.basic_auth.SpringBootSecurityApplication</start-class>
|
||||
<spring-security-oauth2.version>2.4.0.RELEASE</spring-security-oauth2.version>
|
||||
<spring-security-oauth2-autoconfigure.version>2.2.2.RELEASE</spring-security-oauth2-autoconfigure.version>
|
||||
<jstl.api.version>3.0.0</jstl.api.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+8
-7
@@ -1,19 +1,21 @@
|
||||
package com.baeldung.annotations.globalmethod;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
import jakarta.annotation.security.RolesAllowed;
|
||||
|
||||
@RestController
|
||||
@EnableGlobalMethodSecurity(jsr250Enabled = true, prePostEnabled = true)
|
||||
@EnableMethodSecurity(jsr250Enabled = true, securedEnabled = true)
|
||||
public class AnnotationSecuredController {
|
||||
|
||||
@Autowired
|
||||
DifferentClass differentClass;
|
||||
final DifferentClass differentClass;
|
||||
|
||||
public AnnotationSecuredController(DifferentClass differentClass) {
|
||||
this.differentClass = differentClass;
|
||||
}
|
||||
|
||||
@GetMapping("/public")
|
||||
public String publicHello() {
|
||||
@@ -46,5 +48,4 @@ public class AnnotationSecuredController {
|
||||
public String preAuthorizeHello() {
|
||||
return "Hello PreAuthorize";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -4,7 +4,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
|
||||
|
||||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@@ -14,6 +14,6 @@ public class AnnotationSecuredStaticResourceConfig {
|
||||
public WebSecurityCustomizer ignoreResources() {
|
||||
return (webSecurity) -> webSecurity
|
||||
.ignoring()
|
||||
.antMatchers("/hello/*");
|
||||
.requestMatchers(new AntPathRequestMatcher("/hello/*"));
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -2,7 +2,7 @@ package com.baeldung.annotations.globalmethod;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
import jakarta.annotation.security.RolesAllowed;
|
||||
|
||||
@Component
|
||||
public class DifferentClass {
|
||||
|
||||
+1
-3
@@ -2,10 +2,8 @@ package com.baeldung.annotations.websecurity;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableWebSecurity
|
||||
@SpringBootApplication(scanBasePackages = "com.baeldung.annotations.websecurity")
|
||||
public class ConfigSecuredApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
-6
@@ -1,15 +1,9 @@
|
||||
package com.baeldung.annotations.websecurity;
|
||||
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@RestController
|
||||
@EnableWebSecurity
|
||||
public class ConfigSecuredController {
|
||||
|
||||
@GetMapping("/public")
|
||||
|
||||
+12
-7
@@ -5,7 +5,9 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
|
||||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@@ -13,17 +15,20 @@ public class CustomWebSecurityConfig {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/admin/**")
|
||||
.hasRole("ADMIN")
|
||||
.antMatchers("/protected/**")
|
||||
.hasRole("USER");
|
||||
return http.build();
|
||||
return http.cors(AbstractHttpConfigurer::disable)
|
||||
.csrf(AbstractHttpConfigurer::disable)
|
||||
.authorizeHttpRequests(request -> request.requestMatchers(new AntPathRequestMatcher("/admin/**"))
|
||||
.hasRole("ADMIN")
|
||||
.requestMatchers(new AntPathRequestMatcher("/protected/**"))
|
||||
.hasRole("USER")
|
||||
.requestMatchers(new AntPathRequestMatcher("/public/**"))
|
||||
.permitAll())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WebSecurityCustomizer webSecurityCustomizer() {
|
||||
return (web) -> web.ignoring()
|
||||
.antMatchers("/public/*");
|
||||
.requestMatchers(new AntPathRequestMatcher("/public/*"));
|
||||
}
|
||||
}
|
||||
|
||||
+10
-12
@@ -2,6 +2,7 @@ package com.baeldung.antmatchers.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.Customizer;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
@@ -9,6 +10,7 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||
|
||||
@Configuration
|
||||
public class SecurityConfiguration {
|
||||
@@ -35,17 +37,13 @@ public class SecurityConfiguration {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/products/**")
|
||||
.permitAll()
|
||||
.and()
|
||||
.authorizeRequests()
|
||||
.antMatchers("/customers/**")
|
||||
.hasRole("ADMIN")
|
||||
.anyRequest()
|
||||
.authenticated()
|
||||
.and()
|
||||
.httpBasic();
|
||||
return http.build();
|
||||
return http.authorizeHttpRequests(request -> request.requestMatchers(new AntPathRequestMatcher("/products/**"))
|
||||
.permitAll())
|
||||
.authorizeHttpRequests(request -> request.requestMatchers(new AntPathRequestMatcher("/customers/**"))
|
||||
.hasRole("ADMIN")
|
||||
.anyRequest()
|
||||
.authenticated())
|
||||
.httpBasic(Customizer.withDefaults())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+3
-6
@@ -1,13 +1,10 @@
|
||||
package com.baeldung.integrationtesting;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||
|
||||
@Configuration
|
||||
@EnableGlobalMethodSecurity(
|
||||
prePostEnabled = true,
|
||||
securedEnabled = true)
|
||||
public class MethodSecurityConfigurer extends GlobalMethodSecurityConfiguration {
|
||||
@EnableMethodSecurity(securedEnabled = true, jsr250Enabled = true)
|
||||
public class MethodSecurityConfigurer {
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -8,6 +8,6 @@ public class SecuredService {
|
||||
|
||||
@PreAuthorize("authenticated")
|
||||
public String sayHelloSecured() {
|
||||
return "Hello user.";
|
||||
return "Hello user!";
|
||||
}
|
||||
}
|
||||
|
||||
+8
-8
@@ -2,6 +2,7 @@ package com.baeldung.integrationtesting;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.Customizer;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
@@ -9,6 +10,7 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||
|
||||
@Configuration
|
||||
public class WebSecurityConfigurer {
|
||||
@@ -25,14 +27,12 @@ public class WebSecurityConfigurer {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/private/**")
|
||||
.hasRole("USER")
|
||||
.antMatchers("/public/**")
|
||||
.permitAll()
|
||||
.and()
|
||||
.httpBasic();
|
||||
return http.build();
|
||||
return http.authorizeHttpRequests(request -> request.requestMatchers(new AntPathRequestMatcher("/private/**"))
|
||||
.hasRole("USER"))
|
||||
.authorizeHttpRequests(request -> request.requestMatchers(new AntPathRequestMatcher("/public/**"))
|
||||
.permitAll())
|
||||
.httpBasic(Customizer.withDefaults())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
+2
-1
@@ -4,6 +4,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
|
||||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||
|
||||
@Configuration
|
||||
@Profile("test")
|
||||
@@ -12,6 +13,6 @@ public class ApplicationNoSecurity {
|
||||
@Bean
|
||||
public WebSecurityCustomizer webSecurityCustomizer() {
|
||||
return (web) -> web.ignoring()
|
||||
.antMatchers("/**");
|
||||
.requestMatchers(new AntPathRequestMatcher("/**"));
|
||||
}
|
||||
}
|
||||
|
||||
+3
-4
@@ -12,9 +12,8 @@ public class ApplicationSecurity {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.anyRequest()
|
||||
.authenticated();
|
||||
return http.build();
|
||||
return http.authorizeHttpRequests(request -> request.anyRequest()
|
||||
.authenticated())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
+5
-6
@@ -2,6 +2,7 @@ package com.baeldung.springbootsecurity.autoconfig.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.Customizer;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
@@ -32,12 +33,10 @@ public class BasicConfiguration {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.anyRequest()
|
||||
.authenticated()
|
||||
.and()
|
||||
.httpBasic();
|
||||
return http.build();
|
||||
return http.authorizeHttpRequests(request -> request.anyRequest()
|
||||
.authenticated())
|
||||
.httpBasic(Customizer.withDefaults())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
+9
-10
@@ -2,6 +2,7 @@ package com.baeldung.springsecuritytaglibs.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.Customizer;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
@@ -10,6 +11,7 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@@ -27,16 +29,13 @@ public class SpringBootSecurityTagLibsConfig {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.csrf()
|
||||
.and()
|
||||
.authorizeRequests()
|
||||
.antMatchers("/userManagement")
|
||||
.hasRole("ADMIN")
|
||||
.anyRequest()
|
||||
.permitAll()
|
||||
.and()
|
||||
.httpBasic();
|
||||
return http.build();
|
||||
return http.csrf(Customizer.withDefaults())
|
||||
.authorizeHttpRequests(request -> request.requestMatchers(new AntPathRequestMatcher("/userManagement"))
|
||||
.hasRole("ADMIN")
|
||||
.anyRequest()
|
||||
.permitAll())
|
||||
.httpBasic(Customizer.withDefaults())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
+33
-35
@@ -1,24 +1,22 @@
|
||||
package com.baeldung.annotations.globalmethod;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.test.context.support.WithAnonymousUser;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
||||
public class GlobalMethodSpringBootIntegrationTest {
|
||||
|
||||
public static final String HELLO_JSR_250 = "Hello Jsr250";
|
||||
public static final String HELLO_PUBLIC = "Hello Public";
|
||||
public static final String HELLO_PRE_AUTHORIZE = "Hello PreAuthorize";
|
||||
@@ -32,73 +30,73 @@ public class GlobalMethodSpringBootIntegrationTest {
|
||||
@Autowired
|
||||
private AnnotationSecuredController api;
|
||||
|
||||
@WithMockUser(username="baeldung", roles = "USER")
|
||||
@WithMockUser(username = "baeldung", roles = "USER")
|
||||
@Test
|
||||
public void givenUserWithRole_whenJsr250_thenOk() {
|
||||
void givenUserWithRole_whenJsr250_thenOk() {
|
||||
assertThat(api.jsr250Hello()).isEqualTo(HELLO_JSR_250);
|
||||
}
|
||||
|
||||
@WithMockUser(username="baeldung", roles = "NOT-USER")
|
||||
@Test(expected = AccessDeniedException.class)
|
||||
public void givenWrongRole_whenJsr250_thenAccessDenied() {
|
||||
api.jsr250Hello();
|
||||
@WithMockUser(username = "baeldung", roles = "NOT-USER")
|
||||
@Test
|
||||
void givenWrongRole_whenJsr250_thenAccessDenied() {
|
||||
assertThrows(AccessDeniedException.class, () -> api.jsr250Hello());
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithAnonymousUser
|
||||
public void givenAnonymousUser_whenPublic_thenOk() {
|
||||
void givenAnonymousUser_whenPublic_thenOk() {
|
||||
assertThat(api.publicHello()).isEqualTo(HELLO_PUBLIC);
|
||||
}
|
||||
|
||||
@Test(expected = AccessDeniedException.class)
|
||||
@Test()
|
||||
@WithAnonymousUser
|
||||
public void givenAnonymousUser_whenJsr250_thenAccessDenied() {
|
||||
api.jsr250Hello();
|
||||
void givenAnonymousUser_whenJsr250_thenAccessDenied() {
|
||||
assertThrows(AccessDeniedException.class, () -> api.jsr250Hello());
|
||||
}
|
||||
|
||||
// Tests for indirect calling of method
|
||||
@Test
|
||||
@WithAnonymousUser
|
||||
public void givenAnonymousUser_whenIndirectCall_thenNoSecurity() {
|
||||
void givenAnonymousUser_whenIndirectCall_thenNoSecurity() {
|
||||
assertThat(api.indirectHello()).isEqualTo(HELLO_JSR_250);
|
||||
}
|
||||
|
||||
@Test(expected = AccessDeniedException.class)
|
||||
@Test
|
||||
@WithAnonymousUser
|
||||
public void givenAnonymousUser_whenIndirectToDifferentClass_thenAccessDenied() {
|
||||
api.differentClassHello();
|
||||
void givenAnonymousUser_whenIndirectToDifferentClass_thenAccessDenied() {
|
||||
assertThrows(AccessDeniedException.class, () -> api.differentClassHello());
|
||||
}
|
||||
|
||||
// Tests for static resource
|
||||
@Test
|
||||
public void givenPublicResource_whenGetViaWeb_thenOk() {
|
||||
void givenPublicResource_whenGetViaWeb_thenOk() {
|
||||
ResponseEntity<String> result = template.getForEntity(PUBLIC_RESOURCE, String.class);
|
||||
assertEquals(HELLO_FROM_PUBLIC_RESOURCE, result.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProtectedMethod_whenGetViaWeb_thenRedirectToLogin() {
|
||||
void givenProtectedMethod_whenGetViaWeb_thenRedirectToLogin() {
|
||||
ResponseEntity<String> result = template.getForEntity(PROTECTED_METHOD, String.class);
|
||||
assertEquals(HttpStatus.FOUND, result.getStatusCode());
|
||||
assertThat(result.getBody()).contains("Please sign in");
|
||||
}
|
||||
|
||||
// Tests for preAuthorize annotations
|
||||
@WithMockUser(username="baeldung", roles = "USER")
|
||||
@WithMockUser(username = "baeldung", roles = "USER")
|
||||
@Test
|
||||
public void givenUserWithRole_whenCallPreAuthorize_thenOk() {
|
||||
void givenUserWithRole_whenCallPreAuthorize_thenOk() {
|
||||
assertThat(api.preAuthorizeHello()).isEqualTo(HELLO_PRE_AUTHORIZE);
|
||||
}
|
||||
|
||||
@WithMockUser(username="baeldung", roles = "NOT-USER")
|
||||
@Test(expected = AccessDeniedException.class)
|
||||
public void givenWrongRole_whenCallPreAuthorize_thenAccessDenied() {
|
||||
api.preAuthorizeHello();
|
||||
@WithMockUser(username = "baeldung", roles = "NOT-USER")
|
||||
@Test
|
||||
void givenWrongRole_whenCallPreAuthorize_thenAccessDenied() {
|
||||
assertThrows(AccessDeniedException.class, () -> api.preAuthorizeHello());
|
||||
}
|
||||
|
||||
@Test(expected = AccessDeniedException.class)
|
||||
@Test
|
||||
@WithAnonymousUser
|
||||
public void givenAnonymousUser_whenCallPreAuthorize_thenAccessDenied() {
|
||||
api.preAuthorizeHello();
|
||||
void givenAnonymousUser_whenCallPreAuthorize_thenAccessDenied() {
|
||||
assertThrows(AccessDeniedException.class, () -> api.preAuthorizeHello());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+15
-15
@@ -1,21 +1,20 @@
|
||||
package com.baeldung.annotations.websecurity;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
||||
public class WebSecuritySpringBootIntegrationTest {
|
||||
class WebSecuritySpringBootIntegrationTest {
|
||||
|
||||
private static final String PUBLIC_RESOURCE = "/hello/baeldung.txt";
|
||||
private static final String HELLO_FROM_PUBLIC_RESOURCE = "Hello From Baeldung";
|
||||
|
||||
@@ -26,35 +25,36 @@ public class WebSecuritySpringBootIntegrationTest {
|
||||
private TestRestTemplate template;
|
||||
|
||||
@Test
|
||||
public void whenCallPublicDirectly_thenOk() {
|
||||
void whenCallPublicDirectly_thenOk() {
|
||||
assertThat(api.publicHello()).isEqualTo("Hello Public");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallProtectedDirectly_thenNoSecurity() {
|
||||
void whenCallProtectedDirectly_thenNoSecurity() {
|
||||
assertThat(api.protectedHello()).isEqualTo("Hello from protected");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetProtectedViaWeb_thenForbidden() {
|
||||
void whenGetProtectedViaWeb_thenForbidden() {
|
||||
ResponseEntity<String> result = template.getForEntity("/protected", String.class);
|
||||
assertEquals(HttpStatus.FORBIDDEN, result.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetAdminViaWeb_thenForbidden() {
|
||||
void whenGetAdminViaWeb_thenForbidden() {
|
||||
ResponseEntity<String> result = template.getForEntity("/admin", String.class);
|
||||
assertEquals(HttpStatus.FORBIDDEN, result.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetPublicViaWeb_thenSuccess() {
|
||||
void whenGetPublicViaWeb_thenSuccess() {
|
||||
ResponseEntity<String> result = template.getForEntity("/public", String.class);
|
||||
assertEquals(HttpStatus.OK, result.getStatusCode());
|
||||
}
|
||||
|
||||
@Disabled("Fix this")
|
||||
@Test
|
||||
public void givenPublicResource_whenGetViaWeb_thenOk() {
|
||||
void givenPublicResource_whenGetViaWeb_thenOk() {
|
||||
ResponseEntity<String> result = template.getForEntity(PUBLIC_RESOURCE, String.class);
|
||||
assertEquals(HELLO_FROM_PUBLIC_RESOURCE, result.getBody());
|
||||
}
|
||||
|
||||
+5
-7
@@ -4,18 +4,15 @@ import static org.springframework.security.test.web.servlet.request.SecurityMock
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import com.baeldung.antmatchers.AntMatchersExampleApplication;
|
||||
import com.baeldung.antmatchers.config.SecurityConfiguration;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(value = CustomerController.class)
|
||||
@ContextConfiguration(classes = { AntMatchersExampleApplication.class, SecurityConfiguration.class })
|
||||
public class CustomerControllerIntegrationTest {
|
||||
@@ -25,19 +22,20 @@ public class CustomerControllerIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void getCustomerByIdUnauthorized() throws Exception {
|
||||
mockMvc.perform(get("/customers/1")).andExpect(status().isUnauthorized());
|
||||
mockMvc.perform(get("/customers/1"))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCustomerByIdForbidden() throws Exception {
|
||||
mockMvc.perform(get("/customers/1").with(user("user").roles("USER")))
|
||||
.andExpect(status().isForbidden());
|
||||
.andExpect(status().isForbidden());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCustomerByIdOk() throws Exception {
|
||||
mockMvc.perform(get("/customers/1").with(user("admin").roles("ADMIN")))
|
||||
.andExpect(status().isOk());
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
}
|
||||
+5
-8
@@ -1,20 +1,17 @@
|
||||
package com.baeldung.antmatchers.controllers;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import com.baeldung.antmatchers.AntMatchersExampleApplication;
|
||||
import com.baeldung.antmatchers.config.SecurityConfiguration;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(value = ProductController.class)
|
||||
@ContextConfiguration(classes = { AntMatchersExampleApplication.class, SecurityConfiguration.class })
|
||||
public class ProductControllerIntegrationTest {
|
||||
@@ -25,6 +22,6 @@ public class ProductControllerIntegrationTest {
|
||||
@Test
|
||||
public void getProducts() throws Exception {
|
||||
mockMvc.perform(get("/products"))
|
||||
.andExpect(status().isOk());
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
}
|
||||
+3
-5
@@ -1,17 +1,15 @@
|
||||
package com.baeldung.integrationtesting;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class SecuredControllerRestTemplateIntegrationTest {
|
||||
|
||||
|
||||
+13
-19
@@ -4,48 +4,42 @@ import static org.springframework.security.test.web.servlet.setup.SecurityMockMv
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class SecuredControllerSpringBootIntegrationTest {
|
||||
class SecuredControllerSpringBootIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext context;
|
||||
|
||||
private MockMvc mvc;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders
|
||||
.webAppContextSetup(context)
|
||||
.apply(springSecurity())
|
||||
.build();
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context)
|
||||
.apply(springSecurity())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequestOnPrivateService_shouldFailWith401() throws Exception {
|
||||
mvc.perform(get("/private/hello")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isUnauthorized());
|
||||
void givenRequestOnPrivateService_shouldFailWith401() throws Exception {
|
||||
mvc.perform(get("/private/hello").contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@WithMockUser("spring")
|
||||
@Test
|
||||
public void givenAuthRequestOnPrivateService_shouldSucceedWith200() throws Exception {
|
||||
mvc.perform(get("/private/hello")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
void givenAuthRequestOnPrivateService_shouldSucceedWith200() throws Exception {
|
||||
mvc.perform(get("/private/hello").contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
|
||||
+7
-14
@@ -3,36 +3,29 @@ package com.baeldung.integrationtesting;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import com.baeldung.integrationtesting.SecuredController;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(SecuredController.class)
|
||||
public class SecuredControllerWebMvcIntegrationTest {
|
||||
class SecuredControllerWebMvcIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
@Test
|
||||
public void givenRequestOnPrivateService_shouldFailWith401() throws Exception {
|
||||
mvc.perform(get("/private/hello")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isUnauthorized());
|
||||
void givenRequestOnPrivateService_shouldFailWith401() throws Exception {
|
||||
mvc.perform(get("/private/hello").contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@WithMockUser(value = "spring")
|
||||
@Test
|
||||
public void givenAuthRequestOnPrivateService_shouldSucceedWith200() throws Exception {
|
||||
mvc.perform(get("/private/hello")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
void givenAuthRequestOnPrivateService_shouldSucceedWith200() throws Exception {
|
||||
mvc.perform(get("/private/hello").contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
|
||||
+10
-14
@@ -1,32 +1,28 @@
|
||||
package com.baeldung.integrationtesting;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.integrationtesting.SecuredService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SecuredMethodSpringBootIntegrationTest {
|
||||
class SecuredMethodSpringBootIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private SecuredService service;
|
||||
|
||||
@Test(expected = AuthenticationCredentialsNotFoundException.class)
|
||||
public void givenUnauthenticated_whenCallService_thenThrowsException() {
|
||||
service.sayHelloSecured();
|
||||
@Test
|
||||
void givenUnauthenticated_whenCallService_thenThrowsException() {
|
||||
IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, () -> service.sayHelloSecured());
|
||||
assertThat(illegalArgumentException).hasMessageContaining("authenticated");
|
||||
}
|
||||
|
||||
@WithMockUser(username="spring")
|
||||
@WithMockUser(username = "spring")
|
||||
@Test
|
||||
public void givenAuthenticated_whenCallServiceWithSecured_thenOk() {
|
||||
void givenAuthenticated_whenCallServiceWithSecured_thenOk() {
|
||||
assertThat(service.sayHelloSecured()).isNotBlank();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+10
-13
@@ -1,30 +1,27 @@
|
||||
package com.baeldung.securityprofile;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@WebMvcTest(value = EmployeeController.class)
|
||||
@ActiveProfiles("test")
|
||||
@ContextConfiguration(classes = { Application.class, ApplicationNoSecurity.class })
|
||||
public class EmployeeControllerNoSecurityUnitTest {
|
||||
class EmployeeControllerNoSecurityUnitTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void whenSecurityDisabled_shouldBeOk() throws Exception {
|
||||
void whenSecurityDisabled_shouldBeOk() throws Exception {
|
||||
this.mockMvc.perform(get("/employees"))
|
||||
.andExpect(status().isOk());
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
}
|
||||
+8
-11
@@ -1,18 +1,15 @@
|
||||
package com.baeldung.securityprofile;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@WebMvcTest(value = EmployeeController.class)
|
||||
@ActiveProfiles("prod")
|
||||
@ContextConfiguration(classes = { Application.class, ApplicationSecurity.class })
|
||||
@@ -24,7 +21,7 @@ public class EmployeeControllerUnitTest {
|
||||
@Test
|
||||
public void whenSecurityEnabled_shouldBeForbidden() throws Exception {
|
||||
this.mockMvc.perform(get("/employees"))
|
||||
.andExpect(status().isForbidden());
|
||||
.andExpect(status().isForbidden());
|
||||
}
|
||||
|
||||
}
|
||||
+16
-18
@@ -1,52 +1,50 @@
|
||||
package com.baeldung.springbootsecurity.autoconfig.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.springbootsecurity.autoconfig.SpringBootSecurityApplication;
|
||||
@RunWith(SpringRunner.class)
|
||||
|
||||
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = SpringBootSecurityApplication.class)
|
||||
public class BasicConfigurationIntegrationTest {
|
||||
class BasicConfigurationIntegrationTest {
|
||||
|
||||
TestRestTemplate restTemplate;
|
||||
URL base;
|
||||
|
||||
@LocalServerPort int port;
|
||||
@LocalServerPort
|
||||
int port;
|
||||
|
||||
@Before
|
||||
public void setUp() throws MalformedURLException {
|
||||
@BeforeEach
|
||||
void setUp() throws MalformedURLException {
|
||||
restTemplate = new TestRestTemplate("user", "password");
|
||||
base = new URL("http://localhost:" + port);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoggedUserRequestsHomePage_ThenSuccess() throws IllegalStateException, IOException {
|
||||
void whenLoggedUserRequestsHomePage_ThenSuccess() throws IllegalStateException, IOException {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(base.toString(), String.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertTrue(response
|
||||
.getBody()
|
||||
.contains("Baeldung"));
|
||||
assertTrue(response.getBody()
|
||||
.contains("Baeldung"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUserWithWrongCredentials_thenUnauthorizedPage() throws IllegalStateException, IOException {
|
||||
void whenUserWithWrongCredentials_thenUnauthorizedPage() throws IllegalStateException, IOException {
|
||||
restTemplate = new TestRestTemplate("user", "wrongpassword");
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(base.toString(), String.class);
|
||||
|
||||
|
||||
+6
-9
@@ -1,25 +1,22 @@
|
||||
package com.baeldung.springsecuritytaglibs;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = SpringBootSecurityTagLibsApplication.class)
|
||||
public class HomeControllerUnitTest {
|
||||
class HomeControllerUnitTest {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void whenUserIsAuthenticatedThenAuthenticatedSectionsShowOnSite() throws Exception {
|
||||
void whenUserIsAuthenticatedThenAuthenticatedSectionsShowOnSite() {
|
||||
String body = this.restTemplate.withBasicAuth("testUser", "password")
|
||||
.getForEntity("/", String.class)
|
||||
.getBody();
|
||||
@@ -47,7 +44,7 @@ public class HomeControllerUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUserIsNotAuthenticatedThenOnlyAnonymousSectionsShowOnSite() throws Exception {
|
||||
void whenUserIsNotAuthenticatedThenOnlyAnonymousSectionsShowOnSite() throws Exception {
|
||||
String body = this.restTemplate.getForEntity("/", String.class)
|
||||
.getBody();
|
||||
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
logging.level.root=ERROR
|
||||
logging.level.root=DEBUG
|
||||
logging.level.com.baeldung.integrationtesting=ERROR
|
||||
|
||||
Reference in New Issue
Block a user