Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,31 @@

import com.neroyun.mediator.*;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskDecorator;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.util.concurrent.CompletableFuture;

@SuppressWarnings("rawtypes")
@Configuration
public class MediatorConfiguration {
@Bean
public Mediator mediator(ObjectProvider<Handler> handlers, ObjectProvider<Middleware> middlewares, ObjectProvider<Validator> validators) {
public Mediator mediator(ObjectProvider<Handler> handlers, ObjectProvider<Middleware> middlewares, ObjectProvider<Validator> validators, ApplicationEventPublisher publisher) {
return new PipelinedMediator()
.use(() -> handlers.stream())
.use(() -> middlewares.stream())
.use(() -> validators.stream());
.use(() -> validators.stream())
.use(event -> CompletableFuture.runAsync(() -> {
publisher.publishEvent(event);
}, taskExecutor().getThreadPoolExecutor()));
}

@Bean(name = "taskExecutor")
Expand Down Expand Up @@ -47,4 +56,11 @@ private TaskDecorator copyRequestContextDecorator() {
};
};
}

@Bean(name = "applicationEventMulticaster")
public ApplicationEventMulticaster simpleApplicationEventMulticaster() {
SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
multicaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
return multicaster;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.theurl.identity;
package io.theurl.framework.utility;

import lombok.Getter;
import org.jspecify.annotations.NonNull;
Expand Down
40 changes: 24 additions & 16 deletions identity/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@
<artifactId>jjwt-api</artifactId>
<version>0.13.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-amqp</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-redis</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down Expand Up @@ -74,20 +74,25 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-amqp-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-redis-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-test</artifactId>
<scope>test</scope>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>${modelmapper.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -113,6 +118,9 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<parameters>true</parameters>
</configuration>
<executions>
<execution>
<id>default-compile</id>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.theurl.identity.application.command;

import com.neroyun.mediator.Command;
import lombok.Data;

import java.time.LocalDateTime;

@Data
public class TokenCreateCommand implements Command {
private String jti;
private String content;
private Long subject;
private LocalDateTime issuedAt;
private LocalDateTime expiresAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.theurl.identity.application.command;

import com.neroyun.mediator.Command;
import lombok.Getter;

/**
* Command to update the access failure count of a user.
* This command is typically used when a user authentication attempt fails, and we want to increment the failure count for that user.
* The command contains the user ID and the new failure count to be set.
*/
@Getter
public class UserAccessFailureCountCommand implements Command {

private final Long userId;
private final String action;

public UserAccessFailureCountCommand(Long userId, String action) {
this.userId = userId;
this.action = action;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.theurl.identity.application.command;

public class UserCreateCommand {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.theurl.identity.application.command;

public class UserUpdateCommand {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.theurl.identity.application.contract;

import io.theurl.framework.application.ApplicationService;

public interface UserApplicationService extends ApplicationService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.theurl.identity.application.event;

import io.theurl.framework.domain.ApplicationEvent;

public class TokenGrantedEvent extends ApplicationEvent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.theurl.identity.application.event;

import io.theurl.framework.domain.ApplicationEvent;

public class TokenRefreshedEvent extends ApplicationEvent {
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@EqualsAndHashCode(callSuper = true)
@Data
public class UserAuthFailureEvent extends ApplicationEvent implements Event {
private String userId;
private Long userId;
private String username;
private String grantType;
private LocalDateTime grantTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.theurl.identity.application.handler;

import com.neroyun.mediator.Handler;
import io.theurl.identity.application.command.TokenCreateCommand;
import io.theurl.identity.domain.aggregate.Token;
import io.theurl.identity.domain.repository.TokenRepository;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import java.util.concurrent.CompletableFuture;

@Component
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class TokenCreateCommandHandler implements Handler<TokenCreateCommand, Void> {

@Resource
private TokenRepository tokenRepository;

@Override
public CompletableFuture<Void> handleAsync(TokenCreateCommand message) {
var token = Token.create(message.getJti(), message.getContent(), message.getSubject());
token.setExpiresAt(message.getExpiresAt());
token.setIssuedAt(message.getIssuedAt());
tokenRepository.save(token);
return CompletableFuture.completedFuture(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.theurl.identity.application.handler;

import com.neroyun.mediator.Handler;
import io.theurl.identity.application.command.UserAccessFailureCountCommand;
import io.theurl.identity.domain.repository.UserRepository;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import java.util.concurrent.CompletableFuture;

@Component
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class UserAccessFailureCountCommandHandler implements Handler<UserAccessFailureCountCommand, Void> {

private final UserRepository repository;

public UserAccessFailureCountCommandHandler(UserRepository repository) {
this.repository = repository;
}

@Override
public CompletableFuture<Void> handleAsync(UserAccessFailureCountCommand message) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.theurl.identity.application.implement;

import io.theurl.framework.application.BaseApplicationService;
import io.theurl.identity.application.contract.UserApplicationService;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.RequestScope;

@Service
@RequestScope
public class UserApplicationServiceImpl extends BaseApplicationService implements UserApplicationService {

public UserApplicationServiceImpl(ApplicationContext applicationContext) {
super(applicationContext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.theurl.identity.application.subscriber;

public class LoggingEventSubscriber {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.theurl.identity.application.subscriber;

import com.neroyun.mediator.Mediator;
import io.theurl.identity.application.command.TokenCreateCommand;
import io.theurl.identity.application.event.UserAuthSuccessEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class TokenEventSubscriber {
private final Mediator mediator;

public TokenEventSubscriber(Mediator mediator) {
this.mediator = mediator;
}

@Async
@EventListener
public void handleUserAuthSucceedEvent(UserAuthSuccessEvent event) {
var command = new TokenCreateCommand() {{
setJti(event.getData().get("jti"));
setContent(event.getData().get("content"));
setSubject(event.getUserId());
}};

mediator.sendAsync(command)
.join();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.theurl.identity.application.subscriber;

import com.neroyun.mediator.Mediator;
import io.theurl.identity.application.command.UserAccessFailureCountCommand;
import io.theurl.identity.application.event.UserAuthFailureEvent;
import io.theurl.identity.application.event.UserAuthSuccessEvent;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Scope;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
@AllArgsConstructor
public class UserAccessFailureCountEventSubscriber {

private final Mediator mediator;

@Async
@EventListener
public void listen(UserAuthFailureEvent event) {
if (event.getUserId() == null || event.getUserId() <= 0) {
return;
}

mediator.sendAsync(new UserAccessFailureCountCommand(event.getUserId(), "increase"))
.join();
}

@Async
@EventListener
public void listen(UserAuthSuccessEvent event) {
if (event.getUserId() == null || event.getUserId() <= 0) {
return;
}
mediator.sendAsync(new UserAccessFailureCountCommand(event.getUserId(), "reset"))
.join();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.theurl.identity.configure;

import org.modelmapper.ModelMapper;
import org.modelmapper.config.Configuration.AccessLevel;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ModelMapperConfiguration {
@Bean
public ModelMapper modelMapper() {
ModelMapper mapper = new ModelMapper();

mapper.getConfiguration()
.setMatchingStrategy(MatchingStrategies.STRICT)
.setFieldMatchingEnabled(true)
.setFieldAccessLevel(AccessLevel.PRIVATE)
.setCollectionsMergeEnabled(true)
.setSkipNullEnabled(true);

return mapper;
}
}
Loading
Loading