Commit 24986356 by hy

.

parent fb3a3f77
package com.tbyf.license; package com.tbyf.license;
import com.tbyf.license.autoconfigure.LicenseProperties;
import global.namespace.fun.io.bios.BIOS; import global.namespace.fun.io.bios.BIOS;
import global.namespace.truelicense.api.ConsumerLicenseManager; import global.namespace.truelicense.api.ConsumerLicenseManager;
import global.namespace.truelicense.api.License; import global.namespace.truelicense.api.License;
...@@ -15,6 +16,9 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -15,6 +16,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
public class LicenseManager implements InitializingBean { public class LicenseManager implements InitializingBean {
...@@ -34,7 +38,13 @@ public class LicenseManager implements InitializingBean { ...@@ -34,7 +38,13 @@ public class LicenseManager implements InitializingBean {
LicenseProperties licenseProperties; LicenseProperties licenseProperties;
private static void verifyMAC(Object MAC) throws LicenseValidationException { private static void verifyMAC(Object MAC) throws LicenseValidationException {
if (MAC == null || !NetUtil.containsMAC(MAC.toString())) { if (MAC == null ) {
throw new LicenseValidationException(Messages.message("Invalid License"));
}
Set<String> MACs = Arrays.stream(MAC.toString().split(","))
.map(String::trim)
.collect(Collectors.toSet());
if (!NetUtil.containsAnyMAC(MACs)) {
throw new LicenseValidationException(Messages.message("Invalid License")); throw new LicenseValidationException(Messages.message("Invalid License"));
} }
} }
...@@ -71,9 +81,13 @@ public class LicenseManager implements InitializingBean { ...@@ -71,9 +81,13 @@ public class LicenseManager implements InitializingBean {
manager().install(BIOS.file(licensePath)); manager().install(BIOS.file(licensePath));
} }
public void uninstall() throws Exception { public void uninstall() {
try {
manager().uninstall(); manager().uninstall();
} }
catch (Exception ignored) {
}
}
public LicenseValidPeriod getLicenseValidPeriod() throws LicenseManagementException { public LicenseValidPeriod getLicenseValidPeriod() throws LicenseManagementException {
License license = manager().load(); License license = manager().load();
...@@ -89,6 +103,7 @@ public class LicenseManager implements InitializingBean { ...@@ -89,6 +103,7 @@ public class LicenseManager implements InitializingBean {
Files.createDirectories(dir); Files.createDirectories(dir);
Path licensePath = dir.resolve(Constants.LICENSE_FILE_NAME); Path licensePath = dir.resolve(Constants.LICENSE_FILE_NAME);
if (Files.isRegularFile(licensePath) && Files.exists(licensePath)) { if (Files.isRegularFile(licensePath) && Files.exists(licensePath)) {
uninstall();
install(licensePath.toString()); install(licensePath.toString());
} }
} }
......
...@@ -15,6 +15,10 @@ public class NetUtil { ...@@ -15,6 +15,10 @@ public class NetUtil {
return getAllMACs().contains(MAC.toUpperCase()); return getAllMACs().contains(MAC.toUpperCase());
} }
public static boolean containsAnyMAC(Set<String> MACs) {
return getAllMACs().stream().anyMatch(MACs::contains);
}
private static Set<String> getAllMACs() { private static Set<String> getAllMACs() {
Set<String> allMACs = cache; Set<String> allMACs = cache;
if (allMACs != null && !allMACs.isEmpty()) { if (allMACs != null && !allMACs.isEmpty()) {
......
package com.tbyf.license; package com.tbyf.license.autoconfigure;
import com.tbyf.license.LicenseManager;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
...@@ -14,6 +15,7 @@ import javax.servlet.MultipartConfigElement; ...@@ -14,6 +15,7 @@ import javax.servlet.MultipartConfigElement;
@EnableConfigurationProperties(LicenseProperties.class) @EnableConfigurationProperties(LicenseProperties.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@AutoConfiguration(after = {DispatcherServletAutoConfiguration.class}) @AutoConfiguration(after = {DispatcherServletAutoConfiguration.class})
// @ConditionalOnProperty(prefix = "license", name = "enabled", havingValue = "true")
public class LicenseAutoConfiguration { public class LicenseAutoConfiguration {
@Bean @Bean
......
package com.tbyf.license; package com.tbyf.license.autoconfigure;
import com.tbyf.license.Constants;
import com.tbyf.license.LicenseManager;
import lombok.Setter; import lombok.Setter;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
...@@ -31,6 +33,8 @@ public class LicenseInstallationServlet extends HttpServlet { ...@@ -31,6 +33,8 @@ public class LicenseInstallationServlet extends HttpServlet {
try (InputStream is = filePart.getInputStream()) { try (InputStream is = filePart.getInputStream()) {
Path dest = Paths.get(properties.getLocation(), Constants.LICENSE_FILE_NAME).toAbsolutePath().normalize(); Path dest = Paths.get(properties.getLocation(), Constants.LICENSE_FILE_NAME).toAbsolutePath().normalize();
Files.copy(is, dest, StandardCopyOption.REPLACE_EXISTING); Files.copy(is, dest, StandardCopyOption.REPLACE_EXISTING);
this.licenseManager.uninstall();
this.licenseManager.install(dest.toString()); this.licenseManager.install(dest.toString());
resp.setStatus(HttpStatus.OK.value()); resp.setStatus(HttpStatus.OK.value());
resp.setContentType(Constants.TEXT_PLAIN_MEDIA_TYPE); resp.setContentType(Constants.TEXT_PLAIN_MEDIA_TYPE);
...@@ -43,5 +47,4 @@ public class LicenseInstallationServlet extends HttpServlet { ...@@ -43,5 +47,4 @@ public class LicenseInstallationServlet extends HttpServlet {
} }
} }
} }
package com.tbyf.license; package com.tbyf.license.autoconfigure;
import lombok.Data; import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
......
package com.tbyf.license; package com.tbyf.license.autoconfigure;
import com.tbyf.license.LicenseManager;
import global.namespace.fun.io.api.NoContentException; import global.namespace.fun.io.api.NoContentException;
import global.namespace.truelicense.api.LicenseManagementException; import global.namespace.truelicense.api.LicenseManagementException;
import global.namespace.truelicense.api.LicenseValidationException; import global.namespace.truelicense.api.LicenseValidationException;
......
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tbyf.license.LicenseAutoConfiguration com.tbyf.license.autoconfigure.LicenseAutoConfiguration
\ No newline at end of file \ No newline at end of file
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