Merge branch 'eugenp:master' into PR-6910
This commit is contained in:
+2
@@ -25,6 +25,8 @@ import com.baeldung.testcontainers.support.MiddleEarthCharactersRepository;
|
||||
@Testcontainers
|
||||
@SpringBootTest(webEnvironment = DEFINED_PORT)
|
||||
@DirtiesContext(classMode = AFTER_CLASS)
|
||||
// Testcontainers require a valid docker installation.
|
||||
// When running the tests, ensure you have a valid Docker environment
|
||||
class DynamicPropertiesLiveTest {
|
||||
@Container
|
||||
static MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10"));
|
||||
|
||||
+3
@@ -8,6 +8,9 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.testcontainers.containers.MongoDBContainer;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
|
||||
|
||||
// Testcontainers require a valid docker installation.
|
||||
// When running the app locally, ensure you have a valid Docker environment
|
||||
class LocalDevApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+2
@@ -24,6 +24,8 @@ import com.baeldung.testcontainers.support.MiddleEarthCharactersRepository;
|
||||
@Testcontainers
|
||||
@SpringBootTest(webEnvironment = DEFINED_PORT)
|
||||
@DirtiesContext(classMode = AFTER_CLASS)
|
||||
// Testcontainers require a valid docker installation.
|
||||
// When running the tests, ensure you have a valid Docker environment
|
||||
class ServiceConnectionLiveTest {
|
||||
|
||||
@Container
|
||||
|
||||
@@ -102,7 +102,7 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<start-class>com.baeldung.keycloak.SpringBoot</start-class>
|
||||
<start-class>com.baeldung.keycloak.SpringBootKeycloakApp</start-class>
|
||||
<jaxb-runtime.version>4.0.0</jaxb-runtime.version>
|
||||
<wsdl4j.version>1.6.3</wsdl4j.version>
|
||||
<jaxb2-maven-plugin.version>2.5.0</jaxb2-maven-plugin.version>
|
||||
|
||||
+2
-3
@@ -6,11 +6,10 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@SpringBootApplication
|
||||
|
||||
public class SpringBoot {
|
||||
public class SpringBootKeycloakApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBoot.class, args);
|
||||
SpringApplication.run(SpringBootKeycloakApp.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
+1
-2
@@ -4,10 +4,9 @@ import org.junit.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import com.baeldung.keycloak.SpringBoot;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(classes = { SpringBoot.class })
|
||||
@SpringBootTest(classes = { SpringBootKeycloakApp.class })
|
||||
public class KeycloakContextIntegrationTest {
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
## Relevant Articles
|
||||
- [Difference Between permitAll() and anonymous() in Spring Security](https://www.baeldung.com/spring-security-permitall-vs-anonymous)
|
||||
@@ -0,0 +1,51 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-boot-security-2</artifactId>
|
||||
<name>spring-boot-security-1</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Spring Boot Security Auto-Configuration</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+1
-1
@@ -13,7 +13,7 @@ public class EcommerceController {
|
||||
return "Show Cart";
|
||||
}
|
||||
|
||||
//can we accessed by both anonymous and authenticated users
|
||||
//can be accessed by both anonymous and authenticated users
|
||||
@GetMapping("/public/showProducts")
|
||||
public @ResponseBody String listProducts() {
|
||||
return "List Products";
|
||||
@@ -11,7 +11,6 @@ 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
|
||||
|
||||
|
||||
+11
-7
@@ -3,10 +3,11 @@ package com.baeldung.swaggerkeycloak;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.config.Customizer;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||
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.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer;
|
||||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||||
import org.springframework.security.core.session.SessionRegistryImpl;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
|
||||
@@ -24,16 +25,19 @@ public class GlobalSecurityConfig {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.csrf()
|
||||
.disable()
|
||||
.authorizeRequests()
|
||||
.requestMatchers(HttpMethod.OPTIONS)
|
||||
|
||||
http.csrf(AbstractHttpConfigurer::disable)
|
||||
.authorizeHttpRequests((requests) -> requests.requestMatchers(HttpMethod.OPTIONS)
|
||||
.permitAll()
|
||||
.requestMatchers("/api/**")
|
||||
.authenticated()
|
||||
.anyRequest()
|
||||
.permitAll();
|
||||
http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
|
||||
.permitAll());
|
||||
|
||||
http.oauth2ResourceServer((oauth2) -> oauth2
|
||||
.jwt(Customizer.withDefaults())
|
||||
);
|
||||
|
||||
return http.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -13,4 +13,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
- [Spring Web Service Integration Tests with @WebServiceServerTest](https://www.baeldung.com/spring-webserviceservertest)
|
||||
- [Spring Boot – Testing Redis With Testcontainers](https://www.baeldung.com/spring-boot-redis-testcontainers)
|
||||
- [Spring Boot – Keycloak Integration Testing with Testcontainers](https://www.baeldung.com/spring-boot-keycloak-integration-testing)
|
||||
- [Difference Between @Spy and @SpyBean](https://www.baeldung.com/spring-spy-vs-spybean)
|
||||
- More articles: [[<-- prev]](../spring-boot-testing)
|
||||
|
||||
Reference in New Issue
Block a user