Merge branch 'eugenp:master' into PR-7386
This commit is contained in:
commit
203cf00c71
|
@ -19,10 +19,29 @@
|
||||||
<artifactId>validation-api</artifactId>
|
<artifactId>validation-api</artifactId>
|
||||||
<version>${javax.validation.validation-api.version}</version>
|
<version>${javax.validation.validation-api.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.camel</groupId>
|
||||||
|
<artifactId>camel-core</artifactId>
|
||||||
|
<version>${camel.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.camel</groupId>
|
||||||
|
<artifactId>camel-test-junit5</artifactId>
|
||||||
|
<version>${camel.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.camel</groupId>
|
||||||
|
<artifactId>camel-main</artifactId>
|
||||||
|
<version>${camel.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<javax.validation.validation-api.version>2.0.1.Final</javax.validation.validation-api.version>
|
<javax.validation.validation-api.version>2.0.1.Final</javax.validation.validation-api.version>
|
||||||
|
<camel.version>4.3.0</camel.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.baeldung.dynamicrouter;
|
||||||
|
|
||||||
|
import org.apache.camel.ExchangeProperties;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class DynamicRouterBean {
|
||||||
|
public String route(String body, @ExchangeProperties Map<String, Object> properties) {
|
||||||
|
int invoked = (int) properties.getOrDefault("invoked", 0) + 1;
|
||||||
|
|
||||||
|
properties.put("invoked", invoked);
|
||||||
|
|
||||||
|
if (invoked == 1) {
|
||||||
|
switch (body.toLowerCase()) {
|
||||||
|
case "mock":
|
||||||
|
return "mock:dynamicRouter";
|
||||||
|
case "direct":
|
||||||
|
return "mock:directDynamicRouter";
|
||||||
|
case "seda":
|
||||||
|
return "mock:sedaDynamicRouter";
|
||||||
|
case "file":
|
||||||
|
return "mock:fileDynamicRouter";
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.dynamicrouter;
|
||||||
|
|
||||||
|
import org.apache.camel.builder.RouteBuilder;
|
||||||
|
|
||||||
|
public class DynamicRouterRoute extends RouteBuilder {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configure() {
|
||||||
|
|
||||||
|
from("direct:dynamicRouter").dynamicRouter(method(DynamicRouterBean.class, "route"));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,61 @@
|
||||||
|
package dynamicrouter;
|
||||||
|
|
||||||
|
import com.baeldung.dynamicrouter.DynamicRouterRoute;
|
||||||
|
import org.apache.camel.RoutesBuilder;
|
||||||
|
import org.apache.camel.component.mock.MockEndpoint;
|
||||||
|
import org.apache.camel.test.junit5.CamelTestSupport;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class DynamicRouterRouteUnitTest extends CamelTestSupport {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected RoutesBuilder createRouteBuilder() {
|
||||||
|
return new DynamicRouterRoute();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenDynamicRouter_whenMockEndpointExpectedMessageCountOneAndMockAsMessageBody_thenMessageSentToDynamicRouter() throws InterruptedException {
|
||||||
|
|
||||||
|
MockEndpoint mockDynamicEndpoint = getMockEndpoint("mock:dynamicRouter");
|
||||||
|
mockDynamicEndpoint.expectedMessageCount(1);
|
||||||
|
|
||||||
|
template.send("direct:dynamicRouter", exchange -> exchange.getIn()
|
||||||
|
.setBody("mock"));
|
||||||
|
MockEndpoint.assertIsSatisfied(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenDynamicRouter_whenMockEndpointExpectedMessageCountOneAndDirectAsMessageBody_thenMessageSentToDynamicRouter() throws InterruptedException {
|
||||||
|
|
||||||
|
MockEndpoint mockDynamicEndpoint = context.getEndpoint("mock:directDynamicRouter", MockEndpoint.class);
|
||||||
|
mockDynamicEndpoint.expectedMessageCount(1);
|
||||||
|
|
||||||
|
template.send("direct:dynamicRouter", exchange -> exchange.getIn()
|
||||||
|
.setBody("direct"));
|
||||||
|
|
||||||
|
MockEndpoint.assertIsSatisfied(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenDynamicRouter_whenMockEndpointExpectedMessageCountOneAndSedaAsMessageBody_thenMessageSentToDynamicRouter() throws InterruptedException {
|
||||||
|
|
||||||
|
MockEndpoint mockDynamicEndpoint = context.getEndpoint("mock:sedaDynamicRouter", MockEndpoint.class);
|
||||||
|
mockDynamicEndpoint.expectedMessageCount(1);
|
||||||
|
|
||||||
|
template.send("direct:dynamicRouter", exchange -> exchange.getIn()
|
||||||
|
.setBody("seda"));
|
||||||
|
MockEndpoint.assertIsSatisfied(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenDynamicRouter_whenMockEndpointExpectedMessageCountOneAndBookAsMessageBody_thenMessageSentToDynamicRouter() throws InterruptedException {
|
||||||
|
|
||||||
|
MockEndpoint mockDynamicEndpoint = getMockEndpoint("mock:fileDynamicRouter");
|
||||||
|
mockDynamicEndpoint.expectedMessageCount(1);
|
||||||
|
|
||||||
|
template.send("direct:dynamicRouter", exchange -> exchange.getIn()
|
||||||
|
.setBody("file"));
|
||||||
|
MockEndpoint.assertIsSatisfied(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -48,6 +48,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.activemq</groupId>
|
<groupId>org.apache.activemq</groupId>
|
||||||
<artifactId>artemis-jms-server</artifactId>
|
<artifactId>artemis-jms-server</artifactId>
|
||||||
|
<version>${activemq.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
@ -65,4 +66,8 @@
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<activemq.version>2.32.0</activemq.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
2
pom.xml
2
pom.xml
|
@ -1169,7 +1169,7 @@
|
||||||
<hamcrest.version>2.2</hamcrest.version>
|
<hamcrest.version>2.2</hamcrest.version>
|
||||||
<hamcrest-all.version>1.3</hamcrest-all.version>
|
<hamcrest-all.version>1.3</hamcrest-all.version>
|
||||||
<mockito.version>4.4.0</mockito.version>
|
<mockito.version>4.4.0</mockito.version>
|
||||||
<byte-buddy.version>1.14.6</byte-buddy.version>
|
<byte-buddy.version>1.14.11</byte-buddy.version>
|
||||||
|
|
||||||
<!-- logging -->
|
<!-- logging -->
|
||||||
<!-- overwriting in the slf4j and logback in the hibernate-jpa, spring-data-eclipselink. When updated to the latest version remove the version from that module-->
|
<!-- overwriting in the slf4j and logback in the hibernate-jpa, spring-data-eclipselink. When updated to the latest version remove the version from that module-->
|
||||||
|
|
|
@ -10,9 +10,9 @@ public class GreetingServiceWithoutAOP {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(GreetingServiceWithoutAOP.class);
|
private static final Logger logger = LoggerFactory.getLogger(GreetingServiceWithoutAOP.class);
|
||||||
|
|
||||||
public String greet(String name) {
|
public String greet(String name) {
|
||||||
logger.info(">> greet() - {}", name);
|
logger.debug(">> greet() - {}", name);
|
||||||
String result = String.format("Hello %s", name);
|
String result = String.format("Hello %s", name);
|
||||||
logger.info("<< greet() - {}", result);
|
logger.debug("<< greet() - {}", result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,13 +28,13 @@ public class LoggingAspect {
|
||||||
public void logBefore(JoinPoint joinPoint) {
|
public void logBefore(JoinPoint joinPoint) {
|
||||||
Object[] args = joinPoint.getArgs();
|
Object[] args = joinPoint.getArgs();
|
||||||
String methodName = joinPoint.getSignature().getName();
|
String methodName = joinPoint.getSignature().getName();
|
||||||
logger.info(">> {}() - {}", methodName, Arrays.toString(args));
|
logger.debug(">> {}() - {}", methodName, Arrays.toString(args));
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterReturning(value = "publicMethodsFromLoggingPackage()", returning = "result")
|
@AfterReturning(value = "publicMethodsFromLoggingPackage()", returning = "result")
|
||||||
public void logAfter(JoinPoint joinPoint, Object result) {
|
public void logAfter(JoinPoint joinPoint, Object result) {
|
||||||
String methodName = joinPoint.getSignature().getName();
|
String methodName = joinPoint.getSignature().getName();
|
||||||
logger.info("<< {}() - {}", methodName, result);
|
logger.debug("<< {}() - {}", methodName, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterThrowing(pointcut = "publicMethodsFromLoggingPackage()", throwing = "exception")
|
@AfterThrowing(pointcut = "publicMethodsFromLoggingPackage()", throwing = "exception")
|
||||||
|
@ -47,9 +47,9 @@ public class LoggingAspect {
|
||||||
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||||
Object[] args = joinPoint.getArgs();
|
Object[] args = joinPoint.getArgs();
|
||||||
String methodName = joinPoint.getSignature().getName();
|
String methodName = joinPoint.getSignature().getName();
|
||||||
logger.info(">> {}() - {}", methodName, Arrays.toString(args));
|
logger.debug(">> {}() - {}", methodName, Arrays.toString(args));
|
||||||
Object result = joinPoint.proceed();
|
Object result = joinPoint.proceed();
|
||||||
logger.info("<< {}() - {}", methodName, result);
|
logger.debug("<< {}() - {}", methodName, result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,18 @@
|
||||||
<groupId>io.undertow</groupId>
|
<groupId>io.undertow</groupId>
|
||||||
<artifactId>undertow-servlet</artifactId>
|
<artifactId>undertow-servlet</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wildfly</groupId>
|
||||||
|
<artifactId>wildfly-naming-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jboss</groupId>
|
||||||
|
<artifactId>jboss-ejb-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wildfly.common</groupId>
|
||||||
|
<artifactId>wildfly-common</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -21,7 +21,7 @@ public class SpringEjbClientApplication {
|
||||||
Properties jndiProps = new Properties();
|
Properties jndiProps = new Properties();
|
||||||
jndiProps.put("java.naming.factory.initial", "org.jboss.naming.remote.client.InitialContextFactory");
|
jndiProps.put("java.naming.factory.initial", "org.jboss.naming.remote.client.InitialContextFactory");
|
||||||
jndiProps.put("jboss.naming.client.ejb.context", true);
|
jndiProps.put("jboss.naming.client.ejb.context", true);
|
||||||
jndiProps.put("java.naming.provider.url", "http-remoting://localhost:8080");
|
jndiProps.put("java.naming.provider.url", "remote+http://localhost:8080");
|
||||||
return new InitialContext(jndiProps);
|
return new InitialContext(jndiProps);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ public class SpringEjbClientApplication {
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
private String getFullName(Class classType) {
|
private String getFullName(Class classType) {
|
||||||
String moduleName = "spring-ejb-remote/";
|
String moduleName = "ejb:/spring-ejb-remote/";
|
||||||
String beanName = classType.getSimpleName();
|
String beanName = classType.getSimpleName();
|
||||||
String viewClassName = classType.getName();
|
String viewClassName = classType.getName();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
package com.baeldung.spring.kafka.kafkaexception;
|
||||||
|
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import javax.management.MBeanServer;
|
||||||
|
import javax.management.ObjectName;
|
||||||
|
|
||||||
|
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||||
|
import org.apache.kafka.common.serialization.StringSerializer;
|
||||||
|
|
||||||
|
public class HandleInstanceAlreadyExistsException {
|
||||||
|
|
||||||
|
public static void generateUniqueClientIDUsingUUIDRandom() {
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put("bootstrap.servers", "localhost:9092");
|
||||||
|
props.put("key.serializer", StringSerializer.class);
|
||||||
|
props.put("value.serializer", StringSerializer.class);
|
||||||
|
|
||||||
|
String clientId = "my-producer-" + UUID.randomUUID();
|
||||||
|
props.setProperty("client.id", clientId);
|
||||||
|
KafkaProducer<String, String> producer1 = new KafkaProducer<>(props);
|
||||||
|
|
||||||
|
clientId = "my-producer-" + UUID.randomUUID();
|
||||||
|
props.setProperty("client.id", clientId);
|
||||||
|
KafkaProducer<String, String> producer2 = new KafkaProducer<>(props);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void closeProducerProperlyBeforeReinstantiate() {
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put("bootstrap.servers", "localhost:9092");
|
||||||
|
props.put("client.id", "my-producer");
|
||||||
|
props.put("key.serializer", StringSerializer.class);
|
||||||
|
props.put("value.serializer", StringSerializer.class);
|
||||||
|
|
||||||
|
KafkaProducer<String, String> producer1 = new KafkaProducer<>(props);
|
||||||
|
producer1.close();
|
||||||
|
|
||||||
|
producer1 = new KafkaProducer<>(props);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void useUniqueObjectName() throws Exception {
|
||||||
|
MBeanServer mBeanServer1 = ManagementFactory.getPlatformMBeanServer();
|
||||||
|
MBeanServer mBeanServer2 = ManagementFactory.getPlatformMBeanServer();
|
||||||
|
|
||||||
|
ObjectName objectName1 = new ObjectName("kafka.server:type=KafkaMetrics,id=metric1");
|
||||||
|
ObjectName objectName2 = new ObjectName("kafka.server:type=KafkaMetrics,id=metric2");
|
||||||
|
|
||||||
|
MyMBean mBean1 = new MyMBean();
|
||||||
|
mBeanServer1.registerMBean(mBean1, objectName1);
|
||||||
|
|
||||||
|
MyMBean mBean2 = new MyMBean();
|
||||||
|
mBeanServer2.registerMBean(mBean2, objectName2);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.baeldung.spring.kafka.kafkaexception;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class KafkaAppMain {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(KafkaAppMain.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,123 @@
|
||||||
|
package com.baeldung.spring.kafka.kafkaexception;
|
||||||
|
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import javax.management.Attribute;
|
||||||
|
import javax.management.AttributeList;
|
||||||
|
import javax.management.AttributeNotFoundException;
|
||||||
|
import javax.management.DynamicMBean;
|
||||||
|
import javax.management.InvalidAttributeValueException;
|
||||||
|
import javax.management.MBeanAttributeInfo;
|
||||||
|
import javax.management.MBeanConstructorInfo;
|
||||||
|
import javax.management.MBeanException;
|
||||||
|
import javax.management.MBeanInfo;
|
||||||
|
import javax.management.MBeanNotificationInfo;
|
||||||
|
import javax.management.MBeanOperationInfo;
|
||||||
|
import javax.management.MBeanServer;
|
||||||
|
import javax.management.ObjectName;
|
||||||
|
import javax.management.ReflectionException;
|
||||||
|
|
||||||
|
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
||||||
|
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||||
|
import org.apache.kafka.common.serialization.StringDeserializer;
|
||||||
|
import org.apache.kafka.common.serialization.StringSerializer;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class SimulateInstanceAlreadyExistsException {
|
||||||
|
|
||||||
|
public static void jmxRegistrationConflicts() throws Exception {
|
||||||
|
// Create two instances of MBeanServer
|
||||||
|
MBeanServer mBeanServer1 = ManagementFactory.getPlatformMBeanServer();
|
||||||
|
MBeanServer mBeanServer2 = ManagementFactory.getPlatformMBeanServer();
|
||||||
|
|
||||||
|
// Define the same ObjectName for both MBeans
|
||||||
|
ObjectName objectName = new ObjectName("kafka.server:type=KafkaMetrics");
|
||||||
|
|
||||||
|
// Create and register the first MBean
|
||||||
|
MyMBean mBean1 = new MyMBean();
|
||||||
|
mBeanServer1.registerMBean(mBean1, objectName);
|
||||||
|
|
||||||
|
// Attempt to register the second MBean with the same ObjectName
|
||||||
|
MyMBean mBean2 = new MyMBean();
|
||||||
|
mBeanServer2.registerMBean(mBean2, objectName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void duplicateConsumerClientID() {
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put("bootstrap.servers", "localhost:9092");
|
||||||
|
props.put("client.id", "my-consumer");
|
||||||
|
props.put("group.id", "test-group");
|
||||||
|
props.put("key.deserializer", StringDeserializer.class);
|
||||||
|
props.put("value.deserializer", StringDeserializer.class);
|
||||||
|
|
||||||
|
// Simulating concurrent client creation by multiple threads
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
new Thread(() -> {
|
||||||
|
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void duplicateProducerClientID() throws Exception {
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put("bootstrap.servers", "localhost:9092");
|
||||||
|
props.put("client.id", "my-producer");
|
||||||
|
props.put("key.serializer", StringSerializer.class);
|
||||||
|
props.put("value.serializer", StringSerializer.class);
|
||||||
|
|
||||||
|
KafkaProducer<String, String> producer1 = new KafkaProducer<>(props);
|
||||||
|
// Attempting to create another producer using same client.id
|
||||||
|
KafkaProducer<String, String> producer2 = new KafkaProducer<>(props);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void unclosedProducerAndReinitialize() {
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put("bootstrap.servers", "localhost:9092");
|
||||||
|
props.put("client.id", "my-producer");
|
||||||
|
props.put("key.serializer", StringSerializer.class);
|
||||||
|
props.put("value.serializer", StringSerializer.class);
|
||||||
|
|
||||||
|
KafkaProducer<String, String> producer1 = new KafkaProducer<>(props);
|
||||||
|
// Attempting to reinitialize without proper close
|
||||||
|
producer1 = new KafkaProducer<>(props);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class MyMBean implements DynamicMBean {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AttributeList getAttributes(String[] attributes) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AttributeList setAttributes(AttributeList attributes) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MBeanInfo getMBeanInfo() {
|
||||||
|
MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[0];
|
||||||
|
MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[0];
|
||||||
|
MBeanOperationInfo[] operations = new MBeanOperationInfo[0];
|
||||||
|
MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[0];
|
||||||
|
return new MBeanInfo(MyMBean.class.getName(), "My MBean", attributes, constructors, operations, notifications);
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,8 +10,9 @@
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>spring-security-modules</artifactId>
|
<artifactId>parent-boot-3</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-boot-3</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -29,7 +30,7 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.thymeleaf.extras</groupId>
|
<groupId>org.thymeleaf.extras</groupId>
|
||||||
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
|
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
|
@ -56,4 +57,7 @@
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<start-class>com.baeldung.manuallogout.ManualLogoutApplication</start-class>
|
||||||
|
</properties>
|
||||||
</project>
|
</project>
|
|
@ -1,20 +1,20 @@
|
||||||
package com.baeldung.logoutredirects.securityconfig;
|
package com.baeldung.logoutredirects.securityconfig;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
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.EnableWebSecurity;
|
||||||
import org.springframework.security.web.SecurityFilterChain;
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
public class SpringSecurityConfig {
|
public class SpringSecurityConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||||
http.authorizeRequests(authz -> authz.mvcMatchers("/login")
|
http.authorizeHttpRequests(authz -> authz.requestMatchers("/login")
|
||||||
.permitAll()
|
.permitAll()
|
||||||
.anyRequest()
|
.anyRequest()
|
||||||
.authenticated())
|
.authenticated())
|
||||||
|
|
|
@ -5,9 +5,6 @@ import static org.springframework.security.web.header.writers.ClearSiteDataHeade
|
||||||
import static org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter.Directive.EXECUTION_CONTEXTS;
|
import static org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter.Directive.EXECUTION_CONTEXTS;
|
||||||
import static org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter.Directive.STORAGE;
|
import static org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter.Directive.STORAGE;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.http.Cookie;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
@ -20,11 +17,14 @@ import org.springframework.security.web.authentication.logout.HeaderWriterLogout
|
||||||
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
|
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
|
||||||
import org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter;
|
import org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter;
|
||||||
|
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.Cookie;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
public class SimpleSecurityConfiguration {
|
public class SimpleSecurityConfiguration {
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(SimpleSecurityConfiguration.class);
|
private static final Logger logger = LoggerFactory.getLogger(SimpleSecurityConfiguration.class);
|
||||||
|
|
||||||
@Order(4)
|
@Order(4)
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@ -32,8 +32,8 @@ public class SimpleSecurityConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain filterChainLogoutOnRequest(HttpSecurity http) throws Exception {
|
public SecurityFilterChain filterChainLogoutOnRequest(HttpSecurity http) throws Exception {
|
||||||
http.antMatcher("/request/**")
|
http.securityMatcher("/request/**")
|
||||||
.authorizeRequests(authz -> authz.anyRequest()
|
.authorizeHttpRequests(authz -> authz.anyRequest()
|
||||||
.permitAll())
|
.permitAll())
|
||||||
.logout(logout -> logout.logoutUrl("/request/logout")
|
.logout(logout -> logout.logoutUrl("/request/logout")
|
||||||
.addLogoutHandler((request, response, auth) -> {
|
.addLogoutHandler((request, response, auth) -> {
|
||||||
|
@ -53,8 +53,8 @@ public class SimpleSecurityConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain filterChainDefaultLogout(HttpSecurity http) throws Exception {
|
public SecurityFilterChain filterChainDefaultLogout(HttpSecurity http) throws Exception {
|
||||||
http.antMatcher("/basic/**")
|
http.securityMatcher("/basic/**")
|
||||||
.authorizeRequests(authz -> authz.anyRequest()
|
.authorizeHttpRequests(authz -> authz.anyRequest()
|
||||||
.permitAll())
|
.permitAll())
|
||||||
.logout(logout -> logout.logoutUrl("/basic/basiclogout"));
|
.logout(logout -> logout.logoutUrl("/basic/basiclogout"));
|
||||||
return http.build();
|
return http.build();
|
||||||
|
@ -67,8 +67,8 @@ public class SimpleSecurityConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain filterChainAllCookieClearing(HttpSecurity http) throws Exception {
|
public SecurityFilterChain filterChainAllCookieClearing(HttpSecurity http) throws Exception {
|
||||||
http.antMatcher("/cookies/**")
|
http.securityMatcher("/cookies/**")
|
||||||
.authorizeRequests(authz -> authz.anyRequest()
|
.authorizeHttpRequests(authz -> authz.anyRequest()
|
||||||
.permitAll())
|
.permitAll())
|
||||||
.logout(logout -> logout.logoutUrl("/cookies/cookielogout")
|
.logout(logout -> logout.logoutUrl("/cookies/cookielogout")
|
||||||
.addLogoutHandler(new SecurityContextLogoutHandler())
|
.addLogoutHandler(new SecurityContextLogoutHandler())
|
||||||
|
@ -92,8 +92,8 @@ public class SimpleSecurityConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain filterChainClearSiteDataHeader(HttpSecurity http) throws Exception {
|
public SecurityFilterChain filterChainClearSiteDataHeader(HttpSecurity http) throws Exception {
|
||||||
http.antMatcher("/csd/**")
|
http.securityMatcher("/csd/**")
|
||||||
.authorizeRequests(authz -> authz.anyRequest()
|
.authorizeHttpRequests(authz -> authz.anyRequest()
|
||||||
.permitAll())
|
.permitAll())
|
||||||
.logout(logout -> logout.logoutUrl("/csd/csdlogout")
|
.logout(logout -> logout.logoutUrl("/csd/csdlogout")
|
||||||
.addLogoutHandler(new HeaderWriterLogoutHandler(new ClearSiteDataHeaderWriter(SOURCE))));
|
.addLogoutHandler(new HeaderWriterLogoutHandler(new ClearSiteDataHeaderWriter(SOURCE))));
|
||||||
|
|
|
@ -9,9 +9,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
import javax.servlet.http.Cookie;
|
|
||||||
import javax.servlet.http.HttpSession;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -22,6 +19,9 @@ import org.springframework.security.test.context.support.WithMockUser;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.Cookie;
|
||||||
|
import jakarta.servlet.http.HttpSession;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@WebMvcTest(SimpleSecurityConfiguration.class)
|
@WebMvcTest(SimpleSecurityConfiguration.class)
|
||||||
public class ManualLogoutIntegrationTest {
|
public class ManualLogoutIntegrationTest {
|
||||||
|
|
Loading…
Reference in New Issue