Commit 24986356 by hy

.

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