Skip to content

Commit

Permalink
feat($RBAC): migrate RBAC codes from project jm-spring-boot-template
Browse files Browse the repository at this point in the history
Aspects:
 - RBAC
 - SFTP
 - Redis
  • Loading branch information
johnnymillergh committed May 6, 2020
1 parent 19546e1 commit bd559d2
Show file tree
Hide file tree
Showing 30 changed files with 1,289 additions and 16 deletions.
4 changes: 4 additions & 0 deletions api-portal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-sftp</artifactId>
</dependency>

<dependency>
<groupId>com.jmsoftware</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.jmsoftware.apiportal.universal.configuration;

import com.jcraft.jsch.ChannelSftp;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.file.remote.session.CachingSessionFactory;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.sftp.outbound.SftpMessageHandler;
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;
import org.springframework.integration.sftp.session.SftpRemoteFileTemplate;
import org.springframework.messaging.MessageHandler;
import org.springframework.stereotype.Component;

import java.io.File;

/**
* <h1>SftpClientConfiguration</h1>
* <p>SFTP client configuration</p>
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 2019-07-04 18:18
**/
@Data
@Slf4j
@Component
@ConfigurationProperties(prefix = "sftp.client.configuration")
public class SftpClientConfiguration {
/**
* SFTP server IP
*/
private String host;
/**
* SFTP server port
*/
private Integer port;
/**
* Login user
*/
private String user;
/**
* Login password
*/
private String password;
/**
* Remote directory
*/
private String directory;
/**
* Private key
*/
private Resource privateKey;
/**
* Private key pass phrase
*/
private String privateKeyPassPhrase;
/**
* The maximum cache size of session. Default: 10
*/
private Integer sessionCacheSize = 10;
/**
* The session wait timeout (time unit: MILLISECONDS). Default: 10 * 1000L (10 seconds)
*/
private Long sessionWaitTimeout = 10 * 1000L;

@Bean
public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(host);
factory.setPort(port);
factory.setUser(user);
if (privateKey != null) {
factory.setPrivateKey(privateKey);
factory.setPrivateKeyPassphrase(privateKeyPassPhrase);
} else {
factory.setPassword(password);
}
factory.setAllowUnknownKeys(true);
// We return a caching session factory, so that we don't have to reconnect to SFTP server for each time
CachingSessionFactory<ChannelSftp.LsEntry> cachingSessionFactory = new CachingSessionFactory<>(factory,
sessionCacheSize);
cachingSessionFactory.setSessionWaitTimeout(sessionWaitTimeout);
return cachingSessionFactory;
}

@Bean
@ServiceActivator(inputChannel = "toSftpChannel")
@SuppressWarnings("UnresolvedMessageChannel")
public MessageHandler handler(SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory) {
SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory);
handler.setRemoteDirectoryExpression(new LiteralExpression(directory));
handler.setFileNameGenerator(message -> {
if (message.getPayload() instanceof File) {
return ((File) message.getPayload()).getName();
} else {
throw new IllegalArgumentException("File expected as payload.");
}
});
return handler;
}

@Bean
public SftpRemoteFileTemplate template(SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory) {
return new SftpRemoteFileTemplate(sftpSessionFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.jmsoftware.apiportal.universal.configuration;

import com.jmsoftware.apiportal.universal.domain.SftpSubDirectory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.integration.sftp.session.SftpRemoteFileTemplate;
import org.springframework.stereotype.Component;

/**
* <h1>SftpSubDirectoryRunner</h1>
* <p>After dependency injection finished, we must inti the SFTP server's sub directory for out business. If you want
* to customize initialization configuration, config SftpSubDirectory.</p>
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 2019-07-05 08:51
* @see SftpSubDirectory
**/
@Slf4j
@Component
@RequiredArgsConstructor
public class SftpSubDirectoryRunner implements ApplicationRunner {
private final SftpRemoteFileTemplate sftpRemoteFileTemplate;
private final SftpClientConfiguration sftpClientConfiguration;

@Override
public void run(ApplicationArguments args) {
sftpRemoteFileTemplate.setAutoCreateDirectory(true);
sftpRemoteFileTemplate.execute(session -> {
if (!session.exists(sftpClientConfiguration.getDirectory())) {
log.info("Make directories for SFTP server. Directory: {}", sftpClientConfiguration.getDirectory());
session.mkdir(sftpClientConfiguration.getDirectory());
} else {
log.info("SFTP server remote directory exists: {}", sftpClientConfiguration.getDirectory());
}
return null;
});

log.info("Staring to initial SFTP server sub directory.");
sftpRemoteFileTemplate.execute(session -> {
for (SftpSubDirectory sftpSubDirectory : SftpSubDirectory.values()) {
String fullPath = sftpClientConfiguration.getDirectory() + sftpSubDirectory.getSubDirectory();
if (!session.exists(fullPath)) {
log.info("SFTP server sub directory does not exist. Creating sub directory: {}", fullPath);
session.mkdir(fullPath);
} else {
log.info("SFTP server sub directory exists. Path: {}", fullPath);
}
}
return null;
});
log.info("Initialing SFTP server sub directory is done.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.jmsoftware.apiportal.universal.configuration;

import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.stereotype.Component;

import java.io.File;

/**
* <h1>SftpUploadGateway</h1>
* <p>Change description here</p>
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 2019-07-04 21:03
**/
@Component
@MessagingGateway
public interface SftpUploadGateway {
/**
* Upload file
*
* @param file file
*/
@Gateway(requestChannel = "toSftpChannel")
@SuppressWarnings("UnresolvedMessageChannel")
void upload(File file);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.jmsoftware.apiportal.universal.domain;

import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* <h1>GetUserInfoRO</h1>
* <p>Change description here</p>
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 2019-06-30 11:01
**/
@Data
public class GetUserInfoRO {
private Long id;
private String username;
private String email;
private String cellphone;
private String fullName;
private Date birthday;
private String gender;
private Integer status;
private List<UsersRole> usersRoles = new ArrayList<>();

@Data
public static class UsersRole {
private Long roleId;
private String roleName;
private String roleDescription;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jmsoftware.apiportal.universal.mapper;
package com.jmsoftware.apiportal.universal.domain;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public class RolePO {
/**
* Create time
*/
private Date gmtCreated;
private Date createdTime;
/**
* Modify time
*/
private Date gmtModified;
private Date modifiedTime;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.jmsoftware.apiportal.universal.domain;

import lombok.Data;

/**
* <h1>RolePermissionPO</h1>
* <p>Role-permission relation. Persistence class for table `t_role_permission`</p>
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 2019-03-23 19:50
**/
@Data
public class RolePermissionPO {
/**
* Role's ID.
*/
private Long roleId;
/**
* Permission's ID.
*/
private Long permissionId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.jmsoftware.apiportal.universal.domain;

import lombok.Getter;

/**
* <h1>SftpSubDirectory</h1>
* <p>Reminder: if you want to add more custom sub directories in the future, please add en enum item in this class</p>
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 2019-07-04 23:10
**/
@Getter
public enum SftpSubDirectory {
/**
* Sub directory for video
*/
VIDEO("video", "/video/"),
/**
* Sub directory for avatar
*/
AVATAR("avatar", "/avatar/");

private final String directoryName;
private final String subDirectory;

SftpSubDirectory(String directoryName, String subDirectory) {
this.directoryName = directoryName;
this.subDirectory = subDirectory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.jmsoftware.apiportal.universal.domain;

import lombok.Builder;
import lombok.Data;
import org.springframework.integration.file.support.FileExistsMode;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.File;

/**
* <h1>SftpUploadFile</h1>
* <p>Change description here</p>
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 2019-07-06 11:22
**/
@Data
@Builder
public class SftpUploadFile {
/**
* File to be uploaded to SFTP server
*/
@NotNull
private File fileToBeUploaded;
/**
* SFTP server's sub directory (if sub directory does'nt exist, will be auto created). Not empty and it looks
* like this: "/some/sub/directory/"
*/
@NotEmpty
private String subDirectory;
/**
* This enumeration indicates what action shall be taken in case the destination file already exists. In default,
* it should be set as: FileExistsMode.REPLACE
*/
@NotNull
private FileExistsMode fileExistsMode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public class UserPO {
/**
* Create time
*/
private Date gmtCreated;
private Date createdTime;
/**
* Modify time
*/
private Date gmtModified;
private Date modifiedTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jmsoftware.apiportal.universal.mapper.PermissionPO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -109,8 +108,8 @@ public static UserPrincipal create(UserPO user, List<RolePO> roleList, List<Perm
user.getBirthday(),
user.getGender(),
user.getStatus(),
user.getGmtCreated(),
user.getGmtModified(),
user.getCreatedTime(),
user.getModifiedTime(),
roleNames,
authorities);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jmsoftware.apiportal.universal.domain.PermissionPO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
Expand Down
Loading

0 comments on commit bd559d2

Please sign in to comment.