-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($RBAC): migrate RBAC codes from project
jm-spring-boot-template
Aspects: - RBAC - SFTP - Redis
- Loading branch information
1 parent
19546e1
commit bd559d2
Showing
30 changed files
with
1,289 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...c/main/java/com/jmsoftware/apiportal/universal/configuration/SftpClientConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...rc/main/java/com/jmsoftware/apiportal/universal/configuration/SftpSubDirectoryRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...tal/src/main/java/com/jmsoftware/apiportal/universal/configuration/SftpUploadGateway.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
34 changes: 34 additions & 0 deletions
34
api-portal/src/main/java/com/jmsoftware/apiportal/universal/domain/GetUserInfoRO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...portal/universal/mapper/PermissionPO.java → ...portal/universal/domain/PermissionPO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
api-portal/src/main/java/com/jmsoftware/apiportal/universal/domain/RolePermissionPO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
30 changes: 30 additions & 0 deletions
30
api-portal/src/main/java/com/jmsoftware/apiportal/universal/domain/SftpSubDirectory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
api-portal/src/main/java/com/jmsoftware/apiportal/universal/domain/SftpUploadFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.