Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
schbrain-parent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
framework
schbrain-parent
Commits
ad1f1c31
Commit
ad1f1c31
authored
Sep 01, 2023
by
liaozan
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish
parent
7e3fec07
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
44 additions
and
39 deletions
+44
-39
commons/web-common/src/main/java/com/schbrain/common/web/AuthenticationConfiguration.java
.../com/schbrain/common/web/AuthenticationConfiguration.java
+1
-1
commons/web-common/src/main/java/com/schbrain/common/web/CorsFilterConfiguration.java
...java/com/schbrain/common/web/CorsFilterConfiguration.java
+1
-1
commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionResolver.java
.../common/web/exception/DefaultGlobalExceptionResolver.java
+3
-10
commons/web-common/src/main/java/com/schbrain/common/web/result/ResponseBodyHandler.java
...a/com/schbrain/common/web/result/ResponseBodyHandler.java
+6
-6
commons/web-common/src/main/java/com/schbrain/common/web/servlet/ContentCachingRequest.java
...om/schbrain/common/web/servlet/ContentCachingRequest.java
+8
-3
commons/web-common/src/main/java/com/schbrain/common/web/support/LongToStringSerializer.java
...m/schbrain/common/web/support/LongToStringSerializer.java
+3
-3
commons/web-common/src/main/java/com/schbrain/common/web/support/authentication/AbstractAuthenticator.java
...mon/web/support/authentication/AbstractAuthenticator.java
+4
-4
commons/web-common/src/main/java/com/schbrain/common/web/support/authentication/AuthenticationInterceptor.java
...web/support/authentication/AuthenticationInterceptor.java
+4
-5
commons/web-common/src/main/java/com/schbrain/common/web/utils/ExcelUtils.java
...c/main/java/com/schbrain/common/web/utils/ExcelUtils.java
+3
-3
commons/web-common/src/main/java/com/schbrain/common/web/utils/RequestContentCachingUtils.java
...schbrain/common/web/utils/RequestContentCachingUtils.java
+11
-3
No files found.
commons/web-common/src/main/java/com/schbrain/common/web/AuthenticationConfiguration.java
View file @
ad1f1c31
...
...
@@ -21,4 +21,4 @@ public class AuthenticationConfiguration {
return
new
AuthenticationInterceptor
(
authenticator
);
}
}
\ No newline at end of file
}
commons/web-common/src/main/java/com/schbrain/common/web/CorsFilterConfiguration.java
View file @
ad1f1c31
...
...
@@ -49,4 +49,4 @@ public class CorsFilterConfiguration {
return
registrationBean
;
}
}
\ No newline at end of file
}
commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionResolver.java
View file @
ad1f1c31
package
com.schbrain.common.web.exception
;
import
com.schbrain.common.web.annotation.ResponseWrapOption
;
import
com.schbrain.common.web.utils.HandlerMethodAnnotationUtils
;
import
lombok.EqualsAndHashCode
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.context.request.ServletWebRequest
;
...
...
@@ -24,6 +23,8 @@ import java.util.List;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
static
com
.
schbrain
.
common
.
web
.
utils
.
HandlerMethodAnnotationUtils
.
getAnnotation
;
/**
* @author liaozan
* @since 2022/8/30
...
...
@@ -33,13 +34,9 @@ import java.util.concurrent.ConcurrentHashMap;
public
class
DefaultGlobalExceptionResolver
extends
AbstractHandlerMethodExceptionResolver
{
private
final
GlobalExceptionHandler
exceptionHandler
;
private
final
ExceptionHandlerMethodResolver
handlerMethodResolver
;
private
final
HandlerMethodArgumentResolverComposite
argumentResolverComposite
;
private
final
HandlerMethodReturnValueHandlerComposite
returnValueHandlerComposite
;
private
final
Map
<
Class
<?>,
ExceptionHandlerMethodResolver
>
exceptionHandlerMethodResolvers
=
new
ConcurrentHashMap
<>(
64
);
public
DefaultGlobalExceptionResolver
(
ExceptionHandlerExceptionResolver
handlerMethodResolver
,
GlobalExceptionHandler
exceptionHandler
)
{
...
...
@@ -54,8 +51,7 @@ public class DefaultGlobalExceptionResolver extends AbstractHandlerMethodExcepti
protected
boolean
shouldApplyTo
(
HttpServletRequest
request
,
@Nullable
Object
handler
)
{
if
(
handler
instanceof
HandlerMethod
)
{
HandlerMethod
handlerMethod
=
(
HandlerMethod
)
handler
;
ResponseWrapOption
responseWrapOption
=
HandlerMethodAnnotationUtils
.
getAnnotation
(
handlerMethod
,
ResponseWrapOption
.
class
);
ResponseWrapOption
responseWrapOption
=
getAnnotation
(
handlerMethod
,
ResponseWrapOption
.
class
);
if
(
responseWrapOption
==
null
)
{
return
true
;
}
...
...
@@ -123,9 +119,6 @@ public class DefaultGlobalExceptionResolver extends AbstractHandlerMethodExcepti
return
exceptionHandlerMethodResolvers
.
computeIfAbsent
(
handlerType
,
key
->
new
ExceptionHandlerMethodResolver
(
handlerType
));
}
/**
* copy from spring
*/
private
Object
[]
getArguments
(
Exception
exception
,
HandlerMethod
handlerMethod
)
{
List
<
Throwable
>
exceptions
=
new
ArrayList
<>();
Throwable
exToExpose
=
exception
;
...
...
commons/web-common/src/main/java/com/schbrain/common/web/result/ResponseBodyHandler.java
View file @
ad1f1c31
...
...
@@ -14,6 +14,8 @@ import java.util.List;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
static
org
.
springframework
.
core
.
annotation
.
AnnotationUtils
.
getAnnotation
;
/**
* @author liaozan
* @since 2021/10/15
...
...
@@ -53,19 +55,17 @@ public class ResponseBodyHandler implements ResponseBodyAdvice<Object> {
return
false
;
}
Class
<?>
declaringClass
=
targetMethod
.
getDeclaringClass
();
ResponseWrapOption
responseWrapOption
=
targetMethod
.
getAnnotation
(
ResponseWrapOption
.
class
);
ResponseWrapOption
responseWrapOption
=
getAnnotation
(
targetMethod
,
ResponseWrapOption
.
class
);
if
(
responseWrapOption
==
null
)
{
responseWrapOption
=
declaringClass
.
getAnnotation
(
ResponseWrapOption
.
class
);
responseWrapOption
=
getAnnotation
(
targetMethod
.
getDeclaringClass
(),
ResponseWrapOption
.
class
);
}
if
(
responseWrapOption
!=
null
)
{
return
!
responseWrapOption
.
ignore
();
}
String
packageName
=
declaringClass
.
getPackage
().
getName
();
String
packageName
=
targetMethod
.
getDeclaringClass
()
.
getPackage
().
getName
();
return
basePackages
.
stream
().
anyMatch
(
packageName:
:
startsWith
);
}
}
\ No newline at end of file
}
commons/web-common/src/main/java/com/schbrain/common/web/servlet/ContentCachingRequest.java
View file @
ad1f1c31
...
...
@@ -18,7 +18,7 @@ public class ContentCachingRequest extends HttpServletRequestWrapper {
public
ContentCachingRequest
(
HttpServletRequest
request
)
{
super
(
request
);
this
.
inputStream
=
init
WrappedInputStream
(
request
);
this
.
inputStream
=
create
WrappedInputStream
(
request
);
}
@Override
...
...
@@ -27,7 +27,9 @@ public class ContentCachingRequest extends HttpServletRequestWrapper {
}
/**
* Return the cached request content as a String. The Charset used to decode the cached content is the same as returned by getCharacterEncoding.
* Return the cached request content as a String.
* <p>
* The Charset used to decode the cached content is the same as returned by getCharacterEncoding.
*/
public
String
getContentAsString
()
{
return
getContentAsString
(
getCharacterEncoding
());
...
...
@@ -40,7 +42,10 @@ public class ContentCachingRequest extends HttpServletRequestWrapper {
return
inputStream
.
getContentAsString
(
charset
);
}
private
WrappedByteArrayInputStream
initWrappedInputStream
(
HttpServletRequest
request
)
{
/**
* Wrap request inputStream to WrappedByteArrayInputStream
*/
private
WrappedByteArrayInputStream
createWrappedInputStream
(
HttpServletRequest
request
)
{
try
{
byte
[]
bytes
=
StreamUtils
.
copyToByteArray
(
request
.
getInputStream
());
return
new
WrappedByteArrayInputStream
(
bytes
);
...
...
commons/web-common/src/main/java/com/schbrain/common/web/support/LongToStringSerializer.java
View file @
ad1f1c31
...
...
@@ -22,11 +22,11 @@ public class LongToStringSerializer extends StdSerializer<Long> {
}
@Override
public
void
serialize
(
Long
value
,
JsonGenerator
gen
,
SerializerProvider
provider
)
throws
IOException
{
public
void
serialize
(
Long
value
,
JsonGenerator
gen
erator
,
SerializerProvider
provider
)
throws
IOException
{
if
(
value
.
doubleValue
()
>
FRONT_MAX_VALUE
)
{
gen
.
writeString
(
value
.
toString
());
gen
erator
.
writeString
(
value
.
toString
());
}
else
{
gen
.
writeNumber
(
value
);
gen
erator
.
writeNumber
(
value
);
}
}
...
...
commons/web-common/src/main/java/com/schbrain/common/web/support/authentication/AbstractAuthenticator.java
View file @
ad1f1c31
...
...
@@ -3,7 +3,6 @@ package com.schbrain.common.web.support.authentication;
import
cn.hutool.extra.spring.SpringUtil
;
import
com.schbrain.common.annotation.IgnoreLogin
;
import
com.schbrain.common.web.properties.WebProperties
;
import
com.schbrain.common.web.utils.HandlerMethodAnnotationUtils
;
import
lombok.Data
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
...
...
@@ -14,6 +13,8 @@ import javax.annotation.Nullable;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
static
com
.
schbrain
.
common
.
web
.
utils
.
HandlerMethodAnnotationUtils
.
hasAnnotation
;
/**
* @author liaozan
* @since 2022/11/12
...
...
@@ -35,8 +36,7 @@ public abstract class AbstractAuthenticator implements Authenticator {
@Override
public
boolean
validate
(
HttpServletRequest
request
,
HttpServletResponse
response
,
HandlerMethod
handlerMethod
)
{
boolean
ignore
=
HandlerMethodAnnotationUtils
.
hasAnnotation
(
handlerMethod
,
IgnoreLogin
.
class
);
if
(
ignore
)
{
if
(
hasAnnotation
(
handlerMethod
,
IgnoreLogin
.
class
))
{
return
true
;
}
String
authentication
=
getAuthentication
(
request
);
...
...
@@ -60,4 +60,4 @@ public abstract class AbstractAuthenticator implements Authenticator {
return
authentication
;
}
}
\ No newline at end of file
}
commons/web-common/src/main/java/com/schbrain/common/web/support/authentication/AuthenticationInterceptor.java
View file @
ad1f1c31
...
...
@@ -2,13 +2,13 @@ package com.schbrain.common.web.support.authentication;
import
cn.hutool.extra.servlet.ServletUtil
;
import
com.schbrain.common.util.JacksonUtils
;
import
com.schbrain.common.util.ValidateUtils
;
import
com.schbrain.common.web.result.ResponseDTO
;
import
com.schbrain.common.web.support.BaseHandlerInterceptor
;
import
lombok.Data
;
import
lombok.EqualsAndHashCode
;
import
org.springframework.core.Ordered
;
import
org.springframework.http.MediaType
;
import
org.springframework.util.Assert
;
import
org.springframework.web.method.HandlerMethod
;
import
javax.servlet.http.HttpServletRequest
;
...
...
@@ -28,7 +28,7 @@ public class AuthenticationInterceptor extends BaseHandlerInterceptor implements
private
Authenticator
authenticator
;
public
AuthenticationInterceptor
(
Authenticator
authenticator
)
{
Assert
.
notNull
(
authenticator
,
"authenticator must not be null"
);
ValidateUtils
.
notNull
(
authenticator
,
"authenticator must not be null"
);
this
.
authenticator
=
authenticator
;
}
...
...
@@ -39,8 +39,7 @@ public class AuthenticationInterceptor extends BaseHandlerInterceptor implements
@Override
protected
boolean
preHandle
(
HttpServletRequest
request
,
HttpServletResponse
response
,
HandlerMethod
handlerMethod
)
{
boolean
validated
=
authenticator
.
validate
(
request
,
response
,
handlerMethod
);
if
(
validated
)
{
if
(
authenticator
.
validate
(
request
,
response
,
handlerMethod
))
{
return
true
;
}
writeResult
(
response
,
buildAccessDeniedResponse
());
...
...
@@ -61,4 +60,4 @@ public class AuthenticationInterceptor extends BaseHandlerInterceptor implements
return
ResponseDTO
.
error
(
"未获取到认证信息, 请尝试重新登录"
,
LOGIN_REQUIRED
,
ALERT
);
}
}
\ No newline at end of file
}
commons/web-common/src/main/java/com/schbrain/common/web/utils/ExcelUtils.java
View file @
ad1f1c31
...
...
@@ -21,14 +21,14 @@ public class ExcelUtils extends com.schbrain.common.util.ExcelUtils {
public
static
<
T
>
void
writeToResponse
(
List
<
T
>
dataList
,
String
fileName
)
{
if
(
CollectionUtils
.
isEmpty
(
dataList
))
{
throw
new
ExcelException
(
"
D
ataList is empty"
);
throw
new
ExcelException
(
"
d
ataList is empty"
);
}
writeToResponse
(
dataList
,
dataList
.
get
(
0
).
getClass
(),
fileName
);
}
public
static
<
T
>
void
writeToResponse
(
List
<
T
>
dataList
,
Class
<?>
head
,
String
fileName
)
{
if
(
CollectionUtils
.
isEmpty
(
dataList
))
{
throw
new
ExcelException
(
"
D
ataList is empty"
);
throw
new
ExcelException
(
"
d
ataList is empty"
);
}
try
{
HttpServletResponse
response
=
ServletUtils
.
getResponse
();
...
...
@@ -48,4 +48,4 @@ public class ExcelUtils extends com.schbrain.common.util.ExcelUtils {
}
}
}
\ No newline at end of file
}
commons/web-common/src/main/java/com/schbrain/common/web/utils/RequestContentCachingUtils.java
View file @
ad1f1c31
package
com.schbrain.common.web.utils
;
import
com.schbrain.common.util.ValidateUtils
;
import
com.schbrain.common.web.servlet.ContentCachingRequest
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.util.Assert
;
import
javax.annotation.Nullable
;
import
javax.servlet.http.HttpServletRequest
;
...
...
@@ -20,7 +20,7 @@ public class RequestContentCachingUtils {
* Make request content cacheable to avoid stream closed error after inputStream closed
*/
public
static
ContentCachingRequest
wrapIfRequired
(
HttpServletRequest
request
)
{
Assert
.
notNull
(
request
,
"request must not be null"
);
ValidateUtils
.
notNull
(
request
,
"request must not be null"
);
if
(
request
instanceof
ContentCachingRequest
)
{
return
(
ContentCachingRequest
)
request
;
}
else
{
...
...
@@ -33,12 +33,20 @@ public class RequestContentCachingUtils {
*/
@Nullable
public
static
String
getRequestBody
(
HttpServletRequest
request
)
{
return
getRequestBody
(
request
,
request
.
getCharacterEncoding
());
}
/**
* Get request body content
*/
@Nullable
public
static
String
getRequestBody
(
HttpServletRequest
request
,
String
characterEncoding
)
{
ContentCachingRequest
requestToUse
=
getNativeRequest
(
request
,
ContentCachingRequest
.
class
);
if
(
requestToUse
==
null
)
{
log
.
warn
(
"request is not an instance of {}"
,
ContentCachingRequest
.
class
.
getSimpleName
());
return
null
;
}
return
requestToUse
.
getContentAsString
();
return
requestToUse
.
getContentAsString
(
characterEncoding
);
}
}
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