Merge branch 'master' into BAEL-7142-spring-ai
This commit is contained in:
commit
31ea655d12
|
@ -94,7 +94,6 @@
|
|||
<properties>
|
||||
<spring.version>2.2.1.RELEASE</spring.version>
|
||||
<awssdk.version>2.17.283</awssdk.version>
|
||||
<lombok.version>1.18.20</lombok.version>
|
||||
<reactor.version>3.6.0</reactor.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -0,0 +1,129 @@
|
|||
package com.baeldung.iteratorvsforloop;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.List;
|
||||
import java.util.Iterator;
|
||||
import java.util.Collections;
|
||||
import java.util.ListIterator;
|
||||
|
||||
public class IteratorForLoopUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenEmptyCollection_whenUsingForLoop_thenNoElementsAreIterated() {
|
||||
List<String> names = Collections.emptyList();
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < names.size(); i++) {
|
||||
stringBuilder.append(names.get(i));
|
||||
}
|
||||
|
||||
assertEquals("", stringBuilder.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyCollection_whenUsingIterator_thenNoElementsAreIterated() {
|
||||
List<String> names = Collections.emptyList();
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
Iterator<String> iterator = names.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
stringBuilder.append(iterator.next());
|
||||
}
|
||||
|
||||
assertEquals("", stringBuilder.toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenCollectionWithElements_whenUsingForLoop_thenAllElementsAreIterated() {
|
||||
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < names.size(); i++) {
|
||||
stringBuilder.append(names.get(i));
|
||||
}
|
||||
|
||||
assertEquals("AliceBobCharlie", stringBuilder.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionWithElements_whenUsingIterator_thenAllElementsAreIterated() {
|
||||
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
Iterator<String> iterator = names.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
stringBuilder.append(iterator.next());
|
||||
}
|
||||
|
||||
assertEquals("AliceBobCharlie", stringBuilder.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionWithElements_whenUsingForLoop_thenAllElementsAreIteratedReverseOrder() {
|
||||
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
for (int i = names.size() - 1; i >= 0; i--) {
|
||||
stringBuilder.append(names.get(i));
|
||||
}
|
||||
|
||||
assertEquals("CharlieBobAlice", stringBuilder.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionWithElements_whenUsingListIterator_thenAllElementsAreIteratedInReverseOrder() {
|
||||
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
ListIterator<String> listIterator = names.listIterator(names.size());
|
||||
while (listIterator.hasPrevious()) {
|
||||
stringBuilder.append(listIterator.previous());
|
||||
}
|
||||
|
||||
assertEquals("CharlieBobAlice", stringBuilder.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionWithElements_whenRemovingElementDuringForLoopIteration_thenConcurrentModificationExceptionIsThrown() {
|
||||
List<String> names = new ArrayList<>(List.of("Alice", "Bob", "Charlie"));
|
||||
|
||||
assertThrows(ConcurrentModificationException.class, () -> {
|
||||
for (String name : names) {
|
||||
names.remove("Bob");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionWithElements_whenRemovingElementUsingIterator_thenElementIsRemovedSafely() {
|
||||
List<String> names = new ArrayList<>(Arrays.asList("Alice", "Bob", "Charlie"));
|
||||
Iterator<String> iterator = names.iterator();
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
String name = iterator.next();
|
||||
if (name.equals("Bob")) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
List<String> expected = Arrays.asList("Alice", "Charlie");
|
||||
assertIterableEquals(expected, names);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionWithElements_whenModifyingElementToLowerCaseDuringForLoopIteration_thenElementsAreModifiedToLowerCase() {
|
||||
List<String> names = new ArrayList<>(List.of("Alice", "Bob", "Charlie"));
|
||||
|
||||
for (int i = 0; i < names.size(); i++) {
|
||||
names.set(i, names.get(i).toLowerCase());
|
||||
}
|
||||
|
||||
List<String> expected = Arrays.asList("alice","bob", "charlie");
|
||||
assertIterableEquals(expected, names);
|
||||
}
|
||||
}
|
|
@ -10,4 +10,5 @@ This module contains articles about Map data structures in Java.
|
|||
- [Java HashMap Load Factor](https://www.baeldung.com/java-hashmap-load-factor)
|
||||
- [Converting Java Properties to HashMap](https://www.baeldung.com/java-convert-properties-to-hashmap)
|
||||
- [Get Values and Keys as ArrayList From a HashMap](https://www.baeldung.com/java-values-keys-arraylists-hashmap)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-2)
|
||||
- [Remove Duplicate Values From HashMap in Java](https://www.baeldung.com/java-hashmap-delete-duplicates)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-2)[[next -->]](/core-java-modules/core-java-collections-maps-4)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.map.removeuplicate;
|
||||
package com.baeldung.map.removeduplicate;
|
||||
|
||||
import static java.util.stream.Collectors.toMap;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
@ -9,3 +9,4 @@ This module contains articles about Map data structures in Java.
|
|||
- [Difference Between Map and HashMap in Java](https://www.baeldung.com/java-map-vs-hashmap)
|
||||
- [How to Create a New Entry in a Map](https://www.baeldung.com/java-map-new-entry)
|
||||
- [Difference Between Map and MultivaluedMap in Java](https://www.baeldung.com/java-map-vs-multivaluedmap)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-3)[[next -->]](/core-java-modules/core-java-collections-maps-5)
|
||||
|
|
|
@ -10,4 +10,5 @@
|
|||
- [How to Invert a Map in Java](https://www.baeldung.com/java-invert-map)
|
||||
- [Implementing a Map with Multiple Keys in Java](https://www.baeldung.com/java-multiple-keys-map)
|
||||
- [Difference Between Map.ofEntries() and Map.of()](https://www.baeldung.com/map-ofentries-and-map-of)
|
||||
- More articles: [[<-- prev]](../core-java-collections-maps-4)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-4)[[next -->]](/core-java-modules/core-java-collections-maps-6)
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
- [Converting JsonNode Object to Map](https://www.baeldung.com/jackson-jsonnode-map)
|
||||
- [How to Modify a Key in a HashMap?](https://www.baeldung.com/java-hashmap-modify-key)
|
||||
- [Converting String or String Array to Map in Java](https://www.baeldung.com/java-convert-string-to-map)
|
||||
- [Remove Duplicate Values From HashMap in Java](https://www.baeldung.com/java-hashmap-delete-duplicates)
|
||||
- [Sorting Java Map in Descending Order](https://www.baeldung.com/java-sort-map-descending)
|
||||
- [Convert HashMap.toString() to HashMap in Java](https://www.baeldung.com/hashmap-from-tostring)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-5)[[next -->]](/core-java-modules/core-java-collections-maps-7)
|
||||
|
||||
|
|
|
@ -11,4 +11,4 @@ This module contains articles about the Stream API in Java.
|
|||
- [Should We Close a Java Stream?](https://www.baeldung.com/java-stream-close)
|
||||
- [Returning Stream vs. Collection](https://www.baeldung.com/java-return-stream-collection)
|
||||
- [Convert a Java Enumeration Into a Stream](https://www.baeldung.com/java-enumeration-to-stream)
|
||||
- More articles: [[<-- prev>]](/../core-java-streams-2)
|
||||
- More articles: [[<-- prev>]](/../core-java-streams-2) [[next -->]](/../core-java-streams-4)
|
||||
|
|
|
@ -10,3 +10,4 @@
|
|||
- [Understanding the Difference Between Stream.of() and IntStream.range()](https://www.baeldung.com/java-stream-of-and-intstream-range)
|
||||
- [Check if Object Is an Array in Java](https://www.baeldung.com/java-check-if-object-is-an-array)
|
||||
- [Mapping an Array of Integers to Strings Using Java Streams](https://www.baeldung.com/java-stream-integer-array-to-strings)
|
||||
- More articles: [[<-- prev>]](/../core-java-streams-3) [[next -->]](/../core-java-streams-5)
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
- [Partition a Stream in Java](https://www.baeldung.com/java-partition-stream)
|
||||
- [Taking Every N-th Element from Finite and Infinite Streams in Java](https://www.baeldung.com/java-nth-element-finite-infinite-streams)
|
||||
- [Modifying Objects Within Stream While Iterating](https://www.baeldung.com/java-stream-modify-objects-during-iteration)
|
||||
- [Convert a Stream into a Map or Multimap in Java](https://www.baeldung.com/java-convert-stream-map-multimap)
|
||||
- [How to Avoid NoSuchElementException in Stream API](https://www.baeldung.com/java-streams-api-avoid-nosuchelementexception)
|
||||
- [Get Index of First Element Matching Boolean Using Java Streams](https://www.baeldung.com/java-streams-find-first-match-index)
|
||||
- [Handling NullPointerException in findFirst() When the First Element Is Null](https://www.baeldung.com/java-handle-nullpointerexception-findfirst-first-null)
|
||||
- More articles: [[<-- prev>]](/../core-java-streams-4)
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
## Relevant Articles:
|
||||
- [Handle Duplicate Keys When Producing Map Using Java Stream](https://www.baeldung.com/java-duplicate-keys-when-producing-map-using-stream)
|
||||
- [Convert a Stream into a Map or Multimap in Java](https://www.baeldung.com/java-convert-stream-map-multimap)
|
|
@ -0,0 +1,107 @@
|
|||
package com.baeldung.streams.streamtomapandmultimap;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.ListMultimap;
|
||||
|
||||
public class StreamToMapAndMultiMapUnitTest {
|
||||
@Test
|
||||
public void givenStringStream_whenConvertingToMapWithMerge_thenExpectedMapIsGenerated() {
|
||||
Stream<String> stringStream = Stream.of("one", "two", "three", "two");
|
||||
|
||||
Map<String, String> mergedMap = stringStream.collect(
|
||||
Collectors.toMap(s -> s, s -> s, (s1, s2) -> s1 + ", " + s2)
|
||||
);
|
||||
|
||||
// Define the expected map
|
||||
Map<String, String> expectedMap = Map.of(
|
||||
"one", "one",
|
||||
"two", "two, two",
|
||||
"three", "three"
|
||||
);
|
||||
|
||||
assertEquals(expectedMap, mergedMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringStream_whenConvertingToMultimap_thenExpectedMultimapIsGenerated() {
|
||||
Stream<String> stringStream = Stream.of("one", "two", "three", "two");
|
||||
|
||||
ListMultimap<String, String> multimap = stringStream.collect(
|
||||
ArrayListMultimap::create,
|
||||
(map, element) -> map.put(element, element),
|
||||
ArrayListMultimap::putAll
|
||||
);
|
||||
|
||||
ListMultimap<String, String> expectedMultimap = ArrayListMultimap.create();
|
||||
expectedMultimap.put("one", "one");
|
||||
expectedMultimap.put("two", "two");
|
||||
expectedMultimap.put("two", "two");
|
||||
expectedMultimap.put("three", "three");
|
||||
|
||||
assertEquals(expectedMultimap, multimap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringStream_whenConvertingToMultimapWithStreamReduce_thenExpectedMultimapIsGenerated() {
|
||||
Stream<String> stringStream = Stream.of("one", "two", "three", "two");
|
||||
|
||||
Map<String, List<String>> multimap = stringStream.reduce(
|
||||
new HashMap<>(),
|
||||
(map, element) -> {
|
||||
map.computeIfAbsent(element, k -> new ArrayList<>()).add(element);
|
||||
return map;
|
||||
},
|
||||
(map1, map2) -> {
|
||||
map2.forEach((key, value) -> map1.merge(key, value, (list1, list2) -> {
|
||||
list1.addAll(list2);
|
||||
return list1;
|
||||
}));
|
||||
return map1;
|
||||
}
|
||||
);
|
||||
|
||||
Map<String, List<String>> expectedMultimap = new HashMap<>();
|
||||
expectedMultimap.put("one", Collections.singletonList("one"));
|
||||
expectedMultimap.put("two", Arrays.asList("two", "two"));
|
||||
expectedMultimap.put("three", Collections.singletonList("three"));
|
||||
|
||||
assertEquals(expectedMultimap, multimap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringStream_whenConvertingToMapWithStreamReduce_thenExpectedMapIsGenerated() {
|
||||
Stream<String> stringStream = Stream.of("one", "two", "three", "two");
|
||||
|
||||
Map<String, String> resultMap = stringStream.reduce(
|
||||
new HashMap<>(),
|
||||
(map, element) -> {
|
||||
map.put(element, element);
|
||||
return map;
|
||||
},
|
||||
(map1, map2) -> {
|
||||
map1.putAll(map2);
|
||||
return map1;
|
||||
}
|
||||
);
|
||||
|
||||
Map<String, String> expectedMap = new HashMap<>();
|
||||
expectedMap.put("one", "one");
|
||||
expectedMap.put("two", "two");
|
||||
expectedMap.put("three", "three");
|
||||
|
||||
assertEquals(expectedMap, resultMap);
|
||||
}
|
||||
}
|
|
@ -14,7 +14,6 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.annotation</groupId>
|
||||
<artifactId>javax.annotation-api</artifactId>
|
||||
|
|
|
@ -9,7 +9,7 @@ import org.junit.Test;
|
|||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class JsonNodeToJsonObjectUnitTest {
|
||||
public class JsonNodeToObjectNodeUnitTest {
|
||||
|
||||
public static String jsonString = "{\"name\": \"John\", \"gender\": \"male\", \"company\": \"Baeldung\", \"isEmployee\": true, \"age\": 30}";
|
||||
|
||||
|
@ -18,7 +18,7 @@ public class JsonNodeToJsonObjectUnitTest {
|
|||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode jsonNode = objectMapper.readTree(jsonString);
|
||||
ObjectNode objectNode = objectMapper.createObjectNode().setAll((ObjectNode) jsonNode);
|
||||
ObjectNode objectNode = (ObjectNode) jsonNode;
|
||||
|
||||
assertEquals("John", objectNode.get("name").asText());
|
||||
assertEquals("male", objectNode.get("gender").asText());
|
|
@ -3,17 +3,19 @@
|
|||
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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>jkube-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>jkube-demo</name>
|
||||
<description>jkube-demo</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>jkube-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>jkube-demo</name>
|
||||
<description>jkube-demo</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -2,14 +2,13 @@
|
|||
<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">
|
||||
<parent>
|
||||
<artifactId>microservices-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>rest-express</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>rest-express</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>A Basic, MongoDB-backed Service Suite</description>
|
||||
<url>https://github.com/RestExpress/RestExpress-Scaffold</url>
|
||||
<!--
|
||||
To run the project: mvn clean package exec:java
|
||||
* mongod must be running.
|
||||
|
@ -18,11 +17,12 @@
|
|||
mvn clean package
|
||||
mvn assembly:single
|
||||
-->
|
||||
<description>A Basic, MongoDB-backed Service Suite</description>
|
||||
<url>https://github.com/RestExpress/RestExpress-Scaffold</url>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<artifactId>rest-express</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>microservices-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring.version>6.0.12</spring.version>
|
||||
<spring.version>6.1.2</spring.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -1,15 +1,19 @@
|
|||
<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="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>
|
||||
<groupId>com.baeldung.core-java-persistence-3</groupId>
|
||||
<artifactId>core-java-persistence-3</artifactId>
|
||||
<name>core-java-persistence-3</name>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-persistence-3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>persistence-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
|
@ -22,6 +26,7 @@
|
|||
<version>${commons-dbutils.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<h2.version>2.1.214</h2.version>
|
||||
<commons-dbutils.version>1.8.1</commons-dbutils.version>
|
||||
|
|
|
@ -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>
|
||||
|
@ -74,7 +74,10 @@
|
|||
|
||||
<properties>
|
||||
<flyway.configFiles>src/main/resources/application-${spring-boot.run.profiles}.properties</flyway.configFiles>
|
||||
<flyway-maven-plugin.version>10.2.0</flyway-maven-plugin.version>
|
||||
<flyway-maven-plugin.version>10.4.0</flyway-maven-plugin.version>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,38 @@
|
|||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.flywaycallbacks.FlywayApplication;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = { FlywayApplication.class })
|
||||
public class FlywayAppIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Test
|
||||
public void testAllMigrationsExecuted() throws SQLException {
|
||||
DatabaseMetaData metadata = dataSource.getConnection()
|
||||
.getMetaData();
|
||||
ResultSet resultSet = metadata.getTables(null, null, null, new String[] { "TABLE" });
|
||||
Set<String> tables = Set.of("TABLE_ONE", "TABLE_TWO", "TABLE_THREE", "TABLE_FOUR");
|
||||
int migrations = 0;
|
||||
while (resultSet.next()) {
|
||||
if (tables.contains(resultSet.getString("TABLE_NAME"))) {
|
||||
migrations++;
|
||||
}
|
||||
}
|
||||
Assert.assertEquals(migrations, 4);
|
||||
}
|
||||
}
|
|
@ -31,20 +31,34 @@
|
|||
<artifactId>jooq-codegen</artifactId>
|
||||
<version>${jooq.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.15.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>${postgresql.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<jooq.version>3.18.7</jooq.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<java.version>17</java.version>
|
||||
<jooq.version>3.19.0</jooq.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,75 @@
|
|||
package com.baeldung.jooq;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.codegen.GenerationTool;
|
||||
import org.jooq.impl.DSL;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jooq.model.tables.Article;
|
||||
import com.baeldung.jooq.model.tables.Author;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
public class CodeGenerationIntegrationTest {
|
||||
|
||||
static DSLContext context;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() throws SQLException {
|
||||
Connection conn = DriverManager.getConnection("jdbc:h2:mem:tes;INIT=CREATE SCHEMA IF NOT EXISTS \"public\"");
|
||||
context = DSL.using(conn, SQLDialect.H2);
|
||||
|
||||
context.createTable(Author.AUTHOR)
|
||||
.columns(
|
||||
Author.AUTHOR.ID,
|
||||
Author.AUTHOR.FIRST_NAME,
|
||||
Author.AUTHOR.LAST_NAME,
|
||||
Author.AUTHOR.AGE
|
||||
)
|
||||
.execute();
|
||||
context.createTable(Article.ARTICLE)
|
||||
.columns(
|
||||
Article.ARTICLE.ID,
|
||||
Article.ARTICLE.TITLE,
|
||||
Article.ARTICLE.DESCRIPTION,
|
||||
Article.ARTICLE.AUTHOR_ID
|
||||
)
|
||||
.execute();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() throws IOException {
|
||||
File generatedDirectory = new File("src/main/java/com/baeldung/jooq/generated");
|
||||
FileUtils.deleteDirectory(generatedDirectory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassGenerationFromExistingDatabase() throws Exception {
|
||||
|
||||
File generatedDirectory = new File("src/main/java/com/baeldung/jooq/generated");
|
||||
|
||||
assertFalse(generatedDirectory.exists());
|
||||
|
||||
URL jooqConfigURL = getClass().getClassLoader().getResource("jooq-config.xml");
|
||||
assertNotNull(jooqConfigURL);
|
||||
File file = new File(jooqConfigURL.getFile());
|
||||
GenerationTool.generate(Files.readString(file.toPath()));
|
||||
|
||||
assertTrue(generatedDirectory.exists());
|
||||
}
|
||||
}
|
|
@ -28,7 +28,7 @@ import static org.junit.Assert.assertEquals;
|
|||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class CrudLiveTest {
|
||||
public class CrudIntegrationTest {
|
||||
|
||||
static DSLContext context;
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.13.0.xsd">
|
||||
|
||||
<jdbc>
|
||||
<driver>org.h2.Driver</driver>
|
||||
<url>jdbc:h2:mem:tes</url>
|
||||
<user></user>
|
||||
<password></password>
|
||||
</jdbc>
|
||||
|
||||
<generator>
|
||||
<name>org.jooq.codegen.JavaGenerator</name>
|
||||
|
||||
<database>
|
||||
<name>org.jooq.meta.h2.H2Database</name>
|
||||
<inputSchema>public</inputSchema>
|
||||
<includes>.*</includes>
|
||||
<excludes></excludes>
|
||||
</database>
|
||||
|
||||
<target>
|
||||
<packageName>com.baeldung.jooq.generated</packageName>
|
||||
<directory>src/main/java</directory>
|
||||
</target>
|
||||
</generator>
|
||||
</configuration>
|
|
@ -108,7 +108,7 @@
|
|||
<module>spring-jpa</module>
|
||||
<module>spring-jpa-2</module>
|
||||
<module>spring-jdbc</module>
|
||||
<module>spring-jdbc-2</module>
|
||||
<module>spring-jdbc-2</module>
|
||||
<!--<module>spring-jooq</module>--><!-- failing after upgrading to jdk17 -->
|
||||
<module>spring-mybatis</module>
|
||||
<module>spring-persistence-simple</module>
|
||||
|
|
|
@ -7,9 +7,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>
|
||||
|
@ -36,4 +36,8 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring-boot.repackage.skip>true</spring-boot.repackage.skip>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -3,7 +3,6 @@ package com.baeldung.springdatajdbcintro.repository;
|
|||
import com.baeldung.springdatajdbcintro.entity.Person;
|
||||
import org.springframework.data.jdbc.repository.query.Modifying;
|
||||
import org.springframework.data.jdbc.repository.query.Query;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package com.baeldung.springmultipledatasources.todos;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Todo {
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package com.baeldung.springmultipledatasources.topics;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Topic {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?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">
|
||||
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-data-jpa-query-2</artifactId>
|
||||
<name>spring-data-jpa-query-2</name>
|
||||
|
|
|
@ -3,16 +3,17 @@
|
|||
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>
|
||||
<artifactId>spring-data-jpa-repo-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-data-jpa-repo-3</name>
|
||||
<description>spring-data-jpa-repo-3</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
</parent>
|
||||
<artifactId>spring-data-jpa-repo-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-data-jpa-repo-3</name>
|
||||
<description>spring-data-jpa-repo-3</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
|
|
@ -10,11 +10,15 @@
|
|||
|
||||
<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>
|
||||
|
||||
<properties>
|
||||
<start-class>com.baeldung.Main</start-class>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package com.baeldung;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
|
|
|
@ -2,3 +2,5 @@ spring.datasource.url=jdbc:postgresql://localhost:5433/yugabyte
|
|||
spring.datasource.username=yugabyte
|
||||
spring.datasource.password=yugabyte
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
|
||||
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
<?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">
|
||||
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>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>spring-jdbc-2</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
|
|
@ -9,9 +9,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>
|
||||
|
@ -78,11 +78,12 @@
|
|||
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<org.springframework.version>5.3.15</org.springframework.version>
|
||||
<org.springframework.version>6.0.13</org.springframework.version>
|
||||
<!-- persistence -->
|
||||
<spring-mybatis.version>2.0.6</spring-mybatis.version>
|
||||
<spring-mybatis.version>3.0.3</spring-mybatis.version>
|
||||
<mybatis.version>3.5.2</mybatis.version>
|
||||
<mybatis-spring-boot-starter.version>2.2.0</mybatis-spring-boot-starter.version>
|
||||
<mybatis-spring-boot-starter.version>3.0.3</mybatis-spring-boot-starter.version>
|
||||
<spring-boot.repackage.skip>true</spring-boot.repackage.skip>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -5,7 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ArticleMapperCommonUnitTest {
|
||||
abstract class ArticleMapperCommonUnitTest {
|
||||
|
||||
@Autowired
|
||||
ArticleMapper articleMapper;
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
<artifactId>quarkus-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
@ -24,6 +25,7 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
|
@ -48,6 +50,7 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
@ -86,6 +89,7 @@
|
|||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>native</id>
|
||||
|
@ -134,4 +138,5 @@
|
|||
<quarkus.platform.version>2.16.0.Final</quarkus.platform.version>
|
||||
<surefire-plugin.version>3.0.0-M7</surefire-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -10,8 +10,9 @@
|
|||
|
||||
<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>
|
||||
|
@ -92,8 +93,8 @@
|
|||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||
<artifactId>httpclient5</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
|
@ -101,19 +102,6 @@
|
|||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<!-- web -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet-api.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>${jstl.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<!-- util -->
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
|
@ -127,9 +115,9 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>${jaxb-api.version}</version>
|
||||
<groupId>jakarta.xml.bind</groupId>
|
||||
<artifactId>jakarta.xml.bind-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -230,6 +218,7 @@
|
|||
<httpclient.version>4.5.8</httpclient.version>
|
||||
<!-- Maven plugins -->
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
<start-class>com.baeldung.inmemory.InMemoryAuthApplication</start-class>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -4,9 +4,8 @@ import org.springframework.security.core.AuthenticationException;
|
|||
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
|
|
|
@ -2,13 +2,13 @@ package com.baeldung.client;
|
|||
|
||||
import java.net.URI;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.AuthCache;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.impl.auth.BasicScheme;
|
||||
import org.apache.http.impl.client.BasicAuthCache;
|
||||
import org.apache.http.protocol.BasicHttpContext;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.hc.client5.http.auth.AuthCache;
|
||||
import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
|
||||
import org.apache.hc.client5.http.impl.auth.BasicScheme;
|
||||
import org.apache.hc.client5.http.protocol.HttpClientContext;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.protocol.BasicHttpContext;
|
||||
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.baeldung.client;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
|
@ -35,7 +35,7 @@ public class RestTemplateFactory implements FactoryBean<RestTemplate>, Initializ
|
|||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
HttpHost host = new HttpHost("localhost", 8082, "http");
|
||||
HttpHost host = new HttpHost( "http", "localhost", 8082);
|
||||
final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth(host);
|
||||
restTemplate = new RestTemplate(requestFactory);
|
||||
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor("user1", "user1Pass"));
|
||||
|
|
|
@ -2,10 +2,10 @@ package com.baeldung.filter;
|
|||
|
||||
import org.springframework.web.filter.GenericFilterBean;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.ServletRequest;
|
||||
import jakarta.servlet.ServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
public class CustomFilter extends GenericFilterBean {
|
||||
|
|
|
@ -30,14 +30,10 @@ public class CustomWebSecurityConfigurerAdapter {
|
|||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/securityNone")
|
||||
.permitAll()
|
||||
.anyRequest()
|
||||
.authenticated()
|
||||
.and()
|
||||
.httpBasic()
|
||||
.authenticationEntryPoint(authenticationEntryPoint);
|
||||
http.authorizeHttpRequests(expressionInterceptUrlRegistry ->
|
||||
expressionInterceptUrlRegistry.requestMatchers("/securityNone").permitAll()
|
||||
.anyRequest().authenticated())
|
||||
.httpBasic(httpSecurityHttpBasicConfigurer -> httpSecurityHttpBasicConfigurer.authenticationEntryPoint(authenticationEntryPoint));
|
||||
http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class);
|
||||
return http.build();
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.baeldung.inmemory;
|
|||
|
||||
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;
|
||||
|
@ -25,13 +26,11 @@ public class InMemoryAuthWebSecurityConfigurer {
|
|||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/private/**")
|
||||
.authenticated()
|
||||
.antMatchers("/public/**")
|
||||
.permitAll()
|
||||
.and()
|
||||
.httpBasic();
|
||||
http.authorizeHttpRequests(authorizationManagerRequestMatcherRegistry ->
|
||||
authorizationManagerRequestMatcherRegistry.requestMatchers("/private/**").authenticated()
|
||||
.requestMatchers("/public/**").permitAll()
|
||||
)
|
||||
.httpBasic(Customizer.withDefaults());
|
||||
return http.build();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.baeldung.inmemory;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
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;
|
||||
|
@ -21,13 +22,9 @@ public class InMemoryNoOpAuthWebSecurityConfigurer {
|
|||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/private/**")
|
||||
.authenticated()
|
||||
.antMatchers("/public/**")
|
||||
.permitAll()
|
||||
.and()
|
||||
.httpBasic();
|
||||
http.authorizeHttpRequests(authorizationManagerRequestMatcherRegistry -> authorizationManagerRequestMatcherRegistry.requestMatchers("/private/**").authenticated()
|
||||
.requestMatchers("/public/**").permitAll())
|
||||
.httpBasic(Customizer.withDefaults());
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ public class PasswordStorageWebSecurityConfigurer {
|
|||
PasswordEncoder defaultEncoder = new StandardPasswordEncoder();
|
||||
Map<String, PasswordEncoder> encoders = new HashMap<>();
|
||||
encoders.put("bcrypt", new BCryptPasswordEncoder());
|
||||
encoders.put("scrypt", new SCryptPasswordEncoder());
|
||||
encoders.put("scrypt", new SCryptPasswordEncoder(1, 1, 1, 1, 10));
|
||||
encoders.put("noop", NoOpPasswordEncoder.getInstance());
|
||||
|
||||
DelegatingPasswordEncoder passwordEncoder = new DelegatingPasswordEncoder("bcrypt", encoders);
|
||||
|
|
|
@ -2,9 +2,9 @@ package com.baeldung.security;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
|
||||
|
|
|
@ -2,8 +2,8 @@ package com.baeldung.security;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||
|
@ -16,8 +16,7 @@ import org.springframework.stereotype.Component;
|
|||
public final class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
||||
|
||||
@Override
|
||||
public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException) throws IOException {
|
||||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
|
||||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
|
||||
}
|
||||
|
||||
}
|
|
@ -2,8 +2,9 @@ package com.baeldung.web.controller;
|
|||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import com.baeldung.web.dto.Bar;
|
||||
|
||||
import org.apache.hc.client5.http.utils.Base64;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
|
@ -35,7 +36,7 @@ public class BarController {
|
|||
public HttpHeaders createHeaders(String username, String password){
|
||||
return new HttpHeaders() {{
|
||||
String auth = username + ":" + password;
|
||||
byte[] encodedAuth = Base64.encodeBase64(
|
||||
byte[] encodedAuth = Base64.encodeBase64(
|
||||
auth.getBytes(Charset.forName("US-ASCII")) );
|
||||
String authHeader = "Basic " + new String( encodedAuth );
|
||||
set( "Authorization", authHeader );
|
||||
|
|
|
@ -2,7 +2,7 @@ package com.baeldung.web.dto;
|
|||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import jakarta.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement
|
||||
public class Bar implements Serializable {
|
||||
|
|
|
@ -2,7 +2,7 @@ package com.baeldung.web.dto;
|
|||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import jakarta.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement
|
||||
public class Foo implements Serializable {
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
<http-basic entry-point-ref="myBasicAuthenticationEntryPoint"/>
|
||||
|
||||
<intercept-url pattern="/**" access="permitAll" />
|
||||
</http>
|
||||
|
||||
<authentication-manager>
|
||||
|
@ -22,7 +23,7 @@
|
|||
</authentication-manager>
|
||||
|
||||
<global-method-security pre-post-annotations="enabled"/>
|
||||
|
||||
|
||||
<beans:bean id="myBasicAuthenticationEntryPoint" class="com.baeldung.basic.MyBasicAuthenticationEntryPoint" />
|
||||
|
||||
</beans:beans>
|
|
@ -11,9 +11,9 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<artifactId>parent-spring-6</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-spring-5</relativePath>
|
||||
<relativePath>../../parent-spring-6</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -73,6 +73,16 @@
|
|||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate-validator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.expressly</groupId>
|
||||
<artifactId>expressly</artifactId>
|
||||
<version>${expressly.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
|
@ -92,7 +102,7 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-messaging</artifactId>
|
||||
<version>${spring-security.version}</version>
|
||||
<version>${spring-security-messaging.version}</version>
|
||||
</dependency>
|
||||
<!-- Logging -->
|
||||
<dependency>
|
||||
|
@ -107,24 +117,9 @@
|
|||
</dependency>
|
||||
<!-- Servlet -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp.jstl</groupId>
|
||||
<artifactId>jstl-api</artifactId>
|
||||
<version>${jstl-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp</groupId>
|
||||
<artifactId>javax.servlet.jsp-api</artifactId>
|
||||
<version>${javax.servlet.jsp-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>${jstl.version}</version>
|
||||
<groupId>jakarta.platform</groupId>
|
||||
<artifactId>jakarta.jakartaee-api</artifactId>
|
||||
<version>${jakartaee-api.version}</version>
|
||||
</dependency>
|
||||
<!-- Jackson Dependencies -->
|
||||
<dependency>
|
||||
|
@ -144,17 +139,11 @@
|
|||
</dependency>
|
||||
<!-- Test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>${spring-boot-starter-test.version}</version>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>${jaxb-api.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -194,11 +183,15 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<hibernate-core.version>5.2.10.Final</hibernate-core.version>
|
||||
<spring-data-jpa.version>1.11.3.RELEASE</spring-data-jpa.version>
|
||||
<spring-boot-starter-test.version>1.5.10.RELEASE</spring-boot-starter-test.version>
|
||||
<spring-security.version>6.1.5</spring-security.version>
|
||||
<spring-security-messaging.version>6.0.2</spring-security-messaging.version>
|
||||
<hibernate-core.version>6.1.7.Final</hibernate-core.version>
|
||||
<hibernate-validator.version>8.0.1.Final</hibernate-validator.version>
|
||||
<expressly.version>5.0.0</expressly.version>
|
||||
<spring-data-jpa.version>3.1.0</spring-data-jpa.version>
|
||||
<spring-boot-starter-test.version>3.1.0</spring-boot-starter-test.version>
|
||||
<jakartaee-api.version>10.0.0</jakartaee-api.version>
|
||||
<cargo-maven2-plugin.version>1.7.6</cargo-maven2-plugin.version>
|
||||
<jaxb-api.version>2.3.1</jaxb-api.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -9,7 +9,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
|||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.resource.PathResourceResolver;
|
||||
import org.springframework.web.servlet.view.JstlView;
|
||||
import org.springframework.web.servlet.view.UrlBasedViewResolver;
|
||||
|
@ -20,8 +20,9 @@ import java.sql.SQLException;
|
|||
@EnableJpaRepositories
|
||||
@ComponentScan("com.baeldung.springsecuredsockets")
|
||||
@Import({ SecurityConfig.class, DataStoreConfig.class, SocketBrokerConfig.class, SocketSecurityConfig.class })
|
||||
public class AppConfig extends WebMvcConfigurerAdapter {
|
||||
public class AppConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addViewController("/").setViewName("index");
|
||||
registry.addViewController("/login").setViewName("login");
|
||||
|
|
|
@ -13,7 +13,7 @@ import org.springframework.orm.jpa.vendor.Database;
|
|||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Properties;
|
||||
|
||||
|
|
|
@ -6,11 +6,14 @@ import org.springframework.context.annotation.ComponentScan;
|
|||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
||||
import org.springframework.security.config.Customizer;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
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.configuration.WebSecurityCustomizer;
|
||||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||||
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
@ -86,43 +89,30 @@ public class SecurityConfig {
|
|||
*/
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/", "/index", "/authenticate")
|
||||
.permitAll()
|
||||
.antMatchers("/secured/**/**", "/secured/**/**/**", "/secured/socket", "/secured/success")
|
||||
.authenticated()
|
||||
.anyRequest()
|
||||
.authenticated()
|
||||
.and()
|
||||
.formLogin()
|
||||
.loginPage("/login")
|
||||
.permitAll()
|
||||
.usernameParameter("username")
|
||||
.passwordParameter("password")
|
||||
.loginProcessingUrl("/authenticate")
|
||||
.successHandler(loginSuccessHandler())
|
||||
.failureUrl("/denied")
|
||||
.permitAll()
|
||||
.and()
|
||||
.logout()
|
||||
.logoutSuccessHandler(logoutSuccessHandler())
|
||||
.and()
|
||||
http.authorizeHttpRequests(authorizationManagerRequestMatcherRegistry ->
|
||||
authorizationManagerRequestMatcherRegistry
|
||||
.requestMatchers("/", "/index", "/authenticate").permitAll()
|
||||
.requestMatchers("/secured/**/**", "/secured/**/**/**", "/secured/socket", "/secured/success").authenticated()
|
||||
.anyRequest().authenticated())
|
||||
.formLogin(httpSecurityFormLoginConfigurer -> httpSecurityFormLoginConfigurer.loginPage("/login").permitAll()
|
||||
.usernameParameter("username")
|
||||
.passwordParameter("password")
|
||||
.loginProcessingUrl("/authenticate")
|
||||
.successHandler(loginSuccessHandler())
|
||||
.failureUrl("/denied").permitAll())
|
||||
.logout(httpSecurityLogoutConfigurer -> httpSecurityLogoutConfigurer.logoutSuccessHandler(logoutSuccessHandler()))
|
||||
/**
|
||||
* Applies to User Roles - not to login failures or unauthenticated access attempts.
|
||||
*/
|
||||
.exceptionHandling()
|
||||
.accessDeniedHandler(accessDeniedHandler())
|
||||
.and()
|
||||
.exceptionHandling(httpSecurityExceptionHandlingConfigurer -> httpSecurityExceptionHandlingConfigurer.accessDeniedHandler(accessDeniedHandler()))
|
||||
.authenticationProvider(authenticationProvider());
|
||||
|
||||
/** Disabled for local testing */
|
||||
http.csrf()
|
||||
.disable();
|
||||
http.csrf(AbstractHttpConfigurer::disable);
|
||||
|
||||
/** This is solely required to support H2 console viewing in Spring MVC with Spring Security */
|
||||
http.headers()
|
||||
.frameOptions()
|
||||
.disable();
|
||||
http.headers(httpSecurityHeadersConfigurer -> httpSecurityHeadersConfigurer.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable))
|
||||
.authorizeHttpRequests(Customizer.withDefaults());
|
||||
return http.build();
|
||||
}
|
||||
|
||||
|
@ -135,8 +125,7 @@ public class SecurityConfig {
|
|||
|
||||
@Bean
|
||||
public WebSecurityCustomizer webSecurityCustomizer() {
|
||||
return (web) -> web.ignoring()
|
||||
.antMatchers("/resources/**");
|
||||
return (web) -> web.ignoring().requestMatchers("/resources/**");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,14 +8,14 @@ import static com.baeldung.springsecuredsockets.Constants.SECURED_CHAT_SPECIFIC_
|
|||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
||||
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
||||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSocketMessageBroker
|
||||
@ComponentScan("com.baeldung.springsecuredsockets.controllers")
|
||||
public class SocketBrokerConfig extends AbstractWebSocketMessageBrokerConfigurer {
|
||||
public class SocketBrokerConfig implements WebSocketMessageBrokerConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureMessageBroker(MessageBrokerRegistry config) {
|
||||
|
|
|
@ -4,9 +4,9 @@ import org.springframework.web.WebApplicationInitializer;
|
|||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRegistration;
|
||||
import jakarta.servlet.ServletContext;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.ServletRegistration;
|
||||
|
||||
public class WebAppInitializer implements WebApplicationInitializer {
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ import org.springframework.stereotype.Controller;
|
|||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
@Controller
|
||||
public class CsrfTokenController {
|
||||
|
|
|
@ -30,8 +30,7 @@ public class SocketController {
|
|||
@MessageMapping(SECURED_CHAT)
|
||||
@SendTo(SECURED_CHAT_HISTORY)
|
||||
public OutputMessage sendAll(Message msg) throws Exception {
|
||||
OutputMessage out = new OutputMessage(msg.getFrom(), msg.getText(), new SimpleDateFormat("HH:mm").format(new Date()));
|
||||
return out;
|
||||
return new OutputMessage(msg.getFrom(), msg.getText(), new SimpleDateFormat("HH:mm").format(new Date()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.baeldung.springsecuredsockets.domain;
|
||||
|
||||
import javax.persistence.*;
|
||||
import jakarta.persistence.*;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
package com.baeldung.springsecuredsockets.domain;
|
||||
|
||||
import javax.persistence.*;
|
||||
import jakarta.persistence.*;
|
||||
import java.util.Set;
|
||||
|
||||
//Custom User Model
|
||||
|
||||
@Entity
|
||||
@Table(name = "user")
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
|
|
|
@ -4,9 +4,9 @@ import org.springframework.http.HttpStatus;
|
|||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.web.access.AccessDeniedHandler;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
|
|
|
@ -7,8 +7,8 @@ import org.springframework.http.HttpStatus;
|
|||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
public class CustomLoginSuccessHandler implements AuthenticationSuccessHandler {
|
||||
|
|
|
@ -5,15 +5,14 @@ import org.springframework.http.HttpStatus;
|
|||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
public class CustomLogoutSuccessHandler implements LogoutSuccessHandler {
|
||||
@Override
|
||||
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
|
||||
throws IOException, ServletException {
|
||||
throws IOException {
|
||||
|
||||
response.setStatus(HttpStatus.OK.value());
|
||||
response.sendRedirect(request.getContextPath() + "/index");
|
||||
|
|
|
@ -17,7 +17,7 @@ import org.springframework.stereotype.Service;
|
|||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
|
||||
@Service()
|
||||
@Service
|
||||
public class CustomUserDetailsService implements UserDetailsService {
|
||||
|
||||
Logger log = LoggerFactory.getLogger(CustomUserDetailsService.class);
|
||||
|
|
|
@ -9,9 +9,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>
|
||||
|
@ -42,10 +42,6 @@
|
|||
<artifactId>jaxb-runtime</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -77,9 +73,9 @@
|
|||
</plugin>
|
||||
<!-- end::xsd[] -->
|
||||
<plugin>
|
||||
<groupId>org.jvnet.jaxb2.maven2</groupId>
|
||||
<artifactId>maven-jaxb2-plugin</artifactId>
|
||||
<version>${maven-jaxb2-plugin.version}</version>
|
||||
<groupId>org.jvnet.jaxb</groupId>
|
||||
<artifactId>jaxb-maven-plugin</artifactId>
|
||||
<version>${jaxb-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
|
@ -103,7 +99,7 @@
|
|||
|
||||
<properties>
|
||||
<jakarta.xml.bind-api.version>4.0.0</jakarta.xml.bind-api.version>
|
||||
<jaxb-maven-plugin.version>4.0.0</jaxb-maven-plugin.version>
|
||||
<jaxb2-maven-plugin.version>3.1.0</jaxb2-maven-plugin.version>
|
||||
<maven-jaxb2-plugin.version>0.15.3</maven-jaxb2-plugin.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -3,7 +3,7 @@ package com.baeldung.springsoap;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.Assert;
|
||||
|
|
|
@ -9,9 +9,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>
|
||||
|
@ -31,6 +31,7 @@
|
|||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -1,22 +1,12 @@
|
|||
package com.baeldung.config;
|
||||
|
||||
import com.baeldung.contexts.Greeting;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
||||
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.JstlView;
|
||||
import org.thymeleaf.spring5.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
|
||||
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
|
||||
import org.thymeleaf.templatemode.TemplateMode;
|
||||
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
|
||||
import org.thymeleaf.templateresolver.ITemplateResolver;
|
||||
|
||||
import com.baeldung.contexts.Greeting;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* Web Configuration for the entire app
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.baeldung.contexts.services;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.contexts.Greeting;
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package com.baeldung.validation.listvalidation.constraint;
|
||||
|
||||
import jakarta.validation.Constraint;
|
||||
import jakarta.validation.Payload;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
|
||||
@Constraint(validatedBy = MaxSizeConstraintValidator.class)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MaxSizeConstraint {
|
||||
|
|
|
@ -2,10 +2,9 @@ package com.baeldung.validation.listvalidation.constraint;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
|
||||
import com.baeldung.validation.listvalidation.model.Movie;
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
|
||||
public class MaxSizeConstraintValidator implements ConstraintValidator<MaxSizeConstraint, List<Movie>> {
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package com.baeldung.validation.listvalidation.controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
import com.baeldung.validation.listvalidation.constraint.MaxSizeConstraint;
|
||||
import com.baeldung.validation.listvalidation.model.Movie;
|
||||
import com.baeldung.validation.listvalidation.service.MovieService;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
@ -12,9 +12,7 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.validation.listvalidation.constraint.MaxSizeConstraint;
|
||||
import com.baeldung.validation.listvalidation.model.Movie;
|
||||
import com.baeldung.validation.listvalidation.service.MovieService;
|
||||
import java.util.List;
|
||||
|
||||
@Validated
|
||||
@RestController
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
package com.baeldung.validation.listvalidation.exception;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
@ -12,6 +9,8 @@ import org.springframework.http.ResponseEntity;
|
|||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class ConstraintViolationExceptionHandler {
|
||||
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package com.baeldung.validation.listvalidation.model;
|
||||
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
public class Movie {
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ public class ControllerAnnotationIntegrationTest {
|
|||
@Test
|
||||
public void testTestController() throws Exception {
|
||||
|
||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/test/")).andReturn().getModelAndView();
|
||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/test")).andReturn().getModelAndView();
|
||||
|
||||
// validate modal data
|
||||
Assert.assertSame(mv.getModelMap().get("data").toString(), "Welcome home man");
|
||||
|
|
|
@ -40,7 +40,7 @@ public class ControllerIntegrationTest {
|
|||
@Test
|
||||
public void testTestController() throws Exception {
|
||||
|
||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/test/")).andReturn().getModelAndView();
|
||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/test")).andReturn().getModelAndView();
|
||||
|
||||
// validate modal data
|
||||
Assert.assertSame(mv.getModelMap().get("data").toString(), "Welcome home man");
|
||||
|
|
|
@ -33,7 +33,7 @@
|
|||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.24</version>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
|
@ -68,6 +68,17 @@
|
|||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.ua-parser</groupId>
|
||||
<artifactId>uap-java</artifactId>
|
||||
<version>${uap.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -101,6 +112,8 @@
|
|||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<jetty-maven-plugin.version>10.0.4</jetty-maven-plugin.version>
|
||||
<commons-text.version>1.10.0</commons-text.version>
|
||||
<mockito.version>5.6.0</mockito.version>
|
||||
<uap.version>1.5.4</uap.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.servlets;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.*;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.baeldung.servlets.clientinfo.AccountLogic;
|
||||
|
||||
@WebServlet(name = "AccountServlet", urlPatterns = "/account")
|
||||
public class AccountServlet extends HttpServlet {
|
||||
public static final Logger log = LoggerFactory.getLogger(AccountServlet.class);
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
AccountLogic accountLogic = new AccountLogic();
|
||||
Map<String, String> clientInfo = accountLogic.getClientInfo(request);
|
||||
log.info("Request client info: {}, " + clientInfo);
|
||||
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.servlets.clientinfo;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import ua_parser.Client;
|
||||
import ua_parser.Parser;
|
||||
|
||||
public class AccountLogic {
|
||||
public Map<String, String> getClientInfo(HttpServletRequest request) {
|
||||
String remoteAddr = request.getRemoteAddr();
|
||||
String remoteHost = request.getRemoteHost();
|
||||
String remoteUser = request.getRemoteUser();
|
||||
String contentType = request.getHeader("content-type");
|
||||
String userAgent = request.getHeader("user-agent");
|
||||
|
||||
Parser uaParser = new Parser();
|
||||
Client client = uaParser.parse(userAgent);
|
||||
|
||||
Map<String, String> clientInfo = new HashMap<>();
|
||||
clientInfo.put("os_family", client.os.family);
|
||||
clientInfo.put("device_family", client.device.family);
|
||||
clientInfo.put("userAgent_family", client.userAgent.family);
|
||||
clientInfo.put("remote_address", remoteAddr);
|
||||
clientInfo.put("remote_host", remoteHost);
|
||||
clientInfo.put("remote_user", remoteUser);
|
||||
clientInfo.put("content_type", contentType);
|
||||
return clientInfo;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.servlets.clientinfo;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
public class ClientInformationUnitTest {
|
||||
|
||||
@Test
|
||||
void givenMockHttpServletRequestWithHeaders_whenGetClientInfo_thenReturnsUserAGentInfo() {
|
||||
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
|
||||
|
||||
when(request.getHeader("user-agent")).thenReturn("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36, acceptLanguage:en-US,en;q=0.9");
|
||||
when(request.getHeader("content-type")).thenReturn("application/json");
|
||||
when(request.getRemoteAddr()).thenReturn("198.167.0.1");
|
||||
when(request.getRemoteHost()).thenReturn("baeldung.com");
|
||||
when(request.getRemoteUser()).thenReturn("baeldung");
|
||||
|
||||
AccountLogic accountLogic = new AccountLogic();
|
||||
Map<String, String> clientInfo = accountLogic.getClientInfo(request);
|
||||
assertThat(clientInfo.get("os_family")).isEqualTo("Mac OS X");
|
||||
assertThat(clientInfo.get("device_family")).isEqualTo("Mac");
|
||||
assertThat(clientInfo.get("userAgent_family")).isEqualTo("Chrome");
|
||||
assertThat(clientInfo.get("content_type")).isEqualTo("application/json");
|
||||
assertThat(clientInfo.get("remote_user")).isEqualTo("baeldung");
|
||||
assertThat(clientInfo.get("remote_address")).isEqualTo("198.167.0.1");
|
||||
assertThat(clientInfo.get("remote_host")).isEqualTo("baeldung.com");
|
||||
}
|
||||
}
|
|
@ -10,3 +10,5 @@ This module contains articles about eXtensible Markup Language (XML)
|
|||
- [Convert an XML Object to a String in Java](https://www.baeldung.com/java-convert-xml-object-string)
|
||||
- [Convert String Containing XML to org.w3c.dom.Document](https://www.baeldung.com/java-convert-string-xml-dom)
|
||||
- [How to Parse XML to HashMap in Java](https://www.baeldung.com/java-xml-read-into-hashmap)
|
||||
- [Convert an XML File to CSV File](https://www.baeldung.com/java-convert-xml-csv)
|
||||
- - More articles: [[prev -->]](../xml)
|
|
@ -13,4 +13,4 @@ This module contains articles about eXtensible Markup Language (XML)
|
|||
- [Parsing an XML File Using StAX](https://www.baeldung.com/java-stax)
|
||||
- [Parsing an XML File Using SAX Parser](https://www.baeldung.com/java-sax-parser)
|
||||
- [Remove HTML Tags Using Java](https://www.baeldung.com/java-remove-html-tags)
|
||||
- [Convert an XML File to CSV File](https://www.baeldung.com/java-convert-xml-csv)
|
||||
- More articles: [[next -->]](../xml-2)
|
||||
|
|
Loading…
Reference in New Issue