Merge remote-tracking branch 'origin/PR-6910' into PR-6910
# Conflicts: # spring-boot-modules/spring-boot-3/pom.xml
This commit is contained in:
@@ -104,6 +104,7 @@
|
||||
<module>spring-boot-springdoc-2</module>
|
||||
<module>spring-boot-documentation</module>
|
||||
<module>spring-boot-3-url-matching</module>
|
||||
<module>spring-boot-graalvm-docker</module>
|
||||
</modules>
|
||||
|
||||
<dependencyManagement>
|
||||
|
||||
@@ -41,6 +41,27 @@
|
||||
<artifactId>mockserver-netty</artifactId>
|
||||
<version>${mockserver.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${jupiter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${jupiter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${jupiter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
<version>${jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mock-server</groupId>
|
||||
<artifactId>mockserver-client-java</artifactId>
|
||||
@@ -256,9 +277,47 @@
|
||||
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
|
||||
<start-class>com.baeldung.sample.TodoApplication</start-class>
|
||||
<mockserver.version>5.14.0</mockserver.version>
|
||||
<spring-boot.version>3.1.0</spring-boot.version>
|
||||
<spring-boot.version>3.2.0-SNAPSHOT</spring-boot.version>
|
||||
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
|
||||
<jupiter.version>5.10.0</jupiter.version>
|
||||
<testcontainers.version>1.18.3</testcontainers.version>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.restclient;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Article {
|
||||
Integer id;
|
||||
String title;
|
||||
|
||||
public Article(Integer id, String title) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Article article = (Article) o;
|
||||
return Objects.equals(id, article.id) && Objects.equals(title, article.title);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, title);
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.restclient;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/articles")
|
||||
public class ArticleController {
|
||||
Map<Integer, Article> database = new HashMap<>();
|
||||
|
||||
@GetMapping
|
||||
public Collection<Article> getArticles() {
|
||||
return database.values();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Article getArticle(@PathVariable Integer id) {
|
||||
return database.get(id);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public void createArticle(@RequestBody Article article) {
|
||||
database.put(article.getId(), article);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public void updateArticle(@PathVariable Integer id, @RequestBody Article article) {
|
||||
assert Objects.equals(id, article.getId());
|
||||
database.remove(id);
|
||||
database.put(id, article);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void deleteArticle(@PathVariable Integer id) {
|
||||
database.remove(id);
|
||||
}
|
||||
@DeleteMapping()
|
||||
public void deleteArticles() {
|
||||
database.clear();
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.restclient;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class RestClientApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RestClientApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
package com.baeldung.restclient;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class RestClientIntegrationTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
private String uriBase;
|
||||
RestClient restClient = RestClient.create();
|
||||
|
||||
@BeforeAll
|
||||
public void setup() {
|
||||
uriBase = "http://localhost:" + port;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void teardown() {
|
||||
restClient.delete()
|
||||
.uri(uriBase + "/articles")
|
||||
.retrieve()
|
||||
.toBodilessEntity();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldGetArticlesAndReturnString() {
|
||||
String articlesAsString = restClient.get()
|
||||
.uri(uriBase + "/articles")
|
||||
.retrieve()
|
||||
.body(String.class);
|
||||
|
||||
assertThat(articlesAsString).isEqualTo("[]");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldPostAndGetArticles() {
|
||||
Article article = new Article(1, "How to use RestClient");
|
||||
restClient.post()
|
||||
.uri(uriBase + "/articles")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(article)
|
||||
.retrieve()
|
||||
.toBodilessEntity();
|
||||
|
||||
List<Article> articles = restClient.get()
|
||||
.uri(uriBase + "/articles")
|
||||
.retrieve()
|
||||
.body(new ParameterizedTypeReference<>() {});
|
||||
|
||||
assertThat(articles).isEqualTo(List.of(article));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldPostAndPutAndGetArticles() {
|
||||
Article article = new Article(1, "How to use RestClient");
|
||||
restClient.post()
|
||||
.uri(uriBase + "/articles")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(article)
|
||||
.retrieve()
|
||||
.toBodilessEntity();
|
||||
|
||||
Article articleChanged = new Article(1, "How to use RestClient even better");
|
||||
restClient.put()
|
||||
.uri(uriBase + "/articles/1")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(articleChanged)
|
||||
.retrieve()
|
||||
.toBodilessEntity();
|
||||
|
||||
List<Article> articles = restClient.get()
|
||||
.uri(uriBase + "/articles")
|
||||
.retrieve()
|
||||
.body(new ParameterizedTypeReference<>() {});
|
||||
|
||||
assertThat(articles).isEqualTo(List.of(articleChanged));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldPostAndDeleteArticles() {
|
||||
Article article = new Article(1, "How to use RestClient");
|
||||
restClient.post()
|
||||
.uri(uriBase + "/articles")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(article)
|
||||
.retrieve()
|
||||
.toBodilessEntity();
|
||||
|
||||
restClient.delete()
|
||||
.uri(uriBase + "/articles")
|
||||
.retrieve()
|
||||
.toBodilessEntity();
|
||||
|
||||
List<Article> articles = restClient.get()
|
||||
.uri(uriBase + "/articles")
|
||||
.retrieve()
|
||||
.body(new ParameterizedTypeReference<>() {});
|
||||
|
||||
assertThat(articles).isEqualTo(List.of());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
FROM ubuntu:jammy
|
||||
COPY target/springboot-graalvm-docker /springboot-graalvm-docker
|
||||
CMD ["/springboot-graalvm-docker"]
|
||||
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
</parent>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-boot-graalvm-docker</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<name>spring-boot-graalvm-docker</name>
|
||||
<description>Spring Boot GrralVM with Docker</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.graalvm.buildtools</groupId>
|
||||
<artifactId>native-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.graalvmdockerimage;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GraalvmDockerImageApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GraalvmDockerImageApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@RestController
|
||||
class HelloController {
|
||||
|
||||
@GetMapping
|
||||
public String hello() {
|
||||
return "Hello GraalVM";
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
|
||||
@@ -11,10 +11,9 @@
|
||||
<description>This is a simple application demonstrating integration between Keycloak and Spring Boot.</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -11,10 +11,9 @@
|
||||
<description>This is a simple application demonstrating integration between Keycloak and Spring Boot.</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -11,10 +11,9 @@
|
||||
<description>This is a simple application demonstrating integration between Keycloak and Spring Boot.</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -9,10 +9,9 @@
|
||||
<description>Demo project for Spring Boot Logging with Log4J2</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -10,10 +10,9 @@
|
||||
<description>Module For Spring Boot Integration with BIRT</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -11,7 +11,7 @@ This module contains articles about Spring Boot Security
|
||||
- [Disable Security for a Profile in Spring Boot](https://www.baeldung.com/spring-security-disable-profile)
|
||||
- [Spring @EnableWebSecurity vs. @EnableGlobalMethodSecurity](https://www.baeldung.com/spring-enablewebsecurity-vs-enableglobalmethodsecurity)
|
||||
- [Spring Security – Configuring Different URLs](https://www.baeldung.com/spring-security-configuring-urls)
|
||||
|
||||
- [Difference Between permitAll() and anonymous() in Spring Security](https://www.baeldung.com/spring-security-permitall-vs-anonymous)
|
||||
|
||||
### Spring Boot Security Auto-Configuration
|
||||
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.permitallanonymous;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan("com.baeldung.permitallanonymous.*")
|
||||
public class SecuredEcommerceApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SecuredEcommerceApplication.class, args);
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.permitallanonymous.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class EcommerceController {
|
||||
|
||||
//can be accessed by only logged-in users
|
||||
@GetMapping("/private/showCart")
|
||||
public @ResponseBody String showCart() {
|
||||
return "Show Cart";
|
||||
}
|
||||
|
||||
//can we accessed by both anonymous and authenticated users
|
||||
@GetMapping("/public/showProducts")
|
||||
public @ResponseBody String listProducts() {
|
||||
return "List Products";
|
||||
}
|
||||
|
||||
//can be access by only anonymous users not by authenticated users
|
||||
@GetMapping("/public/registerUser")
|
||||
public @ResponseBody String registerUser() {
|
||||
return "Register User";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.permitallanonymous.filter;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
public class AuditInterceptor extends OncePerRequestFilter {
|
||||
private final Logger logger = LoggerFactory.getLogger(AuditInterceptor.class);
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
||||
throws ServletException, IOException {
|
||||
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication instanceof AnonymousAuthenticationToken) {
|
||||
logger.info("Audit anonymous user");
|
||||
}
|
||||
if (authentication instanceof UsernamePasswordAuthenticationToken) {
|
||||
logger.info("Audit registered user");
|
||||
}
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.permitallanonymous.security;
|
||||
|
||||
import com.baeldung.permitallanonymous.filter.AuditInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
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.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
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.authentication.AnonymousAuthenticationFilter;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class EcommerceWebSecurityConfig {
|
||||
@Bean
|
||||
public InMemoryUserDetailsManager userDetailsService(PasswordEncoder passwordEncoder) {
|
||||
UserDetails user = User.withUsername("spring")
|
||||
.password(passwordEncoder.encode("secret"))
|
||||
.roles("USER")
|
||||
.build();
|
||||
|
||||
return new InMemoryUserDetailsManager(user);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.addFilterAfter(new AuditInterceptor(), AnonymousAuthenticationFilter.class)
|
||||
.authorizeRequests()
|
||||
.antMatchers("/private/**").authenticated().and().httpBasic()
|
||||
.and().authorizeRequests()
|
||||
.antMatchers("/public/showProducts").permitAll()
|
||||
.antMatchers("/public/registerUser").anonymous();
|
||||
|
||||
return http.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BCryptPasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.permitallanonymous;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.security.test.context.support.WithAnonymousUser;
|
||||
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.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SecuredEcommerceApplication.class)
|
||||
@AutoConfigureMockMvc
|
||||
public class SecureEcommerceApplicationUnitTest {
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
private static final Logger logger = LoggerFactory.getLogger(SecureEcommerceApplicationUnitTest.class);
|
||||
|
||||
@WithAnonymousUser
|
||||
@Test
|
||||
public void givenAnonymousUser_whenAccessToUserRegisterPage_thenAllowAccess() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/public/registerUser"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.content().string("Register User"));
|
||||
}
|
||||
|
||||
@WithMockUser(username = "spring", password = "secret")
|
||||
@Test
|
||||
public void givenAuthenticatedUser_whenAccessToUserRegisterPage_thenDenyAccess() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/public/registerUser"))
|
||||
.andExpect(MockMvcResultMatchers.status().isForbidden());
|
||||
}
|
||||
|
||||
@WithMockUser(username = "spring", password = "secret")
|
||||
@Test
|
||||
public void givenAuthenticatedUser_whenAccessToProductLinePage_thenAllowAccess() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/public/showProducts"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.content().string("List Products"));
|
||||
}
|
||||
|
||||
@WithAnonymousUser
|
||||
@Test
|
||||
public void givenAnonymousUser_whenAccessToProductLinePage_thenAllowAccess() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/public/showProducts"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.content().string("List Products"));
|
||||
}
|
||||
|
||||
@WithMockUser(username = "spring", password = "secret")
|
||||
@Test
|
||||
public void givenAuthenticatedUser_whenAccessToCartPage_thenAllowAccess() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/private/showCart"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.content().string("Show Cart"));
|
||||
}
|
||||
|
||||
@WithAnonymousUser
|
||||
@Test
|
||||
public void givenAnonymousUser_whenAccessToCartPage_thenDenyAccess() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/private/showCart"))
|
||||
.andExpect(MockMvcResultMatchers.status().isUnauthorized());
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,10 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.1.3</version>
|
||||
<relativePath/>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
</parent>
|
||||
<artifactId>springbootsslbundles</artifactId>
|
||||
<name>spring-boot-ssl-bundles</name>
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class SSLBundleApplicationTests {
|
||||
class SpringContextTest {
|
||||
@Test
|
||||
void contextLoads() {
|
||||
|
||||
Reference in New Issue
Block a user