Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
license-spring-boot-starter
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
黄营
license-spring-boot-starter
Commits
5558cdf9
Commit
5558cdf9
authored
Jan 11, 2024
by
hy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
d1444de6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
135 additions
and
9 deletions
+135
-9
.gitignore
.gitignore
+3
-2
hip-expired.lic
hip-expired.lic
+0
-0
LicenseAutoConfiguration.java
src/main/java/com/tbyf/license/LicenseAutoConfiguration.java
+18
-4
LicenseInstallationServlet.java
...ain/java/com/tbyf/license/LicenseInstallationServlet.java
+47
-0
LicenseValidationFilter.java
src/main/java/com/tbyf/license/LicenseValidationFilter.java
+54
-3
MultipartConfig.java
src/main/java/com/tbyf/license/MultipartConfig.java
+13
-0
No files found.
.gitignore
View file @
5558cdf9
...
...
@@ -36,4 +36,5 @@ build/
### Mac OS ###
.DS_Store
.idea/
\ No newline at end of file
.idea/
*.lic
\ No newline at end of file
hip.lic
→
hip
-expired
.lic
View file @
5558cdf9
File moved
src/main/java/com/tbyf/license/LicenseAutoConfiguration.java
View file @
5558cdf9
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.co
ntext.annotation.Configuration
;
import
org.springframework.co
re.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
;
}
}
src/main/java/com/tbyf/license/LicenseInstallationServlet.java
0 → 100644
View file @
5558cdf9
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
(
"安装失败"
);
}
}
}
src/main/java/com/tbyf/license/LicenseValidationFilter.java
View file @
5558cdf9
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
[]{
0x3c29fe15c616ba98
L
,
0xf20dd79db1dad670
L
,
0x89664e7a93dd88d0
L
}).
toString
();
// /license/uninstall
private
final
static
String
URL2
=
new
ObfuscatedString
(
new
long
[]{
0xc26bb1827bac0fbf
L
,
0xcd9c362b393e579f
L
,
0xe69b7905e04224de
L
,
0x44e7c7c44c3b3c99
L
}).
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
(
LicenseValidation
Exception
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"
));
}
}
src/main/java/com/tbyf/license/MultipartConfig.java
0 → 100644
View file @
5558cdf9
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
{
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment