Skip to content

Commit

Permalink
增加tls功能样例
Browse files Browse the repository at this point in the history
  • Loading branch information
ctlove0523 committed Mar 5, 2022
1 parent a4e4cff commit 4199be8
Show file tree
Hide file tree
Showing 14 changed files with 514 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# spring-samples
Samples of spring or spring boot



## Spring-boot-tls

该模块下的主要功能包括:Spring Boot配置证书提供HTTPS服务,Spring Boot监听多个端口,Spring Boot每个端口处理的资源支持配置。
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<module>spring-cloud-gateway-nacos</module>
<module>spring-boot-jwt</module>
<module>jpa-mysql</module>
<module>spring-boot-tls</module>
</modules>


Expand Down
30 changes: 30 additions & 0 deletions spring-boot-tls/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-samples</artifactId>
<groupId>io.ctlove0523.spring</groupId>
<version>0.0.1-SNAPSHO</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot-tls</artifactId>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.github.ctlove0523.tls;

import org.springframework.util.AntPathMatcher;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class AllowedFilter implements Filter {
private final Map<Integer, List<String>> patternMap = new HashMap<>();
private final AntPathMatcher antPathMatcher;

public AllowedFilter(List<ServerAllowedUrl> urls) {
this.antPathMatcher = new AntPathMatcher();
this.antPathMatcher.setCachePatterns(true);
this.antPathMatcher.setCaseSensitive(true);

if (Objects.isNull(urls)) {
urls = new ArrayList<>();
}

for (ServerAllowedUrl url : urls) {
int port = url.getPort();
if (url.getUrls() == null) {
patternMap.put(port, new ArrayList<>());
} else {
patternMap.put(port, url.getUrls());
}
}
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;

int serverPort = request.getServerPort();

if (!patternMap.containsKey(serverPort)) {
chain.doFilter(httpRequest, httpResponse);
return;
}

List<String> patterns = patternMap.get(serverPort);
for (String pattern : patterns) {
String uri = httpRequest.getRequestURI();
if (antPathMatcher.match(pattern, uri)) {
chain.doFilter(httpRequest, httpResponse);
return;
}

}

httpResponse.setStatus(401);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.github.ctlove0523.tls;

import java.util.List;

public class ConnectorConfigure {
private int port;
private String scheme;
private boolean sslEnabled;
private String keystoreFile;
private String KeystorePass;
private String keyAlias;
private List<String> allowedUrls;

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public String getScheme() {
return scheme;
}

public void setScheme(String scheme) {
this.scheme = scheme;
}

public boolean isSslEnabled() {
return sslEnabled;
}

public void setSslEnabled(boolean sslEnabled) {
this.sslEnabled = sslEnabled;
}

public String getKeystoreFile() {
return keystoreFile;
}

public void setKeystoreFile(String keystoreFile) {
this.keystoreFile = keystoreFile;
}

public String getKeystorePass() {
return KeystorePass;
}

public void setKeystorePass(String keystorePass) {
KeystorePass = keystorePass;
}

public String getKeyAlias() {
return keyAlias;
}

public void setKeyAlias(String keyAlias) {
this.keyAlias = keyAlias;
}

public List<String> getAllowedUrls() {
return allowedUrls;
}

public void setAllowedUrls(List<String> allowedUrls) {
this.allowedUrls = allowedUrls;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.ctlove0523.tls;

import java.util.List;

public interface ConnectorConfigureRepository {

List<ConnectorConfigure> load();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.ctlove0523.tls;

import java.util.List;

public class ServerAllowedUrl {
private int port;
private List<String> urls;

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public List<String> getUrls() {
return urls;
}

public void setUrls(List<String> urls) {
this.urls = urls;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package io.github.ctlove0523.tls;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.UUID;

@Controller
public class TestController {
public static void main(String[] args) {
AntPathMatcher antPathMatcher = new AntPathMatcher();
boolean result= antPathMatcher.match("/**", "/api/hello");

System.out.println(result);
}

@RequestMapping(value = "/api/apps/{appId}", method = RequestMethod.GET)
public ResponseEntity<App> showApp(@PathVariable(name = "appId") String appId) {
App app = new App();
app.setId(appId);
app.setName("hello app");
return ResponseEntity.ok(app);
}

@RequestMapping(value = "/api/health", method = RequestMethod.GET)
public ResponseEntity<String> healthCheck() {
return ResponseEntity.ok("health");
}

@RequestMapping(value = "/api/users", method = RequestMethod.GET)
public ResponseEntity<User> showUser() {
User user = new User();
user.setId(UUID.randomUUID().toString());
user.setName("hello app");
return ResponseEntity.ok(user);
}
}

class User {
private String id;
private String name;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
class App {
private String id;
private String name;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.ctlove0523.tls;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAutoConfiguration
public class TlsServer {
public static void main(String[] args) {
SpringApplication.run(TlsServer.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.github.ctlove0523.tls;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Component
public class YamlConnectorConfigureRepository implements ConnectorConfigureRepository {

@Autowired
private ResourceLoader resourceLoader;

@SuppressWarnings("unchecked")
public List<ConnectorConfigure> load() {
List<ConnectorConfigure> connectorConfigures = new ArrayList<>();
Yaml yaml = new Yaml();
Resource resource = resourceLoader.getResource("classpath:application.yaml");
try {
Map<String, Object> applicationConfig = yaml.load(resource.getInputStream());
List<Map<String, Object>> configs = (List<Map<String, Object>>) applicationConfig.get("servers");
for (Map<String, Object> config : configs) {
ConnectorConfigure connectorConfigure = createConnectorConfigure(config);
connectorConfigures.add(connectorConfigure);
}

return connectorConfigures;
} catch (IOException e) {
e.printStackTrace();
}

return new ArrayList<>();
}

private ConnectorConfigure createConnectorConfigure(Map<String, Object> config) {
ConnectorConfigure connectorConfigure = new ConnectorConfigure();
connectorConfigure.setScheme((String) config.get("scheme"));
connectorConfigure.setPort((int) config.get("port"));

if (Objects.nonNull(config.get("sslEnabled"))) {
connectorConfigure.setSslEnabled((boolean) config.get("sslEnabled"));
}
if (Objects.nonNull(config.get("keystoreFile"))) {
connectorConfigure.setKeystoreFile((String) config.get("keystoreFile"));
}
if (Objects.nonNull(config.get("keystorePass"))) {
connectorConfigure.setKeystorePass(config.get("keystorePass").toString());
}

if (Objects.nonNull(config.get("keyAlias"))) {
connectorConfigure.setKeyAlias((String) config.get("keyAlias"));
}

if (Objects.nonNull(config.get("allowedUrls"))) {
connectorConfigure.setAllowedUrls((List<String>) config.get("allowedUrls"));
}

return connectorConfigure;
}
}
Loading

0 comments on commit 4199be8

Please sign in to comment.