Skip to content

Commit

Permalink
feat($auth-center): migrate codes from api-portal
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed May 10, 2020
1 parent efc5c26 commit 9b07500
Show file tree
Hide file tree
Showing 32 changed files with 1,724 additions and 181 deletions.
29 changes: 27 additions & 2 deletions auth-center/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,22 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>com.jmsoftware</groupId>
Expand Down Expand Up @@ -123,10 +135,23 @@
<artifactId>jedis</artifactId>
</dependency>

<!-- https://github.com/jwtk/jjwt -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
<artifactId>jjwt-api</artifactId>
<version>0.11.1</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId> <!-- or jjwt-gson if Gson is preferred -->
<version>0.11.1</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.jmsoftware.authcenter.universal;
package com.jmsoftware.authcenter.universal.configuration;

import com.jmsoftware.authcenter.universal.configuration.ProjectProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
Expand All @@ -9,12 +9,14 @@
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 3/12/20 3:19 PM
* @date 5/2/20 11:41 PM
**/
@Slf4j
@Component
public class Constants {
public Constants(ProjectProperty projectProperty) {
Constants.REDIS_JWT_KEY_PREFIX = projectProperty.getProjectArtifactId() + ":jwt:";
REDIS_JWT_KEY_PREFIX = String.format("%s:jwt:", projectProperty.getParentArtifactId());
log.info("Initiated 'REDIS_JWT_KEY_PREFIX': {}", REDIS_JWT_KEY_PREFIX);
}

/**
Expand All @@ -29,4 +31,12 @@ public Constants(ProjectProperty projectProperty) {
* Prefix of JWT.
*/
public static final String JWT_PREFIX = "Bearer ";
/**
* Star sign
*/
public static final String ASTERISK = "*";
/**
* At sign
*/
public static final String AT_SIGN = "@";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.jmsoftware.authcenter.universal.configuration;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
* <h1>CustomConfiguration</h1>
* <p>Custom configurations which are written in .yml files, containing a variety of fragmentary configs. Such as,
* Druid login info, web security switch, web log and so on.</p>
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 2019-03-23 14:24
**/
@Data
@Component
@ConfigurationProperties(prefix = "custom.configuration")
public class CustomConfiguration {
/**
* <p>The username of super user who has no restriction to access any system&#39;s resources.</p>
* <p><strong>ATTENTION</strong>: The value of username of super user must be equal to the value that is
* persistent in database.</p>
*/
private String superUser;
/**
* Ignore URLs
*/
private IgnoredRequest ignoredRequest;
private String druidLoginName;
private String druidPassword;
/**
* <p>Web security feature switch. Default is false.</p>
* true - disable web security; false - enable web security.
*/
private Boolean webSecurityDisabled = false;
/**
* Web request log switch. Default is false.
* <p>
* true - disable web request log; false - enable web request log.
*/
private Boolean webRequestLogDisabled = false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.jmsoftware.authcenter.universal.configuration;

import com.google.common.collect.Lists;
import lombok.Data;

import java.util.List;

/**
* <h1>IgnoredRequest</h1>
* <p>
* Ignored request configuration.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 5/2/20 11:41 PM
**/
@Data
public class IgnoredRequest {
/**
* Ignored URL pattern.
*/
private List<String> pattern = Lists.newArrayList();
/**
* Ignored GET request.
*/
private List<String> get = Lists.newArrayList();
/**
* Ignored POST request.
*/
private List<String> post = Lists.newArrayList();
/**
* Ignored DELETE request.
*/
private List<String> delete = Lists.newArrayList();
/**
* Ignored PUT request.
*/
private List<String> put = Lists.newArrayList();
/**
* Ignored HEAD request.
*/
private List<String> head = Lists.newArrayList();
/**
* Ignored PATCH request.
*/
private List<String> patch = Lists.newArrayList();
/**
* Ignored OPTIONS request.
*/
private List<String> options = Lists.newArrayList();
/**
* Ignored TRACE request.
*/
private List<String> trace = Lists.newArrayList();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
* <h1>JwtConfiguration</h1>
* <p>
* JWT configuration
* Ignored request configuration.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 3/12/20 3:03 PM
* @date 5/2/20 11:41 PM
**/
@Data
@Slf4j
@Component
@ConfigurationProperties(prefix = "jwt.configuration")
public class JwtConfiguration {
public JwtConfiguration(ProjectProperty projectProperty) {
this.signingKey = projectProperty.getProjectArtifactId();
log.error("JWT signing key: {}", this.signingKey);
this.signingKey = String.format("%s %s", projectProperty.getParentArtifactId(), projectProperty.getVersion());
log.info("Initiated JWT signing key: {}. The specified key byte array is {} bits", this.signingKey,
this.signingKey.getBytes(StandardCharsets.UTF_8).length * 8);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.jmsoftware.authcenter.universal.configuration;

import com.jcraft.jsch.ChannelSftp;
import lombok.Data;
import lombok.val;
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
@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() {
val 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
val cachingSessionFactory = new CachingSessionFactory<>(factory, sessionCacheSize);
cachingSessionFactory.setSessionWaitTimeout(sessionWaitTimeout);
return cachingSessionFactory;
}

@Bean
@ServiceActivator(inputChannel = "toSftpChannel")
@SuppressWarnings("UnresolvedMessageChannel")
public MessageHandler handler(SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory) {
val 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,56 @@
package com.jmsoftware.authcenter.universal.configuration;

import com.jmsoftware.authcenter.universal.domain.SftpSubDirectory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
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 (val sftpSubDirectory : SftpSubDirectory.values()) {
val 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.");
}
}
Loading

0 comments on commit 9b07500

Please sign in to comment.