Merge branch 'eugenp:master' into master
This commit is contained in:
commit
0f23df40db
@ -71,7 +71,7 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<axon-bom.version>4.5.17</axon-bom.version>
|
||||
<axon-bom.version>4.6.0</axon-bom.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -92,6 +92,11 @@ public class OrderRestEndpoint {
|
||||
return orderQueryService.findAllOrders();
|
||||
}
|
||||
|
||||
@GetMapping(path = "/all-orders-streaming", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public Flux<OrderResponse> allOrdersStreaming() {
|
||||
return orderQueryService.allOrdersStreaming();
|
||||
}
|
||||
|
||||
@GetMapping("/total-shipped/{product-id}")
|
||||
public Integer totalShipped(@PathVariable("product-id") String productId) {
|
||||
return orderQueryService.totalShipped(productId);
|
||||
|
@ -17,7 +17,10 @@ import org.axonframework.config.ProcessingGroup;
|
||||
import org.axonframework.eventhandling.EventHandler;
|
||||
import org.axonframework.queryhandling.QueryHandler;
|
||||
import org.axonframework.queryhandling.QueryUpdateEmitter;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@ -101,6 +104,11 @@ public class InMemoryOrdersEventHandler implements OrdersEventHandler {
|
||||
return new ArrayList<>(orders.values());
|
||||
}
|
||||
|
||||
@QueryHandler
|
||||
public Publisher<Order> handleStreaming(FindAllOrderedProductsQuery query) {
|
||||
return Mono.fromCallable(orders::values).flatMapMany(Flux::fromIterable);
|
||||
}
|
||||
|
||||
@QueryHandler
|
||||
public Integer handle(TotalProductsShippedQuery query) {
|
||||
return orders.values()
|
||||
|
@ -9,6 +9,7 @@ import org.axonframework.messaging.responsetypes.ResponseType;
|
||||
import org.axonframework.messaging.responsetypes.ResponseTypes;
|
||||
import org.axonframework.queryhandling.QueryGateway;
|
||||
import org.axonframework.queryhandling.SubscriptionQueryResult;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
@ -34,6 +35,11 @@ public class OrderQueryService {
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public Flux<OrderResponse> allOrdersStreaming() {
|
||||
Publisher<Order> publisher = queryGateway.streamingQuery(new FindAllOrderedProductsQuery(), Order.class);
|
||||
return Flux.from(publisher).map(OrderResponse::new);
|
||||
}
|
||||
|
||||
public Integer totalShipped(String productId) {
|
||||
return queryGateway.scatterGather(new TotalProductsShippedQuery(productId),
|
||||
ResponseTypes.instanceOf(Integer.class), 10L, TimeUnit.SECONDS)
|
||||
|
@ -11,6 +11,7 @@ import com.baeldung.axon.coreapi.queries.FindAllOrderedProductsQuery;
|
||||
import com.baeldung.axon.coreapi.queries.Order;
|
||||
import com.baeldung.axon.coreapi.queries.OrderUpdatesQuery;
|
||||
import com.baeldung.axon.coreapi.queries.TotalProductsShippedQuery;
|
||||
import org.reactivestreams.Publisher;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -32,6 +33,8 @@ public interface OrdersEventHandler {
|
||||
|
||||
List<Order> handle(FindAllOrderedProductsQuery query);
|
||||
|
||||
Publisher<Order> handleStreaming(FindAllOrderedProductsQuery query);
|
||||
|
||||
Integer handle(TotalProductsShippedQuery query);
|
||||
|
||||
Order handle(OrderUpdatesQuery query);
|
||||
|
@ -10,6 +10,10 @@ POST http://localhost:8080/ship-unconfirmed-order
|
||||
|
||||
GET http://localhost:8080/all-orders
|
||||
|
||||
### Receive all existing orders using a stream
|
||||
|
||||
GET http://localhost:8080/all-orders-streaming
|
||||
|
||||
### Create Order with id 666a1661-474d-4046-8b12-8b5896312768
|
||||
|
||||
POST http://localhost:8080/order/666a1661-474d-4046-8b12-8b5896312768
|
||||
|
@ -14,10 +14,13 @@ import com.baeldung.axon.coreapi.queries.OrderUpdatesQuery;
|
||||
import com.baeldung.axon.coreapi.queries.TotalProductsShippedQuery;
|
||||
import org.axonframework.queryhandling.QueryUpdateEmitter;
|
||||
import org.junit.jupiter.api.*;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
@ -68,6 +71,26 @@ public abstract class AbstractOrdersEventHandlerUnitTest {
|
||||
assertEquals(orderTwo, order_2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoOrdersPlacedOfWhichOneNotShipped_whenFindAllOrderedProductsQueryStreaming_thenCorrectOrdersAreReturned() {
|
||||
resetWithTwoOrders();
|
||||
final Consumer<Order> orderVerifier = order -> {
|
||||
if (order.getOrderId().equals(orderOne.getOrderId())) {
|
||||
assertEquals(orderOne, order);
|
||||
} else if (order.getOrderId().equals(orderTwo.getOrderId())) {
|
||||
assertEquals(orderTwo, order);
|
||||
} else {
|
||||
throw new RuntimeException("Would expect either order one or order two");
|
||||
}
|
||||
};
|
||||
|
||||
StepVerifier.create(Flux.from(handler.handleStreaming(new FindAllOrderedProductsQuery())))
|
||||
.assertNext(orderVerifier)
|
||||
.assertNext(orderVerifier)
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNoOrdersPlaced_whenTotalProductsShippedQuery_thenZeroReturned() {
|
||||
assertEquals(0, handler.handle(new TotalProductsShippedQuery(PRODUCT_ID_1)));
|
||||
|
@ -6,6 +6,7 @@ import com.baeldung.axon.coreapi.events.OrderShippedEvent;
|
||||
import com.baeldung.axon.coreapi.events.ProductAddedEvent;
|
||||
import com.baeldung.axon.coreapi.events.ProductCountDecrementedEvent;
|
||||
import com.baeldung.axon.coreapi.events.ProductCountIncrementedEvent;
|
||||
import com.baeldung.axon.coreapi.queries.FindAllOrderedProductsQuery;
|
||||
import com.baeldung.axon.coreapi.queries.Order;
|
||||
|
||||
import org.axonframework.eventhandling.gateway.EventGateway;
|
||||
@ -13,6 +14,7 @@ import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.util.Collections;
|
||||
@ -60,6 +62,15 @@ class OrderQueryServiceIntegrationTest {
|
||||
.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenOrderCreatedEventSend_whenCallingAllOrdersStreaming_thenOneOrderIsReturned() {
|
||||
Flux<OrderResponse> result = queryService.allOrdersStreaming();
|
||||
StepVerifier.create(result)
|
||||
.assertNext(order -> assertEquals(orderId, order.getOrderId()))
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenThreeDeluxeChairsShipped_whenCallingAllShippedChairs_then234PlusTreeIsReturned() {
|
||||
Order order = new Order(orderId);
|
||||
|
@ -0,0 +1,43 @@
|
||||
package com.baeldung.listandset;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.Assert;
|
||||
import java.util.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ListAndSetUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenList_whenDuplicates_thenAllowed(){
|
||||
List<Integer> integerList = new ArrayList<>();
|
||||
integerList.add(2);
|
||||
integerList.add(3);
|
||||
integerList.add(4);
|
||||
integerList.add(4);
|
||||
assertEquals(integerList.size(), 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSet_whenDuplicates_thenNotAllowed(){
|
||||
Set<Integer> integerSet = new HashSet<>();
|
||||
integerSet.add(2);
|
||||
integerSet.add(3);
|
||||
integerSet.add(4);
|
||||
integerSet.add(4);
|
||||
assertEquals(integerSet.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSet_whenOrdering_thenMayBeAllowed(){
|
||||
Set<Integer> set1 = new LinkedHashSet<>();
|
||||
set1.add(2);
|
||||
set1.add(3);
|
||||
set1.add(4);
|
||||
Set<Integer> set2 = new LinkedHashSet<>();
|
||||
set2.add(2);
|
||||
set2.add(3);
|
||||
set2.add(4);
|
||||
Assert.assertArrayEquals(set1.toArray(), set2.toArray());
|
||||
}
|
||||
}
|
@ -56,6 +56,12 @@
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -80,6 +86,7 @@
|
||||
<assertj.version>3.22.0</assertj.version>
|
||||
<maven.compiler.source.version>17</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>17</maven.compiler.target.version>
|
||||
<spring.version>5.3.22</spring.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,132 @@
|
||||
package com.baeldung.list.tostring;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ListToStringUnitTest {
|
||||
|
||||
@Test
|
||||
void givenAListOfString_whenUsingJava8_thenConvertToStringByUsingString() {
|
||||
|
||||
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
|
||||
|
||||
String commaSeparatedString = String.join(",", arraysAsList);
|
||||
|
||||
assertThat(commaSeparatedString).isEqualTo("ONE,TWO,THREE");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAListOfString_whenUsingJava8_thenConvertToStringByUsingStringJoiner() {
|
||||
|
||||
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
|
||||
|
||||
StringJoiner stringJoiner = new StringJoiner(",");
|
||||
arraysAsList.stream()
|
||||
.forEach(v -> stringJoiner.add(v));
|
||||
String commaSeparatedString = stringJoiner.toString();
|
||||
|
||||
assertThat(commaSeparatedString).isEqualTo("ONE,TWO,THREE");
|
||||
|
||||
StringJoiner stringJoinerWithDelimiterPrefixSuffix = new StringJoiner(",", "[", "]");
|
||||
arraysAsList.stream()
|
||||
.forEach(v -> stringJoinerWithDelimiterPrefixSuffix.add(v));
|
||||
String commaSeparatedStringWithDelimiterPrefixSuffix = stringJoinerWithDelimiterPrefixSuffix.toString();
|
||||
|
||||
assertThat(commaSeparatedStringWithDelimiterPrefixSuffix).isEqualTo("[ONE,TWO,THREE]");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAListOfString_whenUsingJava8_thenConvertToStringByUsingCollectors() {
|
||||
|
||||
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
|
||||
|
||||
String commaSeparatedUsingCollect = arraysAsList.stream()
|
||||
.collect(Collectors.joining(","));
|
||||
|
||||
assertThat(commaSeparatedUsingCollect).isEqualTo("ONE,TWO,THREE");
|
||||
|
||||
String commaSeparatedObjectToString = arraysAsList.stream()
|
||||
.map(Object::toString)
|
||||
.collect(Collectors.joining(","));
|
||||
|
||||
assertThat(commaSeparatedObjectToString).isEqualTo("ONE,TWO,THREE");
|
||||
|
||||
String commaSeparatedStringValueOf = arraysAsList.stream()
|
||||
.map(String::valueOf)
|
||||
.collect(Collectors.joining(","));
|
||||
|
||||
assertThat(commaSeparatedStringValueOf).isEqualTo("ONE,TWO,THREE");
|
||||
|
||||
String commaSeparatedStringValueOfWithDelimiterPrefixSuffix = arraysAsList.stream()
|
||||
.map(String::valueOf)
|
||||
.collect(Collectors.joining(",", "[", "]"));
|
||||
|
||||
assertThat(commaSeparatedStringValueOfWithDelimiterPrefixSuffix).isEqualTo("[ONE,TWO,THREE]");
|
||||
|
||||
String commaSeparatedUsingReduce = arraysAsList.stream()
|
||||
.reduce((x, y) -> x + "," + y)
|
||||
.get();
|
||||
|
||||
assertThat(commaSeparatedUsingReduce).isEqualTo("ONE,TWO,THREE");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAListOfString_whenUsingApacheCommonsLang_thenConvertToStringByUsingStringUtils() {
|
||||
|
||||
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
|
||||
|
||||
String commaSeparatedString = org.apache.commons.lang3.StringUtils.join(arraysAsList, ",");
|
||||
|
||||
assertThat(commaSeparatedString).isEqualTo("ONE,TWO,THREE");
|
||||
|
||||
String commaSeparatedStringIndex = org.apache.commons.lang3.StringUtils.join(arraysAsList.toArray(), ",", 0, 3);
|
||||
|
||||
assertThat(commaSeparatedStringIndex).isEqualTo("ONE,TWO,THREE");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAListOfString_whenUsingSpringCore_thenConvertToStringByUsingStringUtils() {
|
||||
|
||||
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
|
||||
|
||||
String collectionToCommaDelimitedString = org.springframework.util.StringUtils.collectionToCommaDelimitedString(arraysAsList);
|
||||
|
||||
assertThat(collectionToCommaDelimitedString).isEqualTo("ONE,TWO,THREE");
|
||||
|
||||
String collectionToDelimitedString = org.springframework.util.StringUtils.collectionToDelimitedString(arraysAsList, ",");
|
||||
|
||||
assertThat(collectionToDelimitedString).isEqualTo("ONE,TWO,THREE");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAListOfString_whenUsingGoogleGuava_thenConvertToStringByUsingJoiner() {
|
||||
|
||||
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
|
||||
|
||||
String commaSeparatedString = com.google.common.base.Joiner.on(",")
|
||||
.join(arraysAsList);
|
||||
|
||||
assertThat(commaSeparatedString).isEqualTo("ONE,TWO,THREE");
|
||||
|
||||
List<String> arraysAsListWithNull = Arrays.asList("ONE", null, "TWO", null, "THREE");
|
||||
|
||||
String commaSeparatedStringSkipNulls = com.google.common.base.Joiner.on(",")
|
||||
.skipNulls()
|
||||
.join(arraysAsListWithNull);
|
||||
|
||||
assertThat(commaSeparatedStringSkipNulls).isEqualTo("ONE,TWO,THREE");
|
||||
|
||||
String commaSeparatedStringUseForNull = com.google.common.base.Joiner.on(",")
|
||||
.useForNull(" ")
|
||||
.join(arraysAsListWithNull);
|
||||
|
||||
assertThat(commaSeparatedStringUseForNull).isEqualTo("ONE, ,TWO, ,THREE");
|
||||
}
|
||||
|
||||
}
|
@ -3,4 +3,5 @@ This module contains articles about date operations in Java.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Create Date From Unix Timestamp in Java](https://www.baeldung.com/java-date-unix-timestamp)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-date-operations-2)
|
||||
|
@ -5,3 +5,4 @@
|
||||
### Relevant Articles:
|
||||
- [Java 8 Predicate Chain](https://www.baeldung.com/java-predicate-chain)
|
||||
- [Use Cases for Static Methods in Java](https://www.baeldung.com/java-static-methods-use-cases)
|
||||
- [TriFunction Interface in Java](https://www.baeldung.com/java-trifunction)
|
||||
|
@ -11,4 +11,5 @@ This module contains articles about core Java input and output (IO)
|
||||
- [Read User Input Until a Condition is Met](https://www.baeldung.com/java-read-input-until-condition)
|
||||
- [Java Scanner.skip method with examples](https://www.baeldung.com/java-scanner-skip)
|
||||
- [Generate the MD5 Checksum for a File in Java](https://www.baeldung.com/java-md5-checksum-file)
|
||||
- [Getting the Filename From a String Containing an Absolute File Path](https://www.baeldung.com/java-filename-full-path)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-io-3)
|
||||
|
@ -4,3 +4,4 @@ This module contains articles about core Java input/output(IO) APIs.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Constructing a Relative Path From Two Absolute Paths in Java](https://www.baeldung.com/java-relative-path-absolute)
|
||||
- [Java Scanner Taking a Character Input](https://www.baeldung.com/java-scanner-character-input)
|
||||
|
@ -7,4 +7,5 @@
|
||||
- [Evaluating a Math Expression in Java](https://www.baeldung.com/java-evaluate-math-expression-string)
|
||||
- [Swap Two Variables in Java](https://www.baeldung.com/java-swap-two-variables)
|
||||
- [Java Program to Find the Roots of a Quadratic Equation](https://www.baeldung.com/roots-quadratic-equation/)
|
||||
- [Create a BMI Calculator in Java](https://www.baeldung.com/java-body-mass-index-calculator)
|
||||
- More articles: [[<-- Prev]](/core-java-modules/core-java-lang-math-2)
|
||||
|
@ -0,0 +1,51 @@
|
||||
package com.baeldung.portscanner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class PortScanner {
|
||||
private static final int poolSize = 10;
|
||||
private static final int timeOut = 200;
|
||||
|
||||
public void runPortScan(String ip, int nbrPortMaxToScan) throws IOException, RuntimeException {
|
||||
ConcurrentLinkedQueue openPorts = new ConcurrentLinkedQueue<>();
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
|
||||
AtomicInteger port = new AtomicInteger(0);
|
||||
while (port.get() < nbrPortMaxToScan) {
|
||||
final int currentPort = port.getAndIncrement();
|
||||
executorService.submit(() -> {
|
||||
try {
|
||||
Socket socket = new Socket();
|
||||
socket.connect(new InetSocketAddress(ip, currentPort), timeOut);
|
||||
socket.close();
|
||||
openPorts.add(currentPort);
|
||||
System.out.println(ip + " ,port open: " + currentPort);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
executorService.shutdown();
|
||||
try {
|
||||
executorService.awaitTermination(10, TimeUnit.MINUTES);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
List openPortList = new ArrayList<>();
|
||||
System.out.println("openPortsQueue: " + openPorts.size());
|
||||
while (!openPorts.isEmpty()) {
|
||||
openPortList.add(openPorts.poll());
|
||||
}
|
||||
|
||||
openPortList.forEach(p -> System.out.println("port " + p + " is open"));
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.baeldung.portScanner;
|
||||
|
||||
import com.baeldung.portscanner.PortScanner;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
class PortScannerUnitTest {
|
||||
|
||||
private static final int nbrPortMax = 1000; // Max is 65535, number of available ports
|
||||
private static final String ip = "127.0.0.1";
|
||||
PortScanner portScanner = new PortScanner();
|
||||
|
||||
@Test public void when_Run_then_lunchPortScan() throws IOException, RuntimeException {
|
||||
portScanner.runPortScan(ip, nbrPortMax);
|
||||
}
|
||||
}
|
@ -5,3 +5,4 @@
|
||||
- [Armstrong Numbers in Java](https://www.baeldung.com/java-armstrong-numbers)
|
||||
- [List All Factors of a Number in Java](https://www.baeldung.com/java-list-factors-integer)
|
||||
- [Make Division of Two Integers Result in a Float](https://www.baeldung.com/java-integer-division-float-result)
|
||||
- [Creating Random Numbers With No Duplicates in Java](https://www.baeldung.com/java-unique-random-numbers)
|
||||
|
@ -2,3 +2,4 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Compare Characters in Java](https://www.baeldung.com/java-compare-characters)
|
||||
- [String Concatenation in Java](https://www.baeldung.com/java-string-concatenation)
|
||||
|
@ -3,3 +3,4 @@
|
||||
### Relevant Articles:
|
||||
- [Generating Alphanumeric UUID String in Java](https://www.baeldung.com/java-generate-alphanumeric-uuid)
|
||||
- [Guide to UUID in Java](http://www.baeldung.com/java-uuid)
|
||||
- [Validate UUID String in Java](https://www.baeldung.com/java-validate-uuid-string)
|
||||
|
@ -6,4 +6,5 @@
|
||||
- [Difference Between links and depends_on in Docker Compose](https://www.baeldung.com/ops/docker-compose-links-depends-on)
|
||||
- [Mounting Multiple Volumes on a Docker Container](https://www.baeldung.com/ops/docker-mounting-multiple-volumes)
|
||||
- [Rebuild Docker Container in Docker Compose](https://www.baeldung.com/rebuild-docker-container-compose/)
|
||||
- [Assign Static IP to Docker Container and Docker-Compose](https://www.baeldung.com/ops/docker-assign-static-ip-container)
|
||||
|
||||
|
@ -3,3 +3,5 @@
|
||||
- [Pushing a Docker Image to a Private Repository](https://www.baeldung.com/ops/docker-push-image-to-private-repository)
|
||||
- [How to Include Files Outside of Docker’s Build Context](https://www.baeldung.com/ops/docker-include-files-outside-build-context)
|
||||
- [Adding a Comment in a Dockerfile](https://www.baeldung.com/ops/docker-dockerfile-comments/)
|
||||
- [Updating PATH Environment Variable in Dockerfile](https://www.baeldung.com/ops/dockerfile-path-environment-variable)
|
||||
- [Keep Subdirectory Structure in Dockerfile Copy](https://www.baeldung.com/ops/dockerfile-copy-same-subdirectory-structure)
|
||||
|
@ -2,3 +2,4 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Dockerizing a Java Application](https://www.baeldung.com/java-dockerize-app)
|
||||
- [Debugging an Application Running in Docker With IntelliJ IDEA](https://www.baeldung.com/docker-debug-app-with-intellij)
|
||||
|
88
jmeter/src/main/resources/JMeter - Latency Vs Load Time.jmx
Normal file
88
jmeter/src/main/resources/JMeter - Latency Vs Load Time.jmx
Normal file
@ -0,0 +1,88 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.5">
|
||||
<hashTree>
|
||||
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="LatencyVsRespomseTime" enabled="true">
|
||||
<stringProp name="TestPlan.comments"></stringProp>
|
||||
<boolProp name="TestPlan.functional_mode">false</boolProp>
|
||||
<boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
|
||||
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
|
||||
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
|
||||
<collectionProp name="Arguments.arguments"/>
|
||||
</elementProp>
|
||||
<stringProp name="TestPlan.user_define_classpath"></stringProp>
|
||||
</TestPlan>
|
||||
<hashTree>
|
||||
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
|
||||
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
|
||||
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
|
||||
<boolProp name="LoopController.continue_forever">false</boolProp>
|
||||
<stringProp name="LoopController.loops">1</stringProp>
|
||||
</elementProp>
|
||||
<stringProp name="ThreadGroup.num_threads">1</stringProp>
|
||||
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
|
||||
<boolProp name="ThreadGroup.scheduler">false</boolProp>
|
||||
<stringProp name="ThreadGroup.duration"></stringProp>
|
||||
<stringProp name="ThreadGroup.delay"></stringProp>
|
||||
<boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>
|
||||
</ThreadGroup>
|
||||
<hashTree>
|
||||
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request" enabled="true">
|
||||
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
|
||||
<collectionProp name="Arguments.arguments"/>
|
||||
</elementProp>
|
||||
<stringProp name="HTTPSampler.domain"></stringProp>
|
||||
<stringProp name="HTTPSampler.port"></stringProp>
|
||||
<stringProp name="HTTPSampler.protocol"></stringProp>
|
||||
<stringProp name="HTTPSampler.contentEncoding"></stringProp>
|
||||
<stringProp name="HTTPSampler.path">http://localhost:3000</stringProp>
|
||||
<stringProp name="HTTPSampler.method">GET</stringProp>
|
||||
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
|
||||
<boolProp name="HTTPSampler.auto_redirects">false</boolProp>
|
||||
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
|
||||
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
|
||||
<stringProp name="HTTPSampler.embedded_url_re"></stringProp>
|
||||
<stringProp name="HTTPSampler.connect_timeout"></stringProp>
|
||||
<stringProp name="HTTPSampler.response_timeout"></stringProp>
|
||||
</HTTPSamplerProxy>
|
||||
<hashTree/>
|
||||
<ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="true">
|
||||
<boolProp name="ResultCollector.error_logging">false</boolProp>
|
||||
<objProp>
|
||||
<name>saveConfig</name>
|
||||
<value class="SampleSaveConfiguration">
|
||||
<time>true</time>
|
||||
<latency>true</latency>
|
||||
<timestamp>true</timestamp>
|
||||
<success>true</success>
|
||||
<label>true</label>
|
||||
<code>true</code>
|
||||
<message>true</message>
|
||||
<threadName>true</threadName>
|
||||
<dataType>true</dataType>
|
||||
<encoding>false</encoding>
|
||||
<assertions>true</assertions>
|
||||
<subresults>true</subresults>
|
||||
<responseData>false</responseData>
|
||||
<samplerData>false</samplerData>
|
||||
<xml>false</xml>
|
||||
<fieldNames>true</fieldNames>
|
||||
<responseHeaders>false</responseHeaders>
|
||||
<requestHeaders>false</requestHeaders>
|
||||
<responseDataOnError>false</responseDataOnError>
|
||||
<saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
|
||||
<assertionsResultsToSave>0</assertionsResultsToSave>
|
||||
<bytes>true</bytes>
|
||||
<sentBytes>true</sentBytes>
|
||||
<url>true</url>
|
||||
<threadCounts>true</threadCounts>
|
||||
<idleTime>true</idleTime>
|
||||
<connectTime>true</connectTime>
|
||||
</value>
|
||||
</objProp>
|
||||
<stringProp name="filename"></stringProp>
|
||||
</ResultCollector>
|
||||
<hashTree/>
|
||||
</hashTree>
|
||||
</hashTree>
|
||||
</hashTree>
|
||||
</jmeterTestPlan>
|
@ -24,8 +24,8 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring.version>5.3.22</spring.version>
|
||||
<spring-security.version>5.6.0</spring-security.version>
|
||||
<spring.version>5.3.23</spring.version>
|
||||
<spring-security.version>5.7.3</spring-security.version>
|
||||
<spring-boot-starter-test.version>1.5.10.RELEASE</spring-boot-starter-test.version>
|
||||
</properties>
|
||||
|
||||
|
@ -4,3 +4,4 @@
|
||||
- [DAO vs Repository Patterns](https://www.baeldung.com/java-dao-vs-repository)
|
||||
- [Difference Between MVC and MVP Patterns](https://www.baeldung.com/mvc-vs-mvp-pattern)
|
||||
- [The DTO Pattern (Data Transfer Object)](https://www.baeldung.com/java-dto-pattern)
|
||||
- [SEDA With Spring Integration and Apache Camel](https://www.baeldung.com/spring-apache-camel-seda-integration)
|
||||
|
@ -8,3 +8,4 @@
|
||||
- [JDBC Connection Status](https://www.baeldung.com/jdbc-connection-status)
|
||||
- [Get the Number of Rows in a ResultSet](https://www.baeldung.com/java-resultset-number-of-rows)
|
||||
- [Converting a JDBC ResultSet to JSON in Java](https://www.baeldung.com/java-jdbc-convert-resultset-to-json)
|
||||
- [Guide to MicroStream](https://www.baeldung.com/microstream-intro)
|
||||
|
@ -0,0 +1,157 @@
|
||||
package com.baeldung.mongo.insert;
|
||||
|
||||
import com.mongodb.*;
|
||||
import com.mongodb.client.FindIterable;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoCursor;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import org.bson.Document;
|
||||
import org.bson.conversions.Bson;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.mongodb.client.model.Filters.eq;
|
||||
import static com.mongodb.client.model.Filters.in;
|
||||
|
||||
public class InsertArrayOperation {
|
||||
|
||||
private static MongoClient mongoClient;
|
||||
private static MongoDatabase database;
|
||||
private static MongoCollection<Document> collection;
|
||||
private static DB db;
|
||||
private static DBCollection dbCollection;
|
||||
private static String collectionName;
|
||||
private static String databaseName;
|
||||
|
||||
public static void setUp() {
|
||||
if (mongoClient == null) {
|
||||
mongoClient = new MongoClient("localhost", 27017);
|
||||
|
||||
databaseName = "baeldung";
|
||||
collectionName = "student";
|
||||
|
||||
database = mongoClient.getDatabase(databaseName);
|
||||
collection = database.getCollection(collectionName);
|
||||
|
||||
db = mongoClient.getDB(databaseName);
|
||||
dbCollection = db.getCollection(collectionName);
|
||||
}
|
||||
}
|
||||
|
||||
public static void insertSingleBsonDocumentWithStringArray() {
|
||||
BasicDBList coursesList = new BasicDBList();
|
||||
coursesList.add("Chemistry");
|
||||
coursesList.add("Science");
|
||||
|
||||
DBObject student = new BasicDBObject().append("studentId", "STUD1")
|
||||
.append("name", "Jim")
|
||||
.append("age", 13)
|
||||
.append("courses", coursesList);
|
||||
|
||||
dbCollection.insert(student);
|
||||
|
||||
Bson filter = eq("studentId", "STUD1");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
while (cursor.hasNext()) {
|
||||
System.out.println(cursor.next());
|
||||
}
|
||||
}
|
||||
|
||||
public static void insertSingleDocumentWithStringArray() {
|
||||
List<String> coursesList = new ArrayList<>();
|
||||
coursesList.add("Science");
|
||||
coursesList.add("Geography");
|
||||
|
||||
Document student = new Document().append("studentId", "STUD2")
|
||||
.append("name", "Sam")
|
||||
.append("age", 13)
|
||||
.append("courses", coursesList);
|
||||
|
||||
collection.insertOne(student);
|
||||
|
||||
Bson filter = eq("studentId", "STUD2");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
while (cursor.hasNext()) {
|
||||
System.out.println(cursor.next());
|
||||
}
|
||||
}
|
||||
|
||||
public static void insertMultipleDocumentsWithStringArray() {
|
||||
List<String> coursesList1 = new ArrayList<>();
|
||||
coursesList1.add("Chemistry");
|
||||
coursesList1.add("Geography");
|
||||
|
||||
Document student1 = new Document().append("studentId", "STUD3")
|
||||
.append("name", "Sarah")
|
||||
.append("age", 12)
|
||||
.append("courses", coursesList1);
|
||||
|
||||
List<String> coursesList2 = new ArrayList<>();
|
||||
coursesList2.add("Maths");
|
||||
coursesList2.add("History");
|
||||
|
||||
Document student2 = new Document().append("studentId", "STUD4")
|
||||
.append("name", "Tom")
|
||||
.append("age", 13)
|
||||
.append("courses", coursesList2);
|
||||
|
||||
List<Document> students = new ArrayList<>();
|
||||
students.add(student1);
|
||||
students.add(student2);
|
||||
|
||||
collection.insertMany(students);
|
||||
|
||||
Bson filter = in("studentId", "STUD3", "STUD4");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
while (cursor.hasNext()) {
|
||||
System.out.println(cursor.next());
|
||||
}
|
||||
}
|
||||
|
||||
public static void insertSingleDocumentWithObjectArray() {
|
||||
Document course1 = new Document().append("name", "C1")
|
||||
.append("points", 5);
|
||||
|
||||
Document course2 = new Document().append("name", "C2")
|
||||
.append("points", 7);
|
||||
|
||||
List<Document> coursesList = new ArrayList<>();
|
||||
coursesList.add(course1);
|
||||
coursesList.add(course2);
|
||||
|
||||
Document student = new Document().append("studentId", "STUD5")
|
||||
.append("name", "Avin")
|
||||
.append("age", 13)
|
||||
.append("courses", coursesList);
|
||||
|
||||
collection.insertOne(student);
|
||||
|
||||
Bson filter = eq("studentId", "STUD5");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
while (cursor.hasNext()) {
|
||||
System.out.println(cursor.next());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
|
||||
setUp();
|
||||
|
||||
insertSingleBsonDocumentWithStringArray();
|
||||
|
||||
insertSingleDocumentWithStringArray();
|
||||
|
||||
insertMultipleDocumentsWithStringArray();
|
||||
|
||||
insertSingleDocumentWithObjectArray();
|
||||
}
|
||||
}
|
@ -0,0 +1,150 @@
|
||||
package com.baeldung.mongo.insert;
|
||||
|
||||
import com.mongodb.*;
|
||||
import com.mongodb.client.FindIterable;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoCursor;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import org.bson.Document;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.mongodb.client.model.Filters.eq;
|
||||
import static com.mongodb.client.model.Filters.in;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class InsertArrayOperationLiveTest {
|
||||
|
||||
private static MongoClient mongoClient;
|
||||
private static MongoDatabase database;
|
||||
private static MongoCollection<Document> collection;
|
||||
private static DB db;
|
||||
private static DBCollection dbCollection;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
if (mongoClient == null) {
|
||||
mongoClient = new MongoClient("localhost", 27017);
|
||||
|
||||
database = mongoClient.getDatabase("baeldung");
|
||||
collection = database.getCollection("student");
|
||||
|
||||
db = mongoClient.getDB("baeldung");
|
||||
dbCollection = db.getCollection("student");
|
||||
|
||||
collection.drop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSingleStudentObjectWithStringArray_whenInsertingDBObject_thenCheckingForDocument() {
|
||||
BasicDBList coursesList = new BasicDBList();
|
||||
coursesList.add("Chemistry");
|
||||
coursesList.add("Science");
|
||||
|
||||
DBObject student = new BasicDBObject().append("studentId", "STUD1")
|
||||
.append("name", "Jim")
|
||||
.append("age", 13)
|
||||
.append("courses", coursesList);
|
||||
|
||||
dbCollection.insert(student);
|
||||
|
||||
Bson filter = eq("studentId", "STUD2");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSingleStudentObjectWithStringArray_whenInsertingDocument_thenCheckingForDocument() {
|
||||
List<String> coursesList = new ArrayList<>();
|
||||
coursesList.add("Science");
|
||||
coursesList.add("Geography");
|
||||
|
||||
Document student = new Document().append("studentId", "STUD2")
|
||||
.append("name", "Sam")
|
||||
.append("age", 13)
|
||||
.append("courses", coursesList);
|
||||
|
||||
collection.insertOne(student);
|
||||
|
||||
Bson filter = eq("studentId", "STUD2");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleStudentObjectsWithStringArray_whenInsertingDocuments_thenCheckingForDocuments() {
|
||||
List<String> coursesList1 = new ArrayList<>();
|
||||
coursesList1.add("Chemistry");
|
||||
coursesList1.add("Geography");
|
||||
|
||||
Document student1 = new Document().append("studentId", "STUD3")
|
||||
.append("name", "Sarah")
|
||||
.append("age", 12)
|
||||
.append("courses", coursesList1);
|
||||
|
||||
List<String> coursesList2 = new ArrayList<>();
|
||||
coursesList2.add("Maths");
|
||||
coursesList2.add("History");
|
||||
|
||||
Document student2 = new Document().append("studentId", "STUD4")
|
||||
.append("name", "Tom")
|
||||
.append("age", 13)
|
||||
.append("courses", coursesList2);
|
||||
|
||||
List<Document> students = new ArrayList<>();
|
||||
students.add(student1);
|
||||
students.add(student2);
|
||||
|
||||
collection.insertMany(students);
|
||||
|
||||
Bson filter = in("studentId", "STUD3", "STUD4");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSingleStudentObjectWithObjectArray_whenInsertingDocument_thenCheckingForDocument() {
|
||||
Document course1 = new Document().append("name", "C1")
|
||||
.append("points", 5);
|
||||
|
||||
Document course2 = new Document().append("name", "C2")
|
||||
.append("points", 7);
|
||||
|
||||
List<Document> coursesList = new ArrayList<>();
|
||||
coursesList.add(course1);
|
||||
coursesList.add(course2);
|
||||
|
||||
Document student = new Document().append("studentId", "STUD5")
|
||||
.append("name", "Sam")
|
||||
.append("age", 13)
|
||||
.append("courses", coursesList);
|
||||
|
||||
collection.insertOne(student);
|
||||
|
||||
Bson filter = eq("studentId", "STUD5");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
}
|
@ -20,7 +20,8 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = SpringBoot.class)
|
||||
public class KeycloakConfigurationIntegrationTest {
|
||||
//requires running Keycloak server and realm setup as shown in https://www.baeldung.com/spring-boot-keycloak
|
||||
public class KeycloakConfigurationLiveTest {
|
||||
|
||||
@Spy
|
||||
private KeycloakSecurityContextClientRequestInterceptor factory;
|
@ -133,6 +133,7 @@
|
||||
<spring.data.version>2.4.5</spring.data.version>
|
||||
<jackson-databind.version>0.2.1</jackson-databind.version>
|
||||
<springfox.version>2.9.2</springfox.version>
|
||||
<start-class>com.baeldung.openapi.OpenApiApplication</start-class>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -1,9 +1,12 @@
|
||||
package com.baeldung.kong;
|
||||
|
||||
import org.jobrunr.autoconfigure.JobRunrAutoConfiguration;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAutoConfiguration(exclude = { JobRunrAutoConfiguration.class})
|
||||
public class StockApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -5,8 +5,6 @@ This module contains articles about Spring Web MVC in Spring Boot projects.
|
||||
### Relevant Articles:
|
||||
|
||||
- [Functional Controllers in Spring MVC](https://www.baeldung.com/spring-mvc-functional-controllers)
|
||||
- [Specify an Array of Strings as Body Parameters in Swagger](https://www.baeldung.com/swagger-body-array-of-strings)
|
||||
- [Swagger @ApiParam vs @ApiModelProperty](https://www.baeldung.com/swagger-apiparam-vs-apimodelproperty)
|
||||
- [Testing REST with multiple MIME types](https://www.baeldung.com/testing-rest-api-with-multiple-media-types)
|
||||
- [Testing Web APIs with Postman Collections](https://www.baeldung.com/postman-testing-collections)
|
||||
- [Spring Boot Consuming and Producing JSON](https://www.baeldung.com/spring-boot-json)
|
||||
|
@ -28,22 +28,6 @@
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!-- Swagger dependencies for REST documentation -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-spring-webmvc</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
@ -90,8 +74,7 @@
|
||||
|
||||
<properties>
|
||||
<spring.fox.version>3.0.0</spring.fox.version>
|
||||
<start-class>com.baeldung.swagger2boot.SpringBootSwaggerApplication</start-class>
|
||||
<!-- <start-class>com.baeldung.springbootmvc.SpringBootMvcFnApplication</start-class> -->
|
||||
<start-class>com.baeldung.springbootmvc.SpringBootMvcFnApplication</start-class>
|
||||
<xstream.version>1.4.11.1</xstream.version>
|
||||
</properties>
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Swagger: Specify Two Responses with the Same Response Code](https://www.baeldung.com/swagger-two-responses-one-response-code)
|
||||
- [Specify an Array of Strings as Body Parameters in Swagger](https://www.baeldung.com/swagger-body-array-of-strings)
|
||||
- [Swagger @ApiParam vs @ApiModelProperty](https://www.baeldung.com/swagger-apiparam-vs-apimodelproperty)
|
@ -39,6 +39,16 @@
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>${springfox.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-spring-webmvc</artifactId>
|
||||
<version>${springfox.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -63,6 +73,13 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<mainClass>${start-class}</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@ -70,6 +87,8 @@
|
||||
<springfox.version>3.0.0</springfox.version>
|
||||
<swagger-codegen-maven-plugin.version>3.0.34</swagger-codegen-maven-plugin.version>
|
||||
<springdoc.version>1.6.10</springdoc.version>
|
||||
<swagger-maven-plugin.version>3.1.1</swagger-maven-plugin.version>
|
||||
<start-class>com.baeldung.tworesponses.Application</start-class>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.swagger2boot;
|
||||
package com.baeldung.swagger2bootmvc;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
@ -1,7 +1,8 @@
|
||||
package com.baeldung.swagger2boot.config;
|
||||
package com.baeldung.swagger2bootmvc.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
@ -9,10 +10,12 @@ import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableSwagger2WebMvc
|
||||
@EnableWebMvc
|
||||
@EnableSwagger2
|
||||
public class Swagger2Config {
|
||||
@Bean
|
||||
public Docket api() {
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.swagger2boot.controller;
|
||||
package com.baeldung.swagger2bootmvc.controller;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
|
||||
|
||||
@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import com.baeldung.swagger2boot.model.Foo;
|
||||
import com.baeldung.swagger2bootmvc.model.Foo;
|
||||
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
@ -1,19 +1,17 @@
|
||||
package com.baeldung.swagger2boot.controller;
|
||||
package com.baeldung.swagger2bootmvc.controller;
|
||||
|
||||
import com.baeldung.swagger2boot.model.Foo;
|
||||
import com.baeldung.swagger2boot.model.User;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.websocket.server.PathParam;
|
||||
import com.baeldung.swagger2bootmvc.model.User;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
|
||||
@Controller
|
||||
public class UserController {
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.swagger2boot.model;
|
||||
package com.baeldung.swagger2bootmvc.model;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.swagger2boot.model;
|
||||
package com.baeldung.swagger2bootmvc.model;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@FeignClient(name = "product-client", url = "http://localhost:8081/product/", configuration = FeignConfig.class)
|
||||
@FeignClient(name = "product-client", url = "http://localhost:8084/product/", configuration = FeignConfig.class)
|
||||
public interface ProductClient {
|
||||
|
||||
@RequestMapping(value = "{id}", method = RequestMethod.GET)
|
||||
|
@ -27,9 +27,10 @@ public class ProductClientUnitTest {
|
||||
|
||||
@Before
|
||||
public void startWireMockServer() {
|
||||
wireMockServer = new WireMockServer(8081);
|
||||
configureFor("localhost", 8081);
|
||||
wireMockServer = new WireMockServer(8084);
|
||||
configureFor("localhost", 8084);
|
||||
wireMockServer.start();
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
|
@ -20,7 +20,6 @@ import static com.github.tomakehurst.wiremock.client.WireMock.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(ProductController.class)
|
||||
@ImportAutoConfiguration({FeignAutoConfiguration.class, TestControllerAdvice.class})
|
||||
@ -37,8 +36,8 @@ public class ProductControllerUnitTest {
|
||||
|
||||
@Before
|
||||
public void startWireMockServer() {
|
||||
wireMockServer = new WireMockServer(8081);
|
||||
configureFor("localhost", 8081);
|
||||
wireMockServer = new WireMockServer(8084);
|
||||
configureFor("localhost", 8084);
|
||||
wireMockServer.start();
|
||||
}
|
||||
|
||||
|
@ -7,15 +7,18 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
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.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension;
|
||||
import org.springframework.security.provisioning.JdbcUserDetailsManager;
|
||||
import org.springframework.security.provisioning.UserDetailsManager;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.relationships.security.AuthenticationSuccessHandlerImpl;
|
||||
@ -24,7 +27,7 @@ import com.baeldung.relationships.security.CustomUserDetailsService;
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@ComponentScan("com.baeldung.security")
|
||||
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
public class SpringSecurityConfig {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext applicationContext;
|
||||
@ -42,24 +45,28 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
userDetailsService = applicationContext.getBean(CustomUserDetailsService.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.userDetailsService(userDetailsService)
|
||||
@Bean
|
||||
public UserDetailsManager users(HttpSecurity http) throws Exception {
|
||||
AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManagerBuilder.class)
|
||||
.userDetailsService(userDetailsService)
|
||||
.passwordEncoder(encoder())
|
||||
.and()
|
||||
.authenticationProvider(authenticationProvider())
|
||||
.jdbcAuthentication()
|
||||
.dataSource(dataSource);
|
||||
.build();
|
||||
|
||||
JdbcUserDetailsManager jdbcUserDetailsManager = new JdbcUserDetailsManager(dataSource);
|
||||
jdbcUserDetailsManager.setAuthenticationManager(authenticationManager);
|
||||
return jdbcUserDetailsManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(WebSecurity web) {
|
||||
web.ignoring()
|
||||
@Bean
|
||||
public WebSecurityCustomizer webSecurityCustomizer() {
|
||||
return (web) -> web.ignoring()
|
||||
.antMatchers("/resources/**");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(final HttpSecurity http) throws Exception {
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/login")
|
||||
.permitAll()
|
||||
@ -70,6 +77,7 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
.and()
|
||||
.csrf()
|
||||
.disable();
|
||||
return http.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -4,16 +4,16 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
public class SecurityConfig {
|
||||
|
||||
@Override
|
||||
protected void configure(final HttpSecurity http) throws Exception {
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.csrf()
|
||||
.disable()
|
||||
.authorizeRequests()
|
||||
@ -22,6 +22,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
.and()
|
||||
.formLogin()
|
||||
.permitAll();
|
||||
return http.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -1,33 +1,50 @@
|
||||
package com.baeldung.roles.ip.config;
|
||||
|
||||
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.authentication.builders.AuthenticationManagerBuilder;
|
||||
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.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
public class SecurityConfig {
|
||||
|
||||
@Autowired
|
||||
private CustomIpAuthenticationProvider authenticationProvider;
|
||||
|
||||
@Override
|
||||
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.inMemoryAuthentication().withUser("john").password("{noop}123").authorities("ROLE_USER");
|
||||
// auth.authenticationProvider(authenticationProvider);
|
||||
@Bean
|
||||
public InMemoryUserDetailsManager userDetailsService(HttpSecurity http) throws Exception {
|
||||
UserDetails user = User.withUsername("john")
|
||||
.password("{noop}123")
|
||||
.authorities("ROLE_USER")
|
||||
.build();
|
||||
http.getSharedObject(AuthenticationManagerBuilder.class)
|
||||
.authenticationProvider(authenticationProvider)
|
||||
.build();
|
||||
return new InMemoryUserDetailsManager(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(final HttpSecurity http) throws Exception {
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/login").permitAll()
|
||||
// .antMatchers("/foos/**").hasIpAddress("11.11.11.11")
|
||||
.antMatchers("/foos/**").access("isAuthenticated() and hasIpAddress('11.11.11.11')")
|
||||
.anyRequest().authenticated()
|
||||
.and().formLogin().permitAll()
|
||||
.and().csrf().disable();
|
||||
.antMatchers("/login")
|
||||
.permitAll()
|
||||
.antMatchers("/foos/**")
|
||||
.access("isAuthenticated() and hasIpAddress('11.11.11.11')")
|
||||
.anyRequest()
|
||||
.authenticated()
|
||||
.and()
|
||||
.formLogin()
|
||||
.permitAll()
|
||||
.and()
|
||||
.csrf()
|
||||
.disable();
|
||||
return http.build();
|
||||
}
|
||||
}
|
@ -1,26 +1,28 @@
|
||||
package com.baeldung.roles.rolesauthorities.config;
|
||||
|
||||
import com.baeldung.roles.rolesauthorities.CustomAuthenticationProvider;
|
||||
import com.baeldung.roles.rolesauthorities.persistence.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
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.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
||||
|
||||
import com.baeldung.roles.rolesauthorities.CustomAuthenticationProvider;
|
||||
import com.baeldung.roles.rolesauthorities.persistence.UserRepository;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = {"com.baeldung.rolesauthorities"})
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
public class SecurityConfig {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
@ -31,27 +33,30 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
@Autowired
|
||||
private LogoutSuccessHandler myLogoutSuccessHandler;
|
||||
|
||||
@Override
|
||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.authenticationProvider(authProvider());
|
||||
@Bean
|
||||
public AuthenticationManager authManager(HttpSecurity http) throws Exception {
|
||||
return http.getSharedObject(AuthenticationManagerBuilder.class)
|
||||
.authenticationProvider(authProvider())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(WebSecurity web) throws Exception {
|
||||
web.ignoring()
|
||||
@Bean
|
||||
public WebSecurityCustomizer webSecurityCustomizer() {
|
||||
return (web) -> web.ignoring()
|
||||
.antMatchers("/resources/**");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
|
||||
http
|
||||
.csrf().disable()
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.csrf()
|
||||
.disable()
|
||||
.authorizeRequests()
|
||||
.antMatchers("/login*", "/logout*", "/protectedbynothing*", "/home*").permitAll()
|
||||
.antMatchers("/protectedbyrole").hasRole("USER")
|
||||
.antMatchers("/protectedbyauthority").hasAuthority("READ_PRIVILEGE")
|
||||
.antMatchers("/login*", "/logout*", "/protectedbynothing*", "/home*")
|
||||
.permitAll()
|
||||
.antMatchers("/protectedbyrole")
|
||||
.hasRole("USER")
|
||||
.antMatchers("/protectedbyauthority")
|
||||
.hasAuthority("READ_PRIVILEGE")
|
||||
.and()
|
||||
.formLogin()
|
||||
.loginPage("/login")
|
||||
@ -64,6 +69,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
.logoutSuccessUrl("/logout.html?logSucc=true")
|
||||
.deleteCookies("JSESSIONID")
|
||||
.permitAll();
|
||||
return http.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -1,5 +1,8 @@
|
||||
package com.baeldung.roles.voter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@ -11,17 +14,14 @@ import org.springframework.security.access.vote.UnanimousBased;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
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.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.access.expression.WebExpressionVoter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
public class WebSecurityConfig {
|
||||
|
||||
@Autowired
|
||||
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
||||
@ -36,10 +36,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
.roles("ADMIN");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.csrf()
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.csrf()
|
||||
.disable()
|
||||
.authorizeRequests()
|
||||
.anyRequest()
|
||||
@ -51,7 +50,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
.and()
|
||||
.logout()
|
||||
.permitAll()
|
||||
.deleteCookies("JSESSIONID").logoutSuccessUrl("/login");
|
||||
.deleteCookies("JSESSIONID")
|
||||
.logoutSuccessUrl("/login");
|
||||
return http.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -1,20 +1,21 @@
|
||||
package com.baeldung.filter;
|
||||
|
||||
import com.baeldung.security.RestAuthenticationEntryPoint;
|
||||
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.authentication.builders.AuthenticationManagerBuilder;
|
||||
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.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
|
||||
|
||||
import com.baeldung.security.RestAuthenticationEntryPoint;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
|
||||
public class CustomWebSecurityConfigurerAdapter {
|
||||
|
||||
@Autowired private RestAuthenticationEntryPoint authenticationEntryPoint;
|
||||
|
||||
@ -27,10 +28,9 @@ public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAda
|
||||
.authorities("ROLE_USER");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.authorizeRequests()
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/securityNone")
|
||||
.permitAll()
|
||||
.anyRequest()
|
||||
@ -38,8 +38,8 @@ public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAda
|
||||
.and()
|
||||
.httpBasic()
|
||||
.authenticationEntryPoint(authenticationEntryPoint);
|
||||
|
||||
http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class);
|
||||
return http.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -1,27 +1,30 @@
|
||||
package com.baeldung.inmemory;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
public class InMemoryAuthWebSecurityConfigurer extends WebSecurityConfigurerAdapter {
|
||||
public class InMemoryAuthWebSecurityConfigurer {
|
||||
|
||||
@Override
|
||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||
@Bean
|
||||
public InMemoryUserDetailsManager userDetailsService() {
|
||||
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
||||
auth.inMemoryAuthentication()
|
||||
.passwordEncoder(encoder)
|
||||
.withUser("spring")
|
||||
UserDetails user = User.withUsername("spring")
|
||||
.password(encoder.encode("secret"))
|
||||
.roles("USER");
|
||||
.roles("USER")
|
||||
.build();
|
||||
return new InMemoryUserDetailsManager(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/private/**")
|
||||
.authenticated()
|
||||
@ -29,6 +32,7 @@ public class InMemoryAuthWebSecurityConfigurer extends WebSecurityConfigurerAdap
|
||||
.permitAll()
|
||||
.and()
|
||||
.httpBasic();
|
||||
return http.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,23 +1,26 @@
|
||||
package com.baeldung.inmemory;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
//@Configuration
|
||||
public class InMemoryNoOpAuthWebSecurityConfigurer extends WebSecurityConfigurerAdapter {
|
||||
public class InMemoryNoOpAuthWebSecurityConfigurer {
|
||||
|
||||
@Override
|
||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.inMemoryAuthentication()
|
||||
.withUser("spring")
|
||||
@Bean
|
||||
public InMemoryUserDetailsManager userDetailsService() {
|
||||
UserDetails user = User.withUsername("spring")
|
||||
.password("{noop}secret")
|
||||
.roles("USER");
|
||||
.roles("USER")
|
||||
.build();
|
||||
return new InMemoryUserDetailsManager(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/private/**")
|
||||
.authenticated()
|
||||
@ -25,5 +28,6 @@ public class InMemoryNoOpAuthWebSecurityConfigurer extends WebSecurityConfigurer
|
||||
.permitAll()
|
||||
.and()
|
||||
.httpBasic();
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,14 @@
|
||||
package com.baeldung.passwordstorage;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
@ -14,18 +19,16 @@ import org.springframework.security.crypto.password.StandardPasswordEncoder;
|
||||
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
public class PasswordStorageWebSecurityConfigurer extends WebSecurityConfigurerAdapter {
|
||||
public class PasswordStorageWebSecurityConfigurer {
|
||||
|
||||
@Override
|
||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.eraseCredentials(false) // 4
|
||||
@Bean
|
||||
public AuthenticationManager authManager(HttpSecurity http) throws Exception {
|
||||
AuthenticationManagerBuilder authenticationManagerBuilder = http.getSharedObject(AuthenticationManagerBuilder.class);
|
||||
authenticationManagerBuilder.eraseCredentials(false)
|
||||
.userDetailsService(getUserDefaultDetailsService())
|
||||
.passwordEncoder(passwordEncoder());
|
||||
return authenticationManagerBuilder.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -1,16 +1,17 @@
|
||||
package com.baeldung.postman.basic;
|
||||
|
||||
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.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
public class PostmanBasicAuthConfig extends WebSecurityConfigurerAdapter {
|
||||
public class PostmanBasicAuthConfig {
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.csrf()
|
||||
.disable()
|
||||
.authorizeRequests()
|
||||
@ -18,6 +19,7 @@ public class PostmanBasicAuthConfig extends WebSecurityConfigurerAdapter {
|
||||
.authenticated()
|
||||
.and()
|
||||
.httpBasic();
|
||||
return http.build();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
|
@ -1,28 +1,31 @@
|
||||
package com.baeldung.security;
|
||||
|
||||
import com.baeldung.security.web.MySavedRequestAwareAuthenticationSuccessHandler;
|
||||
import com.baeldung.security.web.RestAuthenticationEntryPoint;
|
||||
import com.baeldung.web.error.CustomAccessDeniedHandler;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
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.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
import org.springframework.security.task.DelegatingSecurityContextAsyncTaskExecutor;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
|
||||
|
||||
import com.baeldung.security.web.MySavedRequestAwareAuthenticationSuccessHandler;
|
||||
import com.baeldung.security.web.RestAuthenticationEntryPoint;
|
||||
import com.baeldung.web.error.CustomAccessDeniedHandler;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
||||
@ComponentScan("com.baeldung.security")
|
||||
public class SecurityJavaConfig extends WebSecurityConfigurerAdapter {
|
||||
public class SecurityJavaConfig {
|
||||
|
||||
@Autowired
|
||||
private CustomAccessDeniedHandler accessDeniedHandler;
|
||||
@ -35,17 +38,23 @@ public class SecurityJavaConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
private SimpleUrlAuthenticationFailureHandler myFailureHandler = new SimpleUrlAuthenticationFailureHandler();
|
||||
|
||||
@Override
|
||||
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.inMemoryAuthentication()
|
||||
.withUser("admin").password(encoder().encode("adminPass")).roles("ADMIN")
|
||||
.and()
|
||||
.withUser("user").password(encoder().encode("userPass")).roles("USER");
|
||||
@Bean
|
||||
public InMemoryUserDetailsManager userDetailsService() {
|
||||
UserDetails admin = User.withUsername("admin")
|
||||
.password(encoder().encode("adminPass"))
|
||||
.roles("ADMIN")
|
||||
.build();
|
||||
UserDetails user = User.withUsername("user")
|
||||
.password(encoder().encode("userPass"))
|
||||
.roles("USER")
|
||||
.build();
|
||||
return new InMemoryUserDetailsManager(admin, user);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(final HttpSecurity http) throws Exception {
|
||||
http.csrf().disable()
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.csrf()
|
||||
.disable()
|
||||
.authorizeRequests()
|
||||
.and()
|
||||
.exceptionHandling()
|
||||
@ -53,11 +62,16 @@ public class SecurityJavaConfig extends WebSecurityConfigurerAdapter {
|
||||
.authenticationEntryPoint(restAuthenticationEntryPoint)
|
||||
.and()
|
||||
.authorizeRequests()
|
||||
.antMatchers("/api/csrfAttacker*").permitAll()
|
||||
.antMatchers("/api/customer/**").permitAll()
|
||||
.antMatchers("/api/foos/**").authenticated()
|
||||
.antMatchers("/api/async/**").permitAll()
|
||||
.antMatchers("/api/admin/**").hasRole("ADMIN")
|
||||
.antMatchers("/api/csrfAttacker*")
|
||||
.permitAll()
|
||||
.antMatchers("/api/customer/**")
|
||||
.permitAll()
|
||||
.antMatchers("/api/foos/**")
|
||||
.authenticated()
|
||||
.antMatchers("/api/async/**")
|
||||
.permitAll()
|
||||
.antMatchers("/api/admin/**")
|
||||
.hasRole("ADMIN")
|
||||
.and()
|
||||
.formLogin()
|
||||
.successHandler(mySuccessHandler)
|
||||
@ -66,6 +80,7 @@ public class SecurityJavaConfig extends WebSecurityConfigurerAdapter {
|
||||
.httpBasic()
|
||||
.and()
|
||||
.logout();
|
||||
return http.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -14,4 +14,5 @@ The "REST With Spring" Classes: https://bit.ly/restwithspring
|
||||
- [Spring @RequestParam Annotation](https://www.baeldung.com/spring-request-param)
|
||||
- [Spring @RequestParam vs @PathVariable Annotations](https://www.baeldung.com/spring-requestparam-vs-pathvariable)
|
||||
- [@RequestMapping Value in Properties File](https://www.baeldung.com/spring-requestmapping-properties-file)
|
||||
- [Map a JSON POST to Multiple Spring MVC Parameters](https://www.baeldung.com/spring-mvc-json-param-mapping)
|
||||
- More articles: [[<-- prev]](../spring-mvc-basics-4)
|
||||
|
Loading…
x
Reference in New Issue
Block a user