Commit 5558cdf9 by hy

.

parent d1444de6
......@@ -36,4 +36,5 @@ build/
### Mac OS ###
.DS_Store
.idea/
\ No newline at end of file
.idea/
*.lic
\ No newline at end of file
package com.tbyf.license;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
@Configuration
@ConditionalOnWebApplication
import javax.servlet.MultipartConfigElement;
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@AutoConfiguration(after = {DispatcherServletAutoConfiguration.class})
public class LicenseAutoConfiguration {
@Bean
......@@ -20,7 +25,16 @@ public class LicenseAutoConfiguration {
LicenseValidationFilter filter = new LicenseValidationFilter();
filter.setLicenseManager(licenseManager());
registrationBean.setFilter(filter);
registrationBean.addUrlPatterns("/**");
registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return registrationBean;
}
@Bean
public ServletRegistrationBean<LicenseInstallationServlet> licenseServlet() {
LicenseInstallationServlet servlet = new LicenseInstallationServlet();
servlet.setLicenseManager(licenseManager());
ServletRegistrationBean<LicenseInstallationServlet> registrationBean = new ServletRegistrationBean<>(servlet, "/license/install");
registrationBean.setMultipartConfig(new MultipartConfigElement("", 1024 * 1024, 1024 * 1024, 1024 * 1024));
return registrationBean;
}
}
package com.tbyf.license;
import lombok.Setter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class LicenseInstallationServlet extends HttpServlet {
@Setter
private LicenseManager licenseManager;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Part filePart = req.getPart("license");
String fileName = filePart.getSubmittedFileName();
try (InputStream is = filePart.getInputStream()) {
Path dest = Paths.get(fileName);
Files.copy(is, dest, StandardCopyOption.REPLACE_EXISTING);
this.licenseManager.install(dest.toString());
resp.setStatus(HttpStatus.OK.value());
resp.setContentType("text/plain;charset=UTF-8");
resp.getWriter().write("安装成功");
}
catch (Exception e) {
resp.setStatus(HttpStatus.BAD_REQUEST.value());
resp.setContentType("text/plain;charset=UTF-8");
resp.getWriter().write("安装失败");
}
}
}
package com.tbyf.license;
import global.namespace.fun.io.api.NoContentException;
import global.namespace.truelicense.api.LicenseManagementException;
import global.namespace.truelicense.api.LicenseValidationException;
import global.namespace.truelicense.obfuscate.ObfuscatedString;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
......@@ -12,11 +14,29 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class LicenseValidationFilter extends OncePerRequestFilter {
private LicenseManager licenseManager;
private final AntPathMatcher pathMatcher = new AntPathMatcher();
private static final List<String> whiteList = new ArrayList<>();
// /license/install
private final static String URL1 = new ObfuscatedString(new long[]{0x3c29fe15c616ba98L, 0xf20dd79db1dad670L, 0x89664e7a93dd88d0L}).toString();
// /license/uninstall
private final static String URL2 = new ObfuscatedString(new long[]{0xc26bb1827bac0fbfL, 0xcd9c362b393e579fL, 0xe69b7905e04224deL, 0x44e7c7c44c3b3c99L}).toString();
static {
whiteList.add(URL1);
whiteList.add(URL2);
}
// Invalid License
private final static String INVALID_LICENSE =
new ObfuscatedString(new long[]{
......@@ -37,13 +57,44 @@ public class LicenseValidationFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
if (isInWhiteList(request)) {
filterChain.doFilter(request, response);
return;
}
try {
this.licenseManager.verify();
filterChain.doFilter(request, response);
}
catch (Exception e) {
catch (LicenseValidationException e) {
response.setStatus(HttpStatus.FORBIDDEN.value());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.getWriter().write(INVALID_LICENSE);
}
catch (LicenseManagementException e) {
if (e.getCause() instanceof NoContentException) {
response.setStatus(HttpStatus.FORBIDDEN.value());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.getWriter().write(NO_LICENSE);
}
}
catch (Exception e) {
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
response.setContentType(MediaType.TEXT_PLAIN_VALUE);
response.getWriter().write("Internal Error");
}
}
private boolean isInWhiteList(HttpServletRequest request) {
for (String uri : whiteList) {
if (this.pathMatcher.match(uri, request.getRequestURI())) {
return true;
}
}
return false;
}
public static void main(String[] args) {
System.out.println(ObfuscatedString.obfuscate("/license/uninstall"));
}
}
package com.tbyf.license;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
public class MultipartConfig implements ServletContextInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment