Skip to content

Dev #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 12, 2024
Merged

Dev #79

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/example-application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-example</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.6</version>
<version>3.3.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,7 +16,7 @@ public class CHandler implements IHandler<CEvent> {
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");
}

Expand Down
2 changes: 1 addition & 1 deletion example/example-domain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-example</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.6</version>
<version>3.3.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion example/example-infra-flow/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-example</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.6</version>
<version>3.3.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion example/example-infra-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-example</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.6</version>
<version>3.3.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion example/example-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-example</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.6</version>
<version>3.3.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</parent>

<artifactId>springboot-example</artifactId>
<version>3.3.6</version>
<version>3.3.10</version>

<name>springboot-example</name>
<description>springboot-example project for Spring Boot</description>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-parent</artifactId>
<version>3.3.6</version>
<version>3.3.10</version>

<url>https://github.com/codingapi/springboot-framewrok</url>
<name>springboot-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion springboot-starter-data-fast/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.6</version>
<version>3.3.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
27 changes: 26 additions & 1 deletion springboot-starter-flow/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.6</version>
<version>3.3.10</version>
</parent>

<name>springboot-starter-flow</name>
Expand Down Expand Up @@ -46,5 +46,30 @@
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>


</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

}
2 changes: 1 addition & 1 deletion springboot-starter-security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.6</version>
<version>3.3.10</version>
</parent>

<artifactId>springboot-starter-security</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}
Expand All @@ -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();
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
2 changes: 1 addition & 1 deletion springboot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-parent</artifactId>
<version>3.3.6</version>
<version>3.3.10</version>
</parent>
<artifactId>springboot-starter</artifactId>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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);
}
}

Expand Down
Loading