From 145aada75376e30b9d25beb50659ecf443160c53 Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Sat, 9 Nov 2024 15:26:07 +0800 Subject: [PATCH 1/6] support loop event pusher --- example/example-application/pom.xml | 2 +- .../codingapi/example/handler/CHandler.java | 4 ++- example/example-domain/pom.xml | 2 +- example/example-infra-flow/pom.xml | 2 +- example/example-infra-jpa/pom.xml | 2 +- example/example-server/pom.xml | 2 +- example/pom.xml | 2 +- pom.xml | 2 +- springboot-starter-data-fast/pom.xml | 2 +- springboot-starter-flow/pom.xml | 2 +- springboot-starter-security/pom.xml | 2 +- springboot-starter/pom.xml | 2 +- .../framework/event/DomainEventContext.java | 14 ++++++---- .../framework/event/EventPusher.java | 18 ++++++++++++- .../framework/event/EventTraceContext.java | 26 +++++++++++++++++++ 15 files changed, 66 insertions(+), 18 deletions(-) diff --git a/example/example-application/pom.xml b/example/example-application/pom.xml index c61d2469..fe05448b 100644 --- a/example/example-application/pom.xml +++ b/example/example-application/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.6 + 3.3.7 4.0.0 diff --git a/example/example-application/src/main/java/com/codingapi/example/handler/CHandler.java b/example/example-application/src/main/java/com/codingapi/example/handler/CHandler.java index b1d2cba3..34b02124 100644 --- a/example/example-application/src/main/java/com/codingapi/example/handler/CHandler.java +++ b/example/example-application/src/main/java/com/codingapi/example/handler/CHandler.java @@ -1,6 +1,8 @@ package com.codingapi.example.handler; +import com.codingapi.example.event.AEvent; import com.codingapi.example.event.CEvent; +import com.codingapi.springboot.framework.event.EventPusher; import com.codingapi.springboot.framework.event.EventTraceContext; import com.codingapi.springboot.framework.event.IHandler; import lombok.extern.slf4j.Slf4j; @@ -14,7 +16,7 @@ public class CHandler implements IHandler { public void handler(CEvent event) { log.info("c event:{},eventKey:{}", event, EventTraceContext.getInstance().getEventKey()); -// EventPusher.push(new AEvent()); +// EventPusher.push(new AEvent(),true); // throw new RuntimeException("c handler error"); } diff --git a/example/example-domain/pom.xml b/example/example-domain/pom.xml index 3a437d43..e2a877b3 100644 --- a/example/example-domain/pom.xml +++ b/example/example-domain/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.6 + 3.3.7 4.0.0 diff --git a/example/example-infra-flow/pom.xml b/example/example-infra-flow/pom.xml index 4ede4627..9af0072a 100644 --- a/example/example-infra-flow/pom.xml +++ b/example/example-infra-flow/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.6 + 3.3.7 4.0.0 diff --git a/example/example-infra-jpa/pom.xml b/example/example-infra-jpa/pom.xml index 9809444d..b0b4dba0 100644 --- a/example/example-infra-jpa/pom.xml +++ b/example/example-infra-jpa/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.6 + 3.3.7 4.0.0 diff --git a/example/example-server/pom.xml b/example/example-server/pom.xml index 71ec3c93..38f32cf3 100644 --- a/example/example-server/pom.xml +++ b/example/example-server/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.6 + 3.3.7 4.0.0 diff --git a/example/pom.xml b/example/pom.xml index 7c000f17..cc90de3f 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -17,7 +17,7 @@ springboot-example - 3.3.6 + 3.3.7 springboot-example springboot-example project for Spring Boot diff --git a/pom.xml b/pom.xml index 59cb5b08..3b9d38da 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ com.codingapi.springboot springboot-parent - 3.3.6 + 3.3.7 https://github.com/codingapi/springboot-framewrok springboot-parent diff --git a/springboot-starter-data-fast/pom.xml b/springboot-starter-data-fast/pom.xml index 537cc3ec..c8f0cc2c 100644 --- a/springboot-starter-data-fast/pom.xml +++ b/springboot-starter-data-fast/pom.xml @@ -5,7 +5,7 @@ springboot-parent com.codingapi.springboot - 3.3.6 + 3.3.7 4.0.0 diff --git a/springboot-starter-flow/pom.xml b/springboot-starter-flow/pom.xml index 0fedb300..6fc8fe79 100644 --- a/springboot-starter-flow/pom.xml +++ b/springboot-starter-flow/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 3.3.6 + 3.3.7 springboot-starter-flow diff --git a/springboot-starter-security/pom.xml b/springboot-starter-security/pom.xml index 7224f6f0..c06f3cca 100644 --- a/springboot-starter-security/pom.xml +++ b/springboot-starter-security/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 3.3.6 + 3.3.7 springboot-starter-security diff --git a/springboot-starter/pom.xml b/springboot-starter/pom.xml index 61ea65e9..28d7733c 100644 --- a/springboot-starter/pom.xml +++ b/springboot-starter/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot springboot-parent - 3.3.6 + 3.3.7 springboot-starter diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/DomainEventContext.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/DomainEventContext.java index a22799e2..7d10e38c 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/DomainEventContext.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/DomainEventContext.java @@ -23,9 +23,13 @@ public static DomainEventContext getInstance() { return instance; } - private void push(IEvent event, boolean sync) { + private void push(IEvent event, boolean sync,boolean hasLoopEvent) { if (context != null) { String traceId = EventTraceContext.getInstance().getOrCreateTrace(); + if(hasLoopEvent){ + EventTraceContext.getInstance().clearTrace(); + traceId = EventTraceContext.getInstance().getOrCreateTrace(); + } EventTraceContext.getInstance().addEvent(traceId,event); context.publishEvent(new DomainEvent(event, sync,traceId)); } @@ -36,13 +40,13 @@ private void push(IEvent event, boolean sync) { * @see EventPusher * 默认 同步事件 */ - public void push(IEvent event) { + public void push(IEvent event,boolean hasLoopEvent) { if (event instanceof IAsyncEvent) { - this.push(event, false); + this.push(event, false,hasLoopEvent); } else if (event instanceof ISyncEvent) { - this.push(event, true); + this.push(event, true,hasLoopEvent); } else { - this.push(event, true); + this.push(event, true,hasLoopEvent); } } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/EventPusher.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/EventPusher.java index ee90ddd8..333569ea 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/EventPusher.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/EventPusher.java @@ -5,7 +5,23 @@ */ public class EventPusher { + /** + * 推送事件 + * 默认将自动检测事件是否有循环事件,当出现循环事件时,系统将会抛出循环调用异常。 + * @param event 事件 + */ public static void push(IEvent event) { - DomainEventContext.getInstance().push(event); + push(event, false); + } + + /** + * 推送事件 + * 默认将自动检测事件是否有循环事件,当出现循环事件时,系统将会抛出循环调用异常。 + * 设置hasLoopEvent为true,将不会检测循环事件。 + * @param event 事件 + * @param hasLoopEvent 是否有循环事件 + */ + public static void push(IEvent event, boolean hasLoopEvent) { + DomainEventContext.getInstance().push(event, hasLoopEvent); } } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/EventTraceContext.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/EventTraceContext.java index 844d2cf9..562e2768 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/EventTraceContext.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/EventTraceContext.java @@ -45,12 +45,19 @@ public String getEventKey() { return threadLocal.get(); } + /** + * create event key + * @param traceId traceId + */ void createEventKey(String traceId) { String eventKey = traceId + "#" + RandomGenerator.randomString(8); eventKeyState.put(eventKey, false); threadLocal.set(eventKey); } + /** + * check event state + */ void checkEventState() { String eventKey = threadLocal.get(); if (eventKey != null) { @@ -66,6 +73,11 @@ void checkEventState() { threadLocal.remove(); } + /** + * add event + * @param traceId traceId + * @param event event + */ void addEvent(String traceId, IEvent event) { boolean hasEventLoop = EventStackContext.getInstance().checkEventLoop(traceId, event); if (hasEventLoop) { @@ -78,4 +90,18 @@ void addEvent(String traceId, IEvent event) { } EventStackContext.getInstance().addEvent(traceId, event); } + + /** + * clear trace + */ + public void clearTrace() { + String eventKey = threadLocal.get(); + if (eventKey != null) { + String traceId = eventKey.split("#")[0]; + traceKeys.remove(traceId); + EventStackContext.getInstance().remove(traceId); + eventKeyState.remove(eventKey); + threadLocal.remove(); + } + } } From ce3e54aa2fbf15511b7f07f9fa09da32f39d0734 Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Sat, 9 Nov 2024 16:12:21 +0800 Subject: [PATCH 2/6] add FlowService push loop event --- example/example-application/pom.xml | 2 +- example/example-domain/pom.xml | 2 +- example/example-infra-flow/pom.xml | 2 +- example/example-infra-jpa/pom.xml | 2 +- example/example-server/pom.xml | 2 +- example/pom.xml | 2 +- pom.xml | 2 +- springboot-starter-data-fast/pom.xml | 2 +- springboot-starter-flow/pom.xml | 2 +- .../springboot/flow/service/FlowService.java | 18 +++++++++--------- springboot-starter-security/pom.xml | 2 +- springboot-starter/pom.xml | 2 +- 12 files changed, 20 insertions(+), 20 deletions(-) diff --git a/example/example-application/pom.xml b/example/example-application/pom.xml index fe05448b..ba19bd5b 100644 --- a/example/example-application/pom.xml +++ b/example/example-application/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.7 + 3.3.8 4.0.0 diff --git a/example/example-domain/pom.xml b/example/example-domain/pom.xml index e2a877b3..377d1e60 100644 --- a/example/example-domain/pom.xml +++ b/example/example-domain/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.7 + 3.3.8 4.0.0 diff --git a/example/example-infra-flow/pom.xml b/example/example-infra-flow/pom.xml index 9af0072a..d3816186 100644 --- a/example/example-infra-flow/pom.xml +++ b/example/example-infra-flow/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.7 + 3.3.8 4.0.0 diff --git a/example/example-infra-jpa/pom.xml b/example/example-infra-jpa/pom.xml index b0b4dba0..e87590b2 100644 --- a/example/example-infra-jpa/pom.xml +++ b/example/example-infra-jpa/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.7 + 3.3.8 4.0.0 diff --git a/example/example-server/pom.xml b/example/example-server/pom.xml index 38f32cf3..84f731f7 100644 --- a/example/example-server/pom.xml +++ b/example/example-server/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.7 + 3.3.8 4.0.0 diff --git a/example/pom.xml b/example/pom.xml index cc90de3f..ad903ddc 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -17,7 +17,7 @@ springboot-example - 3.3.7 + 3.3.8 springboot-example springboot-example project for Spring Boot diff --git a/pom.xml b/pom.xml index 3b9d38da..a3c53534 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ com.codingapi.springboot springboot-parent - 3.3.7 + 3.3.8 https://github.com/codingapi/springboot-framewrok springboot-parent diff --git a/springboot-starter-data-fast/pom.xml b/springboot-starter-data-fast/pom.xml index c8f0cc2c..1ff402d1 100644 --- a/springboot-starter-data-fast/pom.xml +++ b/springboot-starter-data-fast/pom.xml @@ -5,7 +5,7 @@ springboot-parent com.codingapi.springboot - 3.3.7 + 3.3.8 4.0.0 diff --git a/springboot-starter-flow/pom.xml b/springboot-starter-flow/pom.xml index 6fc8fe79..3a0d396f 100644 --- a/springboot-starter-flow/pom.xml +++ b/springboot-starter-flow/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 3.3.7 + 3.3.8 springboot-starter-flow diff --git a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/service/FlowService.java b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/service/FlowService.java index 7e032bd8..cb98e66d 100644 --- a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/service/FlowService.java +++ b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/service/FlowService.java @@ -97,7 +97,7 @@ public void urge(long recordId, IFlowOperator currentOperator) { // 推送催办消息 for (FlowRecord record : todoRecords) { IFlowOperator pushOperator = record.getCurrentOperator(); - EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_URGE, record, pushOperator, flowWork, null)); + EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_URGE, record, pushOperator, flowWork, null),true); } } @@ -220,10 +220,10 @@ public void transfer(long recordId, IFlowOperator currentOperator, IFlowOperator flowRecordRepository.save(List.of(transferRecord)); // 推送转办消息 - EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_TRANSFER, flowRecord, currentOperator, flowWork, snapshot.toBindData())); + EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_TRANSFER, flowRecord, currentOperator, flowWork, snapshot.toBindData()),true); // 推送待办消息 - EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_TODO, transferRecord, targetOperator, flowWork, snapshot.toBindData())); + EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_TODO, transferRecord, targetOperator, flowWork, snapshot.toBindData()),true); } @@ -327,8 +327,8 @@ public FlowResult startFlow(String workCode, IFlowOperator operator, IBindData b // 推送事件消息 for (FlowRecord record : records) { - EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_CREATE, record, operator, flowWork, snapshot.toBindData())); - EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_TODO, record, operator, flowWork, snapshot.toBindData())); + EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_CREATE, record, operator, flowWork, snapshot.toBindData()),true); + EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_TODO, record, operator, flowWork, snapshot.toBindData()),true); } // 当前的审批记录 return new FlowResult(flowWork, records); @@ -426,7 +426,7 @@ public FlowResult submitFlow(long recordId, IFlowOperator currentOperator, IBind flowRecordRepository.update(flowRecord); flowRecordRepository.finishFlowRecordByProcessId(flowRecord.getProcessId()); - EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_FINISH, flowRecord, currentOperator, flowWork, snapshot.toBindData())); + EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_FINISH, flowRecord, currentOperator, flowWork, snapshot.toBindData()),true); return new FlowResult(flowWork, flowRecord); } @@ -465,12 +465,12 @@ public FlowResult submitFlow(long recordId, IFlowOperator currentOperator, IBind // 推送审批事件消息 int eventState = flowSourceDirection == FlowSourceDirection.PASS ? FlowApprovalEvent.STATE_PASS : FlowApprovalEvent.STATE_REJECT; - EventPusher.push(new FlowApprovalEvent(eventState, flowRecord, currentOperator, flowWork, snapshot.toBindData())); + EventPusher.push(new FlowApprovalEvent(eventState, flowRecord, currentOperator, flowWork, snapshot.toBindData()),true); // 推送待办事件消息 for (FlowRecord record : records) { IFlowOperator pushOperator = record.getCurrentOperator(); - EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_TODO, record, pushOperator, flowWork, snapshot.toBindData())); + EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_TODO, record, pushOperator, flowWork, snapshot.toBindData()),true); } return new FlowResult(flowWork, records); @@ -514,7 +514,7 @@ public void recall(long recordId, IFlowOperator currentOperator) { flowRecordRepository.update(flowRecord); flowRecordRepository.delete(childrenRecords); - EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_RECALL, flowRecord, currentOperator, flowWork, null)); + EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_RECALL, flowRecord, currentOperator, flowWork, null),true); } } diff --git a/springboot-starter-security/pom.xml b/springboot-starter-security/pom.xml index c06f3cca..40f1ccc0 100644 --- a/springboot-starter-security/pom.xml +++ b/springboot-starter-security/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 3.3.7 + 3.3.8 springboot-starter-security diff --git a/springboot-starter/pom.xml b/springboot-starter/pom.xml index 28d7733c..25acd388 100644 --- a/springboot-starter/pom.xml +++ b/springboot-starter/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot springboot-parent - 3.3.7 + 3.3.8 springboot-starter From fa420501aeb36185203a19e12ba5894556ea4f83 Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Sat, 9 Nov 2024 20:36:36 +0800 Subject: [PATCH 3/6] add event exception logs --- example/example-application/pom.xml | 2 +- example/example-domain/pom.xml | 2 +- example/example-infra-flow/pom.xml | 2 +- example/example-infra-jpa/pom.xml | 2 +- example/example-server/pom.xml | 2 +- example/pom.xml | 2 +- pom.xml | 2 +- springboot-starter-data-fast/pom.xml | 2 +- springboot-starter-flow/pom.xml | 2 +- springboot-starter-security/pom.xml | 2 +- springboot-starter/pom.xml | 2 +- .../springboot/framework/exception/EventException.java | 3 +++ 12 files changed, 14 insertions(+), 11 deletions(-) diff --git a/example/example-application/pom.xml b/example/example-application/pom.xml index ba19bd5b..81a7c792 100644 --- a/example/example-application/pom.xml +++ b/example/example-application/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.8 + 3.3.9.1 4.0.0 diff --git a/example/example-domain/pom.xml b/example/example-domain/pom.xml index 377d1e60..9f57388e 100644 --- a/example/example-domain/pom.xml +++ b/example/example-domain/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.8 + 3.3.9.1 4.0.0 diff --git a/example/example-infra-flow/pom.xml b/example/example-infra-flow/pom.xml index d3816186..cbed9c56 100644 --- a/example/example-infra-flow/pom.xml +++ b/example/example-infra-flow/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.8 + 3.3.9.1 4.0.0 diff --git a/example/example-infra-jpa/pom.xml b/example/example-infra-jpa/pom.xml index e87590b2..5dfe8f57 100644 --- a/example/example-infra-jpa/pom.xml +++ b/example/example-infra-jpa/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.8 + 3.3.9.1 4.0.0 diff --git a/example/example-server/pom.xml b/example/example-server/pom.xml index 84f731f7..9b311e20 100644 --- a/example/example-server/pom.xml +++ b/example/example-server/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.8 + 3.3.9.1 4.0.0 diff --git a/example/pom.xml b/example/pom.xml index ad903ddc..f427156f 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -17,7 +17,7 @@ springboot-example - 3.3.8 + 3.3.9.1 springboot-example springboot-example project for Spring Boot diff --git a/pom.xml b/pom.xml index a3c53534..4622758a 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ com.codingapi.springboot springboot-parent - 3.3.8 + 3.3.9.1 https://github.com/codingapi/springboot-framewrok springboot-parent diff --git a/springboot-starter-data-fast/pom.xml b/springboot-starter-data-fast/pom.xml index 1ff402d1..795adc50 100644 --- a/springboot-starter-data-fast/pom.xml +++ b/springboot-starter-data-fast/pom.xml @@ -5,7 +5,7 @@ springboot-parent com.codingapi.springboot - 3.3.8 + 3.3.9.1 4.0.0 diff --git a/springboot-starter-flow/pom.xml b/springboot-starter-flow/pom.xml index 3a0d396f..787a51aa 100644 --- a/springboot-starter-flow/pom.xml +++ b/springboot-starter-flow/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 3.3.8 + 3.3.9.1 springboot-starter-flow diff --git a/springboot-starter-security/pom.xml b/springboot-starter-security/pom.xml index 40f1ccc0..7100cc43 100644 --- a/springboot-starter-security/pom.xml +++ b/springboot-starter-security/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 3.3.8 + 3.3.9.1 springboot-starter-security diff --git a/springboot-starter/pom.xml b/springboot-starter/pom.xml index 25acd388..2a9dd65c 100644 --- a/springboot-starter/pom.xml +++ b/springboot-starter/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot springboot-parent - 3.3.8 + 3.3.9.1 springboot-starter diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/exception/EventException.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/exception/EventException.java index 68462e73..5a506fec 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/exception/EventException.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/exception/EventException.java @@ -13,5 +13,8 @@ public class EventException extends RuntimeException { public EventException(List error) { super(error.stream().map(Exception::getMessage).collect(Collectors.joining("\n"))); this.error = error; + for (Exception e : error) { + e.printStackTrace(); + } } } From ebaa3a01f1bbebb55660c7fe9fb8ba66acf3d888 Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Tue, 12 Nov 2024 21:24:35 +0800 Subject: [PATCH 4/6] SecurityLoginHandler add UserDetails --- example/example-application/pom.xml | 2 +- example/example-domain/pom.xml | 2 +- example/example-infra-flow/pom.xml | 2 +- example/example-infra-jpa/pom.xml | 2 +- example/example-server/pom.xml | 2 +- example/pom.xml | 2 +- pom.xml | 2 +- springboot-starter-data-fast/pom.xml | 2 +- springboot-starter-flow/pom.xml | 27 ++++++++++++++++- springboot-starter-security/pom.xml | 2 +- .../security/AutoConfiguration.java | 2 +- .../security/filter/MyLoginFilter.java | 30 +++++++++++++------ .../security/filter/SecurityLoginHandler.java | 4 ++- springboot-starter/pom.xml | 2 +- 14 files changed, 61 insertions(+), 22 deletions(-) diff --git a/example/example-application/pom.xml b/example/example-application/pom.xml index 81a7c792..bfb05799 100644 --- a/example/example-application/pom.xml +++ b/example/example-application/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.9.1 + 3.3.10 4.0.0 diff --git a/example/example-domain/pom.xml b/example/example-domain/pom.xml index 9f57388e..ca585179 100644 --- a/example/example-domain/pom.xml +++ b/example/example-domain/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.9.1 + 3.3.10 4.0.0 diff --git a/example/example-infra-flow/pom.xml b/example/example-infra-flow/pom.xml index cbed9c56..95ce0607 100644 --- a/example/example-infra-flow/pom.xml +++ b/example/example-infra-flow/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.9.1 + 3.3.10 4.0.0 diff --git a/example/example-infra-jpa/pom.xml b/example/example-infra-jpa/pom.xml index 5dfe8f57..0159872f 100644 --- a/example/example-infra-jpa/pom.xml +++ b/example/example-infra-jpa/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.9.1 + 3.3.10 4.0.0 diff --git a/example/example-server/pom.xml b/example/example-server/pom.xml index 9b311e20..06f6e28d 100644 --- a/example/example-server/pom.xml +++ b/example/example-server/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.3.9.1 + 3.3.10 4.0.0 diff --git a/example/pom.xml b/example/pom.xml index f427156f..08914efa 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -17,7 +17,7 @@ springboot-example - 3.3.9.1 + 3.3.10 springboot-example springboot-example project for Spring Boot diff --git a/pom.xml b/pom.xml index 4622758a..56c6bdcd 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ com.codingapi.springboot springboot-parent - 3.3.9.1 + 3.3.10 https://github.com/codingapi/springboot-framewrok springboot-parent diff --git a/springboot-starter-data-fast/pom.xml b/springboot-starter-data-fast/pom.xml index 795adc50..a16f8486 100644 --- a/springboot-starter-data-fast/pom.xml +++ b/springboot-starter-data-fast/pom.xml @@ -5,7 +5,7 @@ springboot-parent com.codingapi.springboot - 3.3.9.1 + 3.3.10 4.0.0 diff --git a/springboot-starter-flow/pom.xml b/springboot-starter-flow/pom.xml index 787a51aa..ad52840d 100644 --- a/springboot-starter-flow/pom.xml +++ b/springboot-starter-flow/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 3.3.9.1 + 3.3.10 springboot-starter-flow @@ -46,5 +46,30 @@ + + + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + + prepare-agent + + + + report + test + + report + + + + + + + + diff --git a/springboot-starter-security/pom.xml b/springboot-starter-security/pom.xml index 7100cc43..302d295c 100644 --- a/springboot-starter-security/pom.xml +++ b/springboot-starter-security/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 3.3.9.1 + 3.3.10 springboot-starter-security diff --git a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/AutoConfiguration.java b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/AutoConfiguration.java index 54a77d48..2f40a953 100644 --- a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/AutoConfiguration.java +++ b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/AutoConfiguration.java @@ -71,7 +71,7 @@ public void preHandle(HttpServletRequest request, HttpServletResponse response, } @Override - public LoginResponse postHandle(HttpServletRequest request, HttpServletResponse response, LoginRequest loginRequest, Token token) { + public LoginResponse postHandle(HttpServletRequest request, HttpServletResponse response, LoginRequest loginRequest,UserDetails user, Token token) { LoginResponse loginResponse = new LoginResponse(); loginResponse.setToken(token.getToken()); loginResponse.setUsername(token.getUsername()); diff --git a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyLoginFilter.java b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyLoginFilter.java index 728760b3..a110aa32 100644 --- a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyLoginFilter.java +++ b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyLoginFilter.java @@ -6,10 +6,15 @@ import com.codingapi.springboot.security.dto.request.LoginRequest; import com.codingapi.springboot.security.dto.request.LoginRequestContext; import com.codingapi.springboot.security.dto.response.LoginResponse; -import com.codingapi.springboot.security.gateway.TokenGateway; import com.codingapi.springboot.security.gateway.Token; import com.codingapi.springboot.security.gateway.TokenContext; +import com.codingapi.springboot.security.gateway.TokenGateway; import com.codingapi.springboot.security.properties.CodingApiSecurityProperties; +import io.jsonwebtoken.Header; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; import org.springframework.security.authentication.AuthenticationManager; @@ -18,14 +23,10 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.GrantedAuthority; -import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; -import jakarta.servlet.FilterChain; -import jakarta.servlet.ServletException; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.stream.Collectors; @@ -58,7 +59,7 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ throw new AuthenticationServiceException("request stream read was null."); } try { - loginHandler.preHandle(request,response,login); + loginHandler.preHandle(request, response, login); } catch (Exception e) { throw new AuthenticationServiceException(e.getLocalizedMessage()); } @@ -69,15 +70,20 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ @Override protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException { log.debug("login success authentication ~"); - User user = (User) authResult.getPrincipal(); + UserDetails user = (UserDetails) authResult.getPrincipal(); LoginRequest loginRequest = LoginRequestContext.getInstance().get(); Token token = tokenGateway.create(user.getUsername(), loginRequest.getPassword(), user.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList()), TokenContext.getExtra()); - LoginResponse loginResponse = loginHandler.postHandle(request,response,loginRequest,token); + LoginResponse loginResponse = loginHandler.postHandle(request, response, loginRequest, user, token); String content = JSONObject.toJSONString(SingleResponse.of(loginResponse)); + + // 设置响应的 Content-Type 为 JSON,并指定字符编码为 UTF-8 + response.setContentType("application/json;charset=UTF-8"); + response.setCharacterEncoding("UTF-8"); + IOUtils.write(content, response.getOutputStream(), StandardCharsets.UTF_8); LoginRequestContext.getInstance().clean(); @@ -89,7 +95,13 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException { log.debug("login fail authentication ~"); String content = JSONObject.toJSONString(Response.buildFailure("login.error", failed.getMessage())); + + // 设置响应的 Content-Type 为 JSON,并指定字符编码为 UTF-8 + response.setContentType("application/json;charset=UTF-8"); + response.setCharacterEncoding("UTF-8"); + IOUtils.write(content, response.getOutputStream(), StandardCharsets.UTF_8); + LoginRequestContext.getInstance().clean(); } } diff --git a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/SecurityLoginHandler.java b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/SecurityLoginHandler.java index d367eeab..a9e68ba8 100644 --- a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/SecurityLoginHandler.java +++ b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/SecurityLoginHandler.java @@ -5,11 +5,13 @@ import com.codingapi.springboot.security.gateway.Token; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; public interface SecurityLoginHandler { void preHandle(HttpServletRequest request, HttpServletResponse response, LoginRequest loginRequest) throws Exception; - LoginResponse postHandle(HttpServletRequest request, HttpServletResponse response, LoginRequest loginRequest, Token token); + LoginResponse postHandle(HttpServletRequest request, HttpServletResponse response, LoginRequest loginRequest, UserDetails user, Token token); } diff --git a/springboot-starter/pom.xml b/springboot-starter/pom.xml index 2a9dd65c..84c7d44a 100644 --- a/springboot-starter/pom.xml +++ b/springboot-starter/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot springboot-parent - 3.3.9.1 + 3.3.10 springboot-starter From 565376091150bb1e13e52868c31daccc51cd8cad Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Tue, 12 Nov 2024 21:28:21 +0800 Subject: [PATCH 5/6] SecurityLoginHandler add UserDetails --- .../springboot/security/filter/MyAccessDeniedHandler.java | 4 ++++ .../springboot/security/filter/MyAuthenticationFilter.java | 5 +++++ .../springboot/security/filter/MyLogoutSuccessHandler.java | 4 ++++ .../security/filter/MyUnAuthenticationEntryPoint.java | 4 ++++ 4 files changed, 17 insertions(+) diff --git a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyAccessDeniedHandler.java b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyAccessDeniedHandler.java index 191d1dfc..a1a9bafd 100644 --- a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyAccessDeniedHandler.java +++ b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyAccessDeniedHandler.java @@ -20,6 +20,10 @@ public class MyAccessDeniedHandler implements AccessDeniedHandler { public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException { log.debug("access denied"); String content = JSONObject.toJSONString(Response.buildFailure("not.access", "please check user authentication.")); + // 设置响应的 Content-Type 为 JSON,并指定字符编码为 UTF-8 + response.setContentType("application/json;charset=UTF-8"); + response.setCharacterEncoding("UTF-8"); + IOUtils.write(content, response.getOutputStream(), StandardCharsets.UTF_8); } } diff --git a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyAuthenticationFilter.java b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyAuthenticationFilter.java index dd414bdd..db56c4ae 100644 --- a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyAuthenticationFilter.java +++ b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyAuthenticationFilter.java @@ -2,6 +2,7 @@ import com.alibaba.fastjson.JSONObject; import com.codingapi.springboot.framework.dto.response.Response; +import com.codingapi.springboot.security.dto.request.LoginRequestContext; import com.codingapi.springboot.security.exception.TokenExpiredException; import com.codingapi.springboot.security.gateway.Token; import com.codingapi.springboot.security.gateway.TokenGateway; @@ -80,6 +81,10 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse private void writeResponse(HttpServletResponse servletResponse, Response returnResponse) throws IOException { String content = JSONObject.toJSONString(returnResponse); + // 设置响应的 Content-Type 为 JSON,并指定字符编码为 UTF-8 + servletResponse.setContentType("application/json;charset=UTF-8"); + servletResponse.setCharacterEncoding("UTF-8"); + IOUtils.write(content, servletResponse.getOutputStream(), StandardCharsets.UTF_8); } diff --git a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyLogoutSuccessHandler.java b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyLogoutSuccessHandler.java index 16a86c68..247bad68 100644 --- a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyLogoutSuccessHandler.java +++ b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyLogoutSuccessHandler.java @@ -20,6 +20,10 @@ public class MyLogoutSuccessHandler implements LogoutSuccessHandler { public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { log.debug("logout success ~"); String content = JSONObject.toJSONString(Response.buildSuccess()); + // 设置响应的 Content-Type 为 JSON,并指定字符编码为 UTF-8 + response.setContentType("application/json;charset=UTF-8"); + response.setCharacterEncoding("UTF-8"); + IOUtils.write(content, response.getOutputStream(), StandardCharsets.UTF_8); } } diff --git a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyUnAuthenticationEntryPoint.java b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyUnAuthenticationEntryPoint.java index ecc91b95..84716a70 100644 --- a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyUnAuthenticationEntryPoint.java +++ b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyUnAuthenticationEntryPoint.java @@ -20,6 +20,10 @@ public class MyUnAuthenticationEntryPoint implements AuthenticationEntryPoint { public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { log.debug("no authentication ~"); String content = JSONObject.toJSONString(Response.buildFailure("not.login", "please to login.")); + // 设置响应的 Content-Type 为 JSON,并指定字符编码为 UTF-8 + response.setContentType("application/json;charset=UTF-8"); + response.setCharacterEncoding("UTF-8"); + IOUtils.write(content, response.getOutputStream(), StandardCharsets.UTF_8); } } From b87e6a70e4bb74dd10cbe347a2ad57e7e405bb94 Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Tue, 12 Nov 2024 23:24:33 +0800 Subject: [PATCH 6/6] SecurityLoginHandler add UserDetails --- .../springboot/security/filter/MyAuthenticationFilter.java | 1 - 1 file changed, 1 deletion(-) diff --git a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyAuthenticationFilter.java b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyAuthenticationFilter.java index db56c4ae..2fb81212 100644 --- a/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyAuthenticationFilter.java +++ b/springboot-starter-security/src/main/java/com/codingapi/springboot/security/filter/MyAuthenticationFilter.java @@ -2,7 +2,6 @@ import com.alibaba.fastjson.JSONObject; import com.codingapi.springboot.framework.dto.response.Response; -import com.codingapi.springboot.security.dto.request.LoginRequestContext; import com.codingapi.springboot.security.exception.TokenExpiredException; import com.codingapi.springboot.security.gateway.Token; import com.codingapi.springboot.security.gateway.TokenGateway;