Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wwwanlingxiao
mall
Commits
ea0b1a0f
Commit
ea0b1a0f
authored
Apr 29, 2019
by
macro
Browse files
部分工具类实现改用hutool工具包
parent
0e347c3e
Changes
8
Show whitespace changes
Inline
Side-by-side
mall-admin/src/main/java/com/macro/mall/component/RestAuthenticationEntryPoint.java
View file @
ea0b1a0f
package
com.macro.mall.component
;
package
com.macro.mall.component
;
import
cn.hutool.json.JSONUtil
;
import
com.macro.mall.common.api.CommonResult
;
import
com.macro.mall.common.api.CommonResult
;
import
com.macro.mall.util.JsonUtil
;
import
org.springframework.security.core.AuthenticationException
;
import
org.springframework.security.core.AuthenticationException
;
import
org.springframework.security.web.AuthenticationEntryPoint
;
import
org.springframework.security.web.AuthenticationEntryPoint
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
...
@@ -21,7 +21,7 @@ public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
...
@@ -21,7 +21,7 @@ public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
public
void
commence
(
HttpServletRequest
request
,
HttpServletResponse
response
,
AuthenticationException
authException
)
throws
IOException
,
ServletException
{
public
void
commence
(
HttpServletRequest
request
,
HttpServletResponse
response
,
AuthenticationException
authException
)
throws
IOException
,
ServletException
{
response
.
setCharacterEncoding
(
"UTF-8"
);
response
.
setCharacterEncoding
(
"UTF-8"
);
response
.
setContentType
(
"application/json"
);
response
.
setContentType
(
"application/json"
);
response
.
getWriter
().
println
(
J
son
Util
.
objectToJson
(
CommonResult
.
unauthorized
(
authException
.
getMessage
())));
response
.
getWriter
().
println
(
J
SON
Util
.
parse
(
CommonResult
.
unauthorized
(
authException
.
getMessage
())));
response
.
getWriter
().
flush
();
response
.
getWriter
().
flush
();
}
}
}
}
mall-admin/src/main/java/com/macro/mall/component/RestfulAccessDeniedHandler.java
View file @
ea0b1a0f
package
com.macro.mall.component
;
package
com.macro.mall.component
;
import
cn.hutool.json.JSONUtil
;
import
com.macro.mall.common.api.CommonResult
;
import
com.macro.mall.common.api.CommonResult
;
import
com.macro.mall.util.JsonUtil
;
import
org.springframework.security.access.AccessDeniedException
;
import
org.springframework.security.access.AccessDeniedException
;
import
org.springframework.security.web.access.AccessDeniedHandler
;
import
org.springframework.security.web.access.AccessDeniedHandler
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
...
@@ -23,7 +23,7 @@ public class RestfulAccessDeniedHandler implements AccessDeniedHandler{
...
@@ -23,7 +23,7 @@ public class RestfulAccessDeniedHandler implements AccessDeniedHandler{
AccessDeniedException
e
)
throws
IOException
,
ServletException
{
AccessDeniedException
e
)
throws
IOException
,
ServletException
{
response
.
setCharacterEncoding
(
"UTF-8"
);
response
.
setCharacterEncoding
(
"UTF-8"
);
response
.
setContentType
(
"application/json"
);
response
.
setContentType
(
"application/json"
);
response
.
getWriter
().
println
(
J
son
Util
.
objectToJson
(
CommonResult
.
forbidden
(
e
.
getMessage
())));
response
.
getWriter
().
println
(
J
SON
Util
.
parse
(
CommonResult
.
forbidden
(
e
.
getMessage
())));
response
.
getWriter
().
flush
();
response
.
getWriter
().
flush
();
}
}
}
}
mall-admin/src/main/java/com/macro/mall/component/WebLogAspect.java
View file @
ea0b1a0f
package
com.macro.mall.component
;
package
com.macro.mall.component
;
import
cn.hutool.core.util.StrUtil
;
import
cn.hutool.core.util.URLUtil
;
import
cn.hutool.json.JSON
;
import
cn.hutool.json.JSONUtil
;
import
com.macro.mall.bo.WebLog
;
import
com.macro.mall.bo.WebLog
;
import
com.macro.mall.util.JsonUtil
;
import
com.macro.mall.util.RequestUtil
;
import
io.swagger.annotations.ApiOperation
;
import
io.swagger.annotations.ApiOperation
;
import
net.logstash.logback.marker.Markers
;
import
net.logstash.logback.marker.Markers
;
import
org.aspectj.lang.JoinPoint
;
import
org.aspectj.lang.JoinPoint
;
...
@@ -68,7 +70,8 @@ public class WebLogAspect {
...
@@ -68,7 +70,8 @@ public class WebLogAspect {
webLog
.
setDescription
(
log
.
value
());
webLog
.
setDescription
(
log
.
value
());
}
}
long
endTime
=
System
.
currentTimeMillis
();
long
endTime
=
System
.
currentTimeMillis
();
webLog
.
setBasePath
(
RequestUtil
.
getBasePath
(
request
));
String
urlStr
=
request
.
getRequestURL
().
toString
();
webLog
.
setBasePath
(
StrUtil
.
removeSuffix
(
urlStr
,
URLUtil
.
url
(
urlStr
).
getPath
()));
webLog
.
setIp
(
request
.
getRemoteUser
());
webLog
.
setIp
(
request
.
getRemoteUser
());
webLog
.
setMethod
(
request
.
getMethod
());
webLog
.
setMethod
(
request
.
getMethod
());
webLog
.
setParameter
(
getParameter
(
method
,
joinPoint
.
getArgs
()));
webLog
.
setParameter
(
getParameter
(
method
,
joinPoint
.
getArgs
()));
...
@@ -83,8 +86,8 @@ public class WebLogAspect {
...
@@ -83,8 +86,8 @@ public class WebLogAspect {
logMap
.
put
(
"parameter"
,
webLog
.
getParameter
());
logMap
.
put
(
"parameter"
,
webLog
.
getParameter
());
logMap
.
put
(
"spendTime"
,
webLog
.
getSpendTime
());
logMap
.
put
(
"spendTime"
,
webLog
.
getSpendTime
());
logMap
.
put
(
"description"
,
webLog
.
getDescription
());
logMap
.
put
(
"description"
,
webLog
.
getDescription
());
// LOGGER.info("{}", J
son
Util.
objectToJson
(webLog));
// LOGGER.info("{}", J
SON
Util.
parse
(webLog));
LOGGER
.
info
(
Markers
.
appendEntries
(
logMap
),
JsonUtil
.
objectToJson
(
webLog
));
LOGGER
.
info
(
Markers
.
appendEntries
(
logMap
),
JSONUtil
.
parse
(
webLog
).
toString
(
));
return
result
;
return
result
;
}
}
...
...
mall-admin/src/main/java/com/macro/mall/service/impl/OssServiceImpl.java
View file @
ea0b1a0f
package
com.macro.mall.service.impl
;
package
com.macro.mall.service.impl
;
import
cn.hutool.json.JSONUtil
;
import
com.aliyun.oss.OSSClient
;
import
com.aliyun.oss.OSSClient
;
import
com.aliyun.oss.common.utils.BinaryUtil
;
import
com.aliyun.oss.common.utils.BinaryUtil
;
import
com.aliyun.oss.model.MatchMode
;
import
com.aliyun.oss.model.MatchMode
;
import
com.aliyun.oss.model.PolicyConditions
;
import
com.aliyun.oss.model.PolicyConditions
;
import
com.macro.mall.dto.OssCallbackParam
;
import
com.macro.mall.dto.OssCallbackResult
;
import
com.macro.mall.dto.OssCallbackResult
;
import
com.macro.mall.dto.OssPolicyResult
;
import
com.macro.mall.dto.OssPolicyResult
;
import
com.macro.mall.service.OssService
;
import
com.macro.mall.service.OssService
;
...
@@ -69,7 +71,7 @@ public class OssServiceImpl implements OssService {
...
@@ -69,7 +71,7 @@ public class OssServiceImpl implements OssService {
byte
[]
binaryData
=
postPolicy
.
getBytes
(
"utf-8"
);
byte
[]
binaryData
=
postPolicy
.
getBytes
(
"utf-8"
);
String
policy
=
BinaryUtil
.
toBase64String
(
binaryData
);
String
policy
=
BinaryUtil
.
toBase64String
(
binaryData
);
String
signature
=
ossClient
.
calculatePostSignature
(
postPolicy
);
String
signature
=
ossClient
.
calculatePostSignature
(
postPolicy
);
// String callbackData = BinaryUtil.toBase64String(J
son
Util.
objectToJson(callback
).getBytes("utf-8"));
// String callbackData = BinaryUtil.toBase64String(J
SON
Util.
parse(callback).toString(
).getBytes("utf-8"));
// 返回结果
// 返回结果
result
.
setAccessKeyId
(
ossClient
.
getCredentialsProvider
().
getCredentials
().
getAccessKeyId
());
result
.
setAccessKeyId
(
ossClient
.
getCredentialsProvider
().
getCredentials
().
getAccessKeyId
());
result
.
setPolicy
(
policy
);
result
.
setPolicy
(
policy
);
...
...
mall-admin/src/main/java/com/macro/mall/util/JsonUtil.java
deleted
100644 → 0
View file @
0e347c3e
package
com.macro.mall.util
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.JavaType
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
java.util.List
;
/**
* Jackson json序列化和反序列化工具类
* Created by macro on 2018/4/26.
*/
public
class
JsonUtil
{
// 定义jackson对象
private
static
final
ObjectMapper
MAPPER
=
new
ObjectMapper
();
/**
* 将对象转换成json字符串。
*/
public
static
String
objectToJson
(
Object
data
)
{
try
{
String
string
=
MAPPER
.
writeValueAsString
(
data
);
return
string
;
}
catch
(
JsonProcessingException
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* 将json结果集转化为对象
*
* @param jsonData json数据
* @param beanType 对象中的object类型
*/
public
static
<
T
>
T
jsonToPojo
(
String
jsonData
,
Class
<
T
>
beanType
)
{
try
{
T
t
=
MAPPER
.
readValue
(
jsonData
,
beanType
);
return
t
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* 将json数据转换成pojo对象list
*/
public
static
<
T
>
List
<
T
>
jsonToList
(
String
jsonData
,
Class
<
T
>
beanType
)
{
JavaType
javaType
=
MAPPER
.
getTypeFactory
().
constructParametricType
(
List
.
class
,
beanType
);
try
{
List
<
T
>
list
=
MAPPER
.
readValue
(
jsonData
,
javaType
);
return
list
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
}
mall-admin/src/main/java/com/macro/mall/util/RequestUtil.java
deleted
100644 → 0
View file @
0e347c3e
package
com.macro.mall.util
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.util.Enumeration
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* request工具类
* Created by macro on 2018/4/26.
*/
public
class
RequestUtil
{
/**
* 移除request指定参数
*/
public
String
removeParam
(
HttpServletRequest
request
,
String
paramName
)
{
String
queryString
=
""
;
Enumeration
keys
=
request
.
getParameterNames
();
while
(
keys
.
hasMoreElements
())
{
String
key
=
(
String
)
keys
.
nextElement
();
if
(
key
.
equals
(
paramName
))
{
continue
;
}
if
(
""
.
equals
(
queryString
))
{
queryString
=
key
+
"="
+
request
.
getParameter
(
key
);
}
else
{
queryString
+=
"&"
+
key
+
"="
+
request
.
getParameter
(
key
);
}
}
return
queryString
;
}
/**
* 获取请求basePath
*/
public
static
String
getBasePath
(
HttpServletRequest
request
)
{
StringBuffer
basePath
=
new
StringBuffer
();
String
scheme
=
request
.
getScheme
();
String
domain
=
request
.
getServerName
();
int
port
=
request
.
getServerPort
();
basePath
.
append
(
scheme
);
basePath
.
append
(
"://"
);
basePath
.
append
(
domain
);
if
(
"http"
.
equalsIgnoreCase
(
scheme
)
&&
80
!=
port
)
{
basePath
.
append
(
":"
).
append
(
String
.
valueOf
(
port
));
}
else
if
(
"https"
.
equalsIgnoreCase
(
scheme
)
&&
port
!=
443
)
{
basePath
.
append
(
":"
).
append
(
String
.
valueOf
(
port
));
}
return
basePath
.
toString
();
}
/**
* 请求中参数转Map<String, String>,for支付宝异步回调,平时建议直接使用request.getParameterMap(),返回Map<String, String[]>
*/
public
static
Map
<
String
,
String
>
getParameterMap
(
HttpServletRequest
request
)
{
Map
<
String
,
String
>
result
=
new
HashMap
<>();
Enumeration
parameterNames
=
request
.
getParameterNames
();
while
(
parameterNames
.
hasMoreElements
())
{
String
parameterName
=
(
String
)
parameterNames
.
nextElement
();
result
.
put
(
parameterName
,
request
.
getParameter
(
parameterName
));
}
return
result
;
}
}
mall-admin/src/test/com/macro/mall/PmsDaoTests.java
View file @
ea0b1a0f
package
com.macro.mall
;
package
com.macro.mall
;
import
cn.hutool.json.JSONUtil
;
import
com.macro.mall.dao.PmsMemberPriceDao
;
import
com.macro.mall.dao.PmsMemberPriceDao
;
import
com.macro.mall.dao.PmsProductDao
;
import
com.macro.mall.dao.PmsProductDao
;
import
com.macro.mall.dto.PmsProductResult
;
import
com.macro.mall.dto.PmsProductResult
;
import
com.macro.mall.model.PmsMemberPrice
;
import
com.macro.mall.model.PmsMemberPrice
;
import
com.macro.mall.util.JsonUtil
;
import
org.junit.Assert
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
...
@@ -48,7 +48,7 @@ public class PmsDaoTests {
...
@@ -48,7 +48,7 @@ public class PmsDaoTests {
@Test
@Test
public
void
testGetProductUpdateInfo
(){
public
void
testGetProductUpdateInfo
(){
PmsProductResult
productResult
=
productDao
.
getUpdateInfo
(
7L
);
PmsProductResult
productResult
=
productDao
.
getUpdateInfo
(
7L
);
String
json
=
J
son
Util
.
objectToJson
(
productResult
);
String
json
=
J
SON
Util
.
parse
(
productResult
)
.
toString
()
;
LOGGER
.
info
(
json
);
LOGGER
.
info
(
json
);
}
}
}
}
mall-common/pom.xml
View file @
ea0b1a0f
...
@@ -33,6 +33,12 @@
...
@@ -33,6 +33,12 @@
<artifactId>
spring-data-commons
</artifactId>
<artifactId>
spring-data-commons
</artifactId>
<version>
2.1.5.RELEASE
</version>
<version>
2.1.5.RELEASE
</version>
</dependency>
</dependency>
<!--Hutool Java工具包-->
<dependency>
<groupId>
cn.hutool
</groupId>
<artifactId>
hutool-all
</artifactId>
<version>
4.5.7
</version>
</dependency>
</dependencies>
</dependencies>
</project>
</project>
\ No newline at end of file
Write
Preview
Supports
Markdown
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