Merge branch 'eugenp:master' into master

This commit is contained in:
Wynn Teo 2024-02-14 09:55:42 +08:00 committed by GitHub
commit 2d18d5a529
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
44 changed files with 600 additions and 215 deletions

View File

@ -31,15 +31,21 @@
<version>${jackson-databind.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-hikaricp</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>${hikari.cp.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
@ -70,11 +76,12 @@
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<hibernate.version>5.4.21.Final</hibernate.version>
<hibernate.version>6.4.2.Final</hibernate.version>
<aws-lambda-java-core.version>1.2.0</aws-lambda-java-core.version>
<aws-lambda-java-events.version>3.1.0</aws-lambda-java-events.version>
<jackson-databind.version>2.11.2</jackson-databind.version>
<postgresql.version>42.2.16</postgresql.version>
<hikari.cp.version>5.1.0</hikari.cp.version>
</properties>
</project>

View File

@ -83,7 +83,7 @@ private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
}
private static SessionFactory createSessionFactory() {
Map<String, String> settings = new HashMap<>();
Map<String, Object> settings = new HashMap<>();
settings.put(URL, System.getenv("DB_URL"));
settings.put(DIALECT, "org.hibernate.dialect.PostgreSQLDialect");
settings.put(DEFAULT_SCHEMA, "shipping");

View File

@ -1,7 +1,7 @@
package com.baeldung.lambda.shipping;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
@Embeddable
public class Checkin {

View File

@ -1,10 +1,10 @@
package com.baeldung.lambda.shipping;
import javax.persistence.*;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
import static javax.persistence.FetchType.EAGER;
import static jakarta.persistence.FetchType.EAGER;
@Entity(name = "consignment")
@Table(name = "consignment")

View File

@ -1,7 +1,7 @@
package com.baeldung.lambda.shipping;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
@Embeddable
public class Item {

View File

@ -63,7 +63,7 @@ public class VectorAPIExamples {
public float[] scalarNormOfTwoArrays(float[] arr1, float[] arr2) {
float[] finalResult = new float[arr1.length];
for (int i = 0; i < arr1.length; i++) {
finalResult[i] = (arr1[i] * arr1[i] + arr2[i] * arr2[i]) * -1.0f;
finalResult[i] = (float) Math.sqrt(arr1[i] * arr1[i] + arr2[i] * arr2[i]);
}
return finalResult;
}
@ -77,13 +77,13 @@ public class VectorAPIExamples {
var vb = FloatVector.fromArray(PREFERRED_SPECIES, arr2, i);
var vc = va.mul(va)
.add(vb.mul(vb))
.neg();
.sqrt();
vc.intoArray(finalResult, i);
}
// tail cleanup
for (; i < arr1.length; i++) {
finalResult[i] = (arr1[i] * arr1[i] + arr2[i] * arr2[i]) * -1.0f;
finalResult[i] = (float) Math.sqrt(arr1[i] * arr1[i] + arr2[i] * arr2[i]);
}
return finalResult;
}

View File

@ -28,7 +28,7 @@ public class VectorAPIUnitTest {
public void whenTwoValuesProvided_thenComputeScalarNorm() {
float[] arr1 = { 1, 2.3f };
float[] arr2 = { 1.3f, 2.0f };
float[] result = { -2.6899998f, -9.29f };
float[] result = { 1.6401219f, 3.047950f };
Assertions.assertArrayEquals(result, vector.scalarNormOfTwoArrays(arr1, arr2));
}
@ -36,7 +36,7 @@ public class VectorAPIUnitTest {
public void whenTwoValuesProvided_thenComputeVectorNorm() {
float[] arr1 = { 1, 2.3f };
float[] arr2 = { 1.3f, 2.0f };
float[] result = { -2.6899998f, -9.29f };
float[] result = { 1.6401219f, 3.047950f };
Assertions.assertArrayEquals(result, vector.vectorNormalForm(arr1, arr2));
}

View File

@ -0,0 +1,111 @@
package com.baeldung.map.entrysettolinkedhashmap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class EntrySetToLinkedHashMapUnitTest {
private Map<Integer, String> map;
@Test
void givenMap_whenUsingCollectorsGroupingBy_thenCollectToLinkedHashMap() {
Map<String, Set<String>> countryToCities = Map.of("Paris", "France", "Nice", "France", "Madrid", "Spain")
.entrySet()
.stream()
.collect(Collectors.groupingBy(Map.Entry::getValue, LinkedHashMap::new, Collectors.mapping(Map.Entry::getKey, Collectors.toSet())));
assertThat(countryToCities).isExactlyInstanceOf(LinkedHashMap.class)
.containsOnly(entry("France", Set.of("Paris", "Nice")), entry("Spain", Set.of("Madrid")));
}
@Test
void givenMap_whenUsingCollectorsToMap_thenCollectAndConvertToLinkedHashMap() {
Map<Integer, String> result = new LinkedHashMap<>(map.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
assertThat(result).isExactlyInstanceOf(LinkedHashMap.class)
.containsOnly(entry(1, "value 1"), entry(2, "value 2"));
}
@Test
void givenMap_whenUsingCollectorsToMap_thenCollectToLinkedHashMap() {
Map<Integer, String> result = map.entrySet()
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey, Map.Entry::getValue,
(e1, e2) -> {
throw new RuntimeException();
},
LinkedHashMap::new));
assertThat(result).isExactlyInstanceOf(LinkedHashMap.class)
.containsOnly(entry(1, "value 1"), entry(2, "value 2"));
}
@Test
void givenMap_whenUsingLinkedHashMapConstructor_thenObtainLinkedHashMap() {
Map<Integer, String> result = new LinkedHashMap<>(map);
assertThat(result).isExactlyInstanceOf(LinkedHashMap.class)
.containsOnly(entry(1, "value 1"), entry(2, "value 2"));
}
@Test
void givenMap_whenUsingPutWithForLoop_thenInsertIntoLinkedHashMap() {
Map<Integer, String> result = new LinkedHashMap<>();
for (Map.Entry<Integer, String> entry : map.entrySet()) {
result.put(entry.getKey(), entry.getValue());
}
assertThat(result).isExactlyInstanceOf(LinkedHashMap.class)
.containsOnly(entry(1, "value 1"), entry(2, "value 2"));
}
@Test
void givenMap_whenUsingPutWithMapForEach_thenInsertIntoLinkedHashMap() {
Map<Integer, String> result = new LinkedHashMap<>();
map.forEach((k, v) -> result.put(k, v));
assertThat(result).isExactlyInstanceOf(LinkedHashMap.class)
.containsOnly(entry(1, "value 1"), entry(2, "value 2"));
}
@Test
void givenMap_whenUsingPutWithSetForEach_thenInsertIntoLinkedHashMap() {
Map<Integer, String> result = new LinkedHashMap<>();
map.entrySet()
.forEach(entry -> result.put(entry.getKey(), entry.getValue()));
assertThat(result).isExactlyInstanceOf(LinkedHashMap.class)
.containsOnly(entry(1, "value 1"), entry(2, "value 2"));
}
@Test
void givenMap_whenUsingPutWithStreamForEach_thenInsertIntoLinkedHashMapp() {
Map<Integer, String> result = new LinkedHashMap<>();
map.entrySet()
.stream()
.forEach(entry -> result.put(entry.getKey(), entry.getValue()));
assertThat(result).isExactlyInstanceOf(LinkedHashMap.class)
.containsOnly(entry(1, "value 1"), entry(2, "value 2"));
}
@BeforeEach
void init() {
map = Map.of(1, "value 1", 2, "value 2");
}
}

View File

@ -9,3 +9,4 @@
- [How to Log to the Console in Color](https://www.baeldung.com/java-log-console-in-color)
- [Create Table Using ASCII in a Console in Java](https://www.baeldung.com/java-console-ascii-make-table)
- [Printing Message on Console without Using main() Method in Java](https://www.baeldung.com/java-no-main-print-message-console)
- [Guide to System.in.read()](https://www.baeldung.com/java-system-in-read)

View File

@ -0,0 +1,45 @@
package com.baeldung.systemin;
import java.io.IOException;
class SystemInRead {
static void readSingleCharacter() {
System.out.println("Enter a character:");
try {
int input = System.in.read();
System.out.println((char) input);
}
catch (IOException e) {
System.err.println("Error reading input: " + e.getMessage());
}
}
static void readMultipleCharacters() {
System.out.println("Enter characters (Press 'Enter' to quit):");
try {
int input;
while ((input = System.in.read()) != '\n') {
System.out.print((char) input);
}
} catch (IOException e) {
System.err.println("Error reading input: " + e.getMessage());
}
}
static void readWithParameters() {
try {
byte[] byteArray = new byte[5];
int bytesRead;
int totalBytesRead = 0;
while ((bytesRead = System.in.read(byteArray, 0, byteArray.length)) != -1) {
System.out.print("Data read: " + new String(byteArray, 0, bytesRead));
totalBytesRead += bytesRead;
}
System.out.println("\nBytes Read: " + totalBytesRead);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.systemin;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.jupiter.api.Test;
public class SystemInReadUnitTest {
@Test
void givenUserInput_whenUsingReadMultipleCharacters_thenRead() {
System.setIn(new ByteArrayInputStream("Hello\n".getBytes()));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStream));
SystemInRead.readMultipleCharacters();
assertEquals("Enter characters (Press 'Enter' to quit):\n" + "Hello", outputStream.toString().trim());
}
@Test
void givenUserInput_whenUsingReadSingleCharacter_thenRead() {
System.setIn(new ByteArrayInputStream("A".getBytes()));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStream));
SystemInRead.readSingleCharacter();
assertEquals("Enter a character:\nA", outputStream.toString().trim());
}
@Test
void givenUserInput_whenUsingReadWithParameters_thenRead() {
System.setIn(new ByteArrayInputStream("ABC".getBytes()));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStream));
SystemInRead.readWithParameters();
assertEquals("Data read: ABC\n" + "Bytes Read: 3", outputStream.toString().trim());
}
}

View File

@ -0,0 +1,2 @@
### Relevant Articles:

View File

@ -0,0 +1,72 @@
<?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>core-java-string-operations-8</artifactId>
<name>core-java-string-operations-8</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache.commons.lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<apache.commons.lang3.version>3.13.0</apache.commons.lang3.version>
<commons-text.version>1.10.0</commons-text.version>
</properties>
</project>

View File

@ -0,0 +1,53 @@
package com.baeldung.emailandphonemasking;
import org.junit.jupiter.api.Test;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class EmailAndPhoneMaskingUnitTest {
String phoneNumber = "+12344567890";
String expectedMaskedPhoneNumber = "+*******7890";
String email = "testemailaddress@example.com";
String expectedMaskedEmail = "te**************@example.com";
@Test
public void givenEmailAddress_whenUsingStringManipulation_thenMaskEmail() {
int atIndex = email.indexOf('@');
String repeatedString = IntStream.range(0, atIndex - 2).mapToObj(i -> "*").collect(Collectors.joining());
String maskedPart = email.substring(0, atIndex - repeatedString.length()) + repeatedString;
String maskedEmail = maskedPart + email.substring(atIndex);
assertEquals(expectedMaskedEmail, maskedEmail);
}
@Test
public void givenEmailAddress_whenUsingRegex_thenMaskEmail() {
int atIndex = email.indexOf('@');
String regex = "(.{2})(.*)(@.*)";
String repeatedAsterisks = "*".repeat(atIndex - 2);
String maskedEmail = email.replaceAll(regex, "$1" + repeatedAsterisks + "$3");
assertEquals(expectedMaskedEmail, maskedEmail);
}
@Test
public void givenPhoneNumber_whenUsingStringManipulation_thenMaskPhone() {
String maskedPhoneNumber = phoneNumber.replaceAll("\\d(?=\\d{4})", "*");
assertEquals(expectedMaskedPhoneNumber, maskedPhoneNumber);
}
@Test
public void givenPhoneNumber_whenUsingRegex_thenMaskPhone() {
int lastDigitsIndex = phoneNumber.length() - 5;
String regex = "(\\+)(\\d+)(\\d{4})";
String repeatedAsterisks = "*".repeat(Math.max(0, lastDigitsIndex));
String maskedPhoneNumber = phoneNumber.replaceAll(regex, "$1" + repeatedAsterisks + "$3");
assertEquals(expectedMaskedPhoneNumber, maskedPhoneNumber);
}
}

View File

@ -33,6 +33,7 @@
<!-- <module>core-java-modules/core-java-14</module> --> <!-- JAVA-26056 -->
<!-- <module>core-java-modules/core-java-16</module> --> <!-- JAVA-26056 -->
<!-- <module>core-java-modules/core-java-17</module> --> <!-- JAVA-26056 -->
<!-- <module>core-java-modules/core-java-18</module> --> <!-- JAVA-26056 -->
<!-- <module>core-java-modules/core-java-19</module> --> <!-- JAVA-26056 -->
<module>core-java-numbers-conversions</module>
<module>core-java-9-improvements</module>
@ -199,6 +200,7 @@
<module>core-java-string-operations-2</module>
<module>core-java-string-operations-6</module>
<module>core-java-string-operations-7</module>
<module>core-java-string-operations-8</module>
<module>core-java-regex</module>
<module>core-java-regex-2</module>
<module>core-java-regex-3</module>

View File

@ -0,0 +1,22 @@
package com.baeldung.preventexpressingintasfloat;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Hashtable;
public class Main {
public static String draft = "[{\"id\":4077395,\"field_id\":242566,\"body\":\"\"}, " +
"{\"id\":4077398,\"field_id\":242569,\"body\":[[273019,0],[273020,1],[273021,0]]}, " +
"{\"id\":4077399,\"field_id\":242570,\"body\":[[273022,0],[273023,1],[273024,0]]}]";
public static void main(String[] args) {
ArrayList<Hashtable<String, Object>> responses;
Type ResponseList = new TypeToken<ArrayList<Hashtable<String, Object>>>() {
}.getType();
responses = new Gson().fromJson(draft, ResponseList);
System.out.println(responses);
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.preventexpressingintasfloat;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Hashtable;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class PreventExpressingIntAsFloatUnitTest {
public String jsonString = "[{\"id\":4077395,\"field_id\":242566,\"body\":\"\"}, " +
"{\"id\":4077398,\"field_id\":242569,\"body\":[[273019,0],[273020,1],[273021,0]]}, " +
"{\"id\":4077399,\"field_id\":242570,\"body\":[[273022,0],[273023,1],[273024,0]]}]";
public String expectedOutput = "[{body=, field_id=242566, id=4077395}, " +
"{body=[[273019, 0], [273020, 1], [273021, 0]], field_id=242569, id=4077398}, " +
"{body=[[273022, 0], [273023, 1], [273024, 0]], field_id=242570, id=4077399}]";
@Test
public void givenJsonString_whenUsingsetObjectToNumberStrategyMethod_thenValidateOutput() {
Gson gson = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();
ArrayList<Hashtable<String, Object>> responses = gson.fromJson(jsonString, new TypeToken<ArrayList<Hashtable<String, Object>>>() {
}.getType());
assertEquals(expectedOutput, responses.toString());
}
}

View File

@ -31,7 +31,7 @@
<version>${rest-assured.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-core.version}</version>
</dependency>
@ -64,7 +64,7 @@
</dependencies>
<properties>
<hibernate-core.version>5.2.16.Final</hibernate-core.version>
<hibernate-core.version>6.4.2.Final</hibernate-core.version>
<mysql-connector.version>8.2.0</mysql-connector.version>
<spring-boot.version>2.7.5</spring-boot.version>
<rest-assured.version>5.3.0</rest-assured.version>

View File

@ -6,8 +6,8 @@ import com.baeldung.daopattern.daos.JpaUserDao;
import com.baeldung.daopattern.entities.User;
import java.util.List;
import java.util.Optional;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Persistence;
public class UserApplication {

View File

@ -8,10 +8,10 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
import jakarta.persistence.EntityManager;
import javax.sql.DataSource;
import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceUnitInfo;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.spi.PersistenceUnitInfo;
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;
import org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor;

View File

@ -6,11 +6,11 @@ import java.util.Collections;
import java.util.List;
import java.util.Properties;
import javax.sql.DataSource;
import javax.persistence.SharedCacheMode;
import javax.persistence.ValidationMode;
import javax.persistence.spi.ClassTransformer;
import javax.persistence.spi.PersistenceUnitInfo;
import javax.persistence.spi.PersistenceUnitTransactionType;
import jakarta.persistence.SharedCacheMode;
import jakarta.persistence.ValidationMode;
import jakarta.persistence.spi.ClassTransformer;
import jakarta.persistence.spi.PersistenceUnitInfo;
import jakarta.persistence.spi.PersistenceUnitTransactionType;
import org.hibernate.jpa.HibernatePersistenceProvider;
public class PersistenceUnitInfoImpl implements PersistenceUnitInfo {

View File

@ -5,9 +5,9 @@ import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.Query;
public class JpaUserDao implements Dao<User> {

View File

@ -1,10 +1,10 @@
package com.baeldung.daopattern.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "users")

View File

@ -1,6 +1,6 @@
package com.baeldung.repositoryvsdaopattern;
import javax.persistence.EntityManager;
import jakarta.persistence.EntityManager;
public class UserDaoImpl implements UserDao {

View File

@ -822,7 +822,7 @@
<module>spring-actuator</module>
<module>spring-ai</module>
<module>spring-aop-2</module>
<!--<module>spring-aop</module>--><!-- JAVA-30541 -->
<module>spring-aop</module>
<module>spring-batch-2</module>
<module>spring-batch</module>
<module>spring-boot-modules</module>
@ -1063,7 +1063,7 @@
<module>spring-actuator</module>
<module>spring-ai</module>
<module>spring-aop-2</module>
<!--<module>spring-aop</module>--><!-- JAVA-30541 -->
<module>spring-aop</module>
<module>spring-batch-2</module>
<module>spring-batch</module>
<module>spring-boot-modules</module>

View File

@ -86,6 +86,7 @@
<properties>
<aspectj-plugin.version>1.14.0</aspectj-plugin.version>
<java.version>16</java.version>
</properties>
</project>

View File

@ -1,5 +1,6 @@
package com.baeldung.joinpoint;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -21,6 +22,10 @@ public class JoinPointAroundCacheAspectIntegrationTest {
@Autowired
private ArticleService articleService;
@Before
public void removeCache() {
JoinPointAroundCacheAspect.CACHE.clear();
}
@Test
public void shouldPopulateCache() {
assertTrue(JoinPointAroundCacheAspect.CACHE.isEmpty());

View File

@ -27,7 +27,7 @@
<module>spring-security-oauth2-sso</module>
<module>spring-security-oidc</module>
<module>spring-security-okta</module>
<module>spring-security-saml</module>
<!-- <module>spring-security-saml</module>--> <!-- This module wasn't able to update to spring boot 3 because Jakarta clashes with javax -->
<module>spring-security-social-login</module> <!-- spring-social-facebook is deprecated and not supported in latest version of spring security. JAVA-29302 -->
<module>spring-security-web-angular</module>
<module>spring-security-web-boot-1</module>

View File

@ -1,3 +1,4 @@
Note: For integration testing get the OPA server running first. Check the official [OPA documentation](https://www.openpolicyagent.org/docs/latest/) for instructions on how to run the OPA server.
### Relevant Articles:

View File

@ -1,14 +1,15 @@
<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">
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>
<artifactId>spring-security-opa</artifactId>
<description>Spring Security with OPA authorization</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>spring-security-modules</artifactId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-3</relativePath>
</parent>
<dependencies>
@ -28,7 +29,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -5,7 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

View File

@ -11,15 +11,14 @@ import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@EnableConfigurationProperties(OpaProperties.class)
public class OpaConfiguration {
private final OpaProperties opaProperties;
@Bean
public WebClient opaWebClient(WebClient.Builder builder) {
return builder
.baseUrl(opaProperties.getEndpoint())
.build();
return builder.baseUrl(opaProperties.getEndpoint())
.build();
}
}

View File

@ -1,14 +1,14 @@
package com.baeldung.security.opa.config;
import javax.annotation.Nonnull;
import org.springframework.boot.context.properties.ConfigurationProperties;
import jakarta.annotation.Nonnull;
import lombok.Data;
@ConfigurationProperties(prefix = "opa")
@Data
public class OpaProperties {
@Nonnull
private String endpoint = "http://localhost:8181";
}

View File

@ -1,8 +1,6 @@
package com.baeldung.security.opa.config;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
@ -11,17 +9,16 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ClientHttpRequest;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.authorization.ReactiveAuthorizationManager;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.security.web.server.authorization.AuthorizationContext;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
@ -32,79 +29,65 @@ import reactor.core.publisher.Mono;
@Configuration
public class SecurityConfiguration {
@Bean
public SecurityWebFilterChain accountAuthorization(ServerHttpSecurity http, @Qualifier("opaWebClient")WebClient opaWebClient) {
// @formatter:on
return http
.httpBasic()
.and()
.authorizeExchange(exchanges -> {
exchanges
.pathMatchers("/account/*")
.access(opaAuthManager(opaWebClient));
})
.build();
// @formatter:on
public SecurityWebFilterChain accountAuthorization(ServerHttpSecurity http, @Qualifier("opaWebClient") WebClient opaWebClient) {
return http.httpBasic(Customizer.withDefaults())
.authorizeExchange(exchanges -> exchanges.pathMatchers("/account/*")
.access(opaAuthManager(opaWebClient)))
.build();
}
@Bean
public ReactiveAuthorizationManager<AuthorizationContext> opaAuthManager(WebClient opaWebClient) {
return (auth, context) -> {
return opaWebClient.post()
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.body(toAuthorizationPayload(auth,context), Map.class)
.exchangeToMono(this::toDecision);
};
return (auth, context) -> opaWebClient.post()
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.body(toAuthorizationPayload(auth, context), Map.class)
.exchangeToMono(this::toDecision);
}
private Mono<AuthorizationDecision> toDecision(ClientResponse response) {
if ( !response.statusCode().is2xxSuccessful()) {
if (!response.statusCode()
.is2xxSuccessful()) {
return Mono.just(new AuthorizationDecision(false));
}
return response
.bodyToMono(ObjectNode.class)
.map(node -> {
boolean authorized = node.path("result").path("authorized").asBoolean(false);
return new AuthorizationDecision(authorized);
});
return response.bodyToMono(ObjectNode.class)
.map(node -> {
boolean authorized = node.path("result")
.path("authorized")
.asBoolean(false);
return new AuthorizationDecision(authorized);
});
}
private Publisher<Map<String,Object>> toAuthorizationPayload(Mono<Authentication> auth, AuthorizationContext context) {
// @formatter:off
return auth
.defaultIfEmpty(new AnonymousAuthenticationToken("**ANONYMOUS**", new Object(), Arrays.asList(new SimpleGrantedAuthority("ANONYMOUS"))))
.map( a -> {
Map<String,String> headers = context.getExchange().getRequest()
.getHeaders()
.toSingleValueMap();
Map<String,Object> attributes = ImmutableMap.<String,Object>builder()
.put("principal",a.getName())
.put("authorities",
a.getAuthorities()
.stream()
.map(g -> g.getAuthority())
.collect(Collectors.toList()))
.put("uri", context.getExchange().getRequest().getURI().getPath())
.put("headers",headers)
.build();
Map<String,Object> input = ImmutableMap.<String,Object>builder()
.put("input",attributes)
.build();
return input;
});
// @formatter:on
private Publisher<Map<String, Object>> toAuthorizationPayload(Mono<Authentication> auth, AuthorizationContext context) {
return auth.defaultIfEmpty(
new AnonymousAuthenticationToken("**ANONYMOUS**", new Object(), Collections.singletonList(new SimpleGrantedAuthority("ANONYMOUS"))))
.map(a -> {
Map<String, String> headers = context.getExchange()
.getRequest()
.getHeaders()
.toSingleValueMap();
Map<String, Object> attributes = ImmutableMap.<String, Object> builder()
.put("principal", a.getName())
.put("authorities", a.getAuthorities()
.stream()
.map(GrantedAuthority::getAuthority)
.collect(Collectors.toList()))
.put("uri", context.getExchange()
.getRequest()
.getURI()
.getPath())
.put("headers", headers)
.build();
return ImmutableMap.<String, Object> builder()
.put("input", attributes)
.build();
});
}
}

View File

@ -13,9 +13,9 @@ import reactor.core.publisher.Mono;
@RestController
@RequiredArgsConstructor
public class AccountController {
private final AccountService accountService;
@GetMapping("/account/{accountId}")
public Mono<Account> getAccount(@PathVariable("accountId") String accountId) {
return accountService.findByAccountId(accountId);

View File

@ -6,20 +6,17 @@ import lombok.Data;
@Data
public class Account {
private String id;
private BigDecimal balance;
private String currency;
public static Account of(String id, BigDecimal balance, String currency) {
Account acc = new Account();
acc.setId(id);
acc.setBalance(balance);
acc.setCurrency(currency);
return acc;
Account account = new Account();
account.setId(id);
account.setBalance(balance);
account.setCurrency(currency);
return account;
}
}

View File

@ -1,6 +1,3 @@
/**
*
*/
package com.baeldung.security.opa.service;
import java.math.BigDecimal;
@ -13,25 +10,20 @@ import com.google.common.collect.ImmutableMap;
import reactor.core.publisher.Mono;
/**
* @author Philippe
*
*/
@Service
public class AccountService {
private Map<String, Account> accounts = ImmutableMap.<String, Account>builder()
.put("0001", Account.of("0001", BigDecimal.valueOf(100.00), "USD"))
.put("0002", Account.of("0002", BigDecimal.valueOf(101.00), "EUR"))
.put("0003", Account.of("0003", BigDecimal.valueOf(102.00), "BRL"))
.put("0004", Account.of("0004", BigDecimal.valueOf(103.00), "AUD"))
.put("0005", Account.of("0005", BigDecimal.valueOf(10400.00), "JPY"))
.build();
private Map<String, Account> accounts = ImmutableMap.<String, Account> builder()
.put("0001", Account.of("0001", BigDecimal.valueOf(100.00), "USD"))
.put("0002", Account.of("0002", BigDecimal.valueOf(101.00), "EUR"))
.put("0003", Account.of("0003", BigDecimal.valueOf(102.00), "BRL"))
.put("0004", Account.of("0004", BigDecimal.valueOf(103.00), "AUD"))
.put("0005", Account.of("0005", BigDecimal.valueOf(10400.00), "JPY"))
.build();
public Mono<Account> findByAccountId(String accountId) {
return Mono.just(accounts.get(accountId))
.switchIfEmpty(Mono.error(new IllegalArgumentException("invalid.account")));
.switchIfEmpty(Mono.error(new IllegalArgumentException("invalid.account")));
}
}

View File

@ -12,7 +12,6 @@ import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.reactive.server.WebTestClient;
// !!! NOTICE: Start OPA server before running this test class !!!
@SpringBootTest
@ActiveProfiles("test")
class AccountControllerLiveTest {
@ -24,44 +23,42 @@ class AccountControllerLiveTest {
@BeforeEach
public void setup() {
this.rest = WebTestClient.bindToApplicationContext(this.context)
.apply(springSecurity())
.configureClient()
.build();
.apply(springSecurity())
.configureClient()
.build();
}
@Test
@WithMockUser(username = "user1", roles = { "account:read:0001"} )
@WithMockUser(username = "user1", roles = { "account:read:0001" })
void testGivenValidUser_thenSuccess() {
rest.get()
.uri("/account/0001")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus()
.is2xxSuccessful();
.uri("/account/0001")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus()
.is2xxSuccessful();
}
@Test
@WithMockUser(username = "user1", roles = { "account:read:0002"} )
@WithMockUser(username = "user1", roles = { "account:read:0002" })
void testGivenValidUser_thenUnauthorized() {
rest.get()
.uri("/account/0001")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus()
.isForbidden();
.uri("/account/0001")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus()
.isForbidden();
}
@Test
@WithMockUser(username = "user1", roles = {} )
@WithMockUser(username = "user1", roles = {})
void testGivenNoAuthorities_thenForbidden() {
rest.get()
.uri("/account/0001")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus()
.isForbidden();
.uri("/account/0001")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus()
.isForbidden();
}
}

View File

@ -2,7 +2,6 @@ package com.baeldung.saml;
import static org.springframework.security.config.Customizer.withDefaults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@ -13,26 +12,30 @@ import org.springframework.security.saml2.provider.service.registration.RelyingP
import org.springframework.security.saml2.provider.service.web.DefaultRelyingPartyRegistrationResolver;
import org.springframework.security.saml2.provider.service.web.Saml2MetadataFilter;
import org.springframework.security.saml2.provider.service.web.authentication.Saml2WebSsoAuthenticationFilter;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Autowired
private RelyingPartyRegistrationRepository relyingPartyRegistrationRepository;
private final RelyingPartyRegistrationRepository relyingPartyRegistrationRepository;
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
DefaultRelyingPartyRegistrationResolver relyingPartyRegistrationResolver = new DefaultRelyingPartyRegistrationResolver(this.relyingPartyRegistrationRepository);
DefaultRelyingPartyRegistrationResolver relyingPartyRegistrationResolver = new DefaultRelyingPartyRegistrationResolver(
this.relyingPartyRegistrationRepository);
Saml2MetadataFilter filter = new Saml2MetadataFilter(relyingPartyRegistrationResolver, new OpenSamlMetadataResolver());
http.csrf(AbstractHttpConfigurer::disable).authorizeHttpRequests(authorize -> authorize.anyRequest().authenticated())
.saml2Login(withDefaults())
.saml2Logout(withDefaults())
.addFilterBefore(filter, Saml2WebSsoAuthenticationFilter.class);
DefaultSecurityFilterChain chain = http.build();
return chain;
http.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(authorize -> authorize.anyRequest()
.authenticated())
.saml2Login(withDefaults())
.saml2Logout(withDefaults())
.addFilterBefore(filter, Saml2WebSsoAuthenticationFilter.class);
return http.build();
}
public SecurityConfig(RelyingPartyRegistrationRepository relyingPartyRegistrationRepository) {
this.relyingPartyRegistrationRepository = relyingPartyRegistrationRepository;
}
}

View File

@ -10,9 +10,9 @@
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
<relativePath>../../parent-boot-3</relativePath>
</parent>
<dependencies>
@ -54,11 +54,30 @@
<artifactId>json-patch</artifactId>
<version>${jsonpatch.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${httpclient5.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<xstream.version>1.4.9</xstream.version>
<jsonpatch.version>1.12</jsonpatch.version>
<httpclient5.version>5.2.1</httpclient5.version>
</properties>
</project>

View File

@ -1,22 +1,22 @@
package com.baeldung.responseheaders.controllers;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jakarta.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/filter-response-header")
public class FilterResponseHeaderController {
@GetMapping("/no-extra-header")
public String FilterHeaderResponseWithNoExtraHeader() {
public String filterHeaderResponseWithNoExtraHeader() {
return "Response body with Filter header and no extra header";
}
@GetMapping("/extra-header")
public String FilterHeaderResponseWithExtraHeader(HttpServletResponse response) {
public String filterHeaderResponseWithExtraHeader(HttpServletResponse response) {
response.addHeader("Baeldung-Example-Header", "Value-ExtraHeader");
return "Response body with Filter header and one extra header";
}

View File

@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;

View File

@ -2,18 +2,18 @@ package com.baeldung.responseheaders.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletResponse;
@WebFilter("/filter-response-header/*")
public class AddResponseHeaderFilter implements Filter {

View File

@ -23,8 +23,6 @@ import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.net.URI;
import javax.validation.Valid;
@RestController
@RequestMapping(value = "/customers")
public class CustomerRestController {
@ -40,25 +38,28 @@ public class CustomerRestController {
public ResponseEntity<Customer> createCustomer(@RequestBody Customer customer) {
Customer customerCreated = customerService.createCustomer(customer);
URI location = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(customerCreated.getId())
.toUri();
return ResponseEntity.created(location).build();
.path("/{id}")
.buildAndExpand(customerCreated.getId())
.toUri();
return ResponseEntity.created(location)
.build();
}
@PatchMapping(path = "/{id}", consumes = "application/json-patch+json")
public ResponseEntity<Customer> updateCustomer(@PathVariable String id,
@RequestBody JsonPatch patch) {
public ResponseEntity<Customer> updateCustomer(@PathVariable String id, @RequestBody JsonPatch patch) {
try {
Customer customer = customerService.findCustomer(id).orElseThrow(CustomerNotFoundException::new);
Customer customer = customerService.findCustomer(id)
.orElseThrow(CustomerNotFoundException::new);
Customer customerPatched = applyPatchToCustomer(patch, customer);
customerService.updateCustomer(customerPatched);
return ResponseEntity.ok(customerPatched);
} catch (CustomerNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.build();
} catch (JsonPatchException | JsonProcessingException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.build();
}
}