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
jinli gu
Eladmin
Commits
d752b3d9
Commit
d752b3d9
authored
Nov 22, 2020
by
ZhengJie
Browse files
[代码优化](v2.6):修复服务监控磁盘统计不准确的问题,修复本机IP获取不准确的问题
close
https://github.com/elunez/eladmin/issues/527
parent
41cd9305
Changes
3
Hide whitespace changes
Inline
Side-by-side
eladmin-common/src/main/java/me/zhengjie/utils/StringUtils.java
View file @
d752b3d9
...
...
@@ -29,10 +29,13 @@ import org.springframework.core.io.ClassPathResource;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.File
;
import
java.net.Inet4Address
;
import
java.net.InetAddress
;
import
java.net.NetworkInterface
;
import
java.net.UnknownHostException
;
import
java.util.Calendar
;
import
java.util.Date
;
import
java.util.Enumeration
;
/**
* @author Zheng Jie
...
...
@@ -248,20 +251,37 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* @return /
*/
public
static
String
getLocalIp
()
{
InetAddress
addr
;
try
{
addr
=
InetAddress
.
getLocalHost
();
}
catch
(
UnknownHostException
e
)
{
return
"unknown"
;
}
byte
[]
ipAddr
=
addr
.
getAddress
();
StringBuilder
ipAddrStr
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
ipAddr
.
length
;
i
++)
{
if
(
i
>
0
)
{
ipAddrStr
.
append
(
"."
);
InetAddress
candidateAddress
=
null
;
// 遍历所有的网络接口
for
(
Enumeration
<
NetworkInterface
>
interfaces
=
NetworkInterface
.
getNetworkInterfaces
();
interfaces
.
hasMoreElements
();)
{
NetworkInterface
anInterface
=
interfaces
.
nextElement
();
// 在所有的接口下再遍历IP
for
(
Enumeration
<
InetAddress
>
inetAddresses
=
anInterface
.
getInetAddresses
();
inetAddresses
.
hasMoreElements
();)
{
InetAddress
inetAddr
=
inetAddresses
.
nextElement
();
// 排除loopback类型地址
if
(!
inetAddr
.
isLoopbackAddress
())
{
if
(
inetAddr
.
isSiteLocalAddress
())
{
// 如果是site-local地址,就是它了
return
inetAddr
.
getHostAddress
();
}
else
if
(
candidateAddress
==
null
)
{
// site-local类型的地址未被发现,先记录候选地址
candidateAddress
=
inetAddr
;
}
}
}
}
if
(
candidateAddress
!=
null
)
{
return
candidateAddress
.
getHostAddress
();
}
ipAddrStr
.
append
(
ipAddr
[
i
]
&
0xFF
);
// 如果没有发现 non-loopback地址.只能用最次选的方案
InetAddress
jdkSuppliedAddress
=
InetAddress
.
getLocalHost
();
if
(
jdkSuppliedAddress
==
null
)
{
return
""
;
}
return
jdkSuppliedAddress
.
getHostAddress
();
}
catch
(
Exception
e
)
{
return
""
;
}
return
ipAddrStr
.
toString
();
}
}
eladmin-system/src/main/java/me/zhengjie/modules/security/config/SecurityConfig.java
→
eladmin-system/src/main/java/me/zhengjie/modules/security/config/
Spring
SecurityConfig.java
View file @
d752b3d9
...
...
@@ -40,7 +40,6 @@ import org.springframework.web.filter.CorsFilter;
import
org.springframework.web.method.HandlerMethod
;
import
org.springframework.web.servlet.mvc.method.RequestMappingInfo
;
import
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
;
import
java.util.*
;
/**
...
...
@@ -50,7 +49,7 @@ import java.util.*;
@EnableWebSecurity
@RequiredArgsConstructor
@EnableGlobalMethodSecurity
(
prePostEnabled
=
true
,
securedEnabled
=
true
)
public
class
SecurityConfig
extends
WebSecurityConfigurerAdapter
{
public
class
Spring
SecurityConfig
extends
WebSecurityConfigurerAdapter
{
private
final
TokenProvider
tokenProvider
;
private
final
CorsFilter
corsFilter
;
...
...
@@ -138,6 +137,10 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.
and
().
apply
(
securityConfigurerAdapter
());
}
private
TokenConfigurer
securityConfigurerAdapter
()
{
return
new
TokenConfigurer
(
tokenProvider
,
properties
,
onlineUserService
,
userCacheClean
);
}
private
Map
<
String
,
Set
<
String
>>
getAnonymousUrl
(
Map
<
RequestMappingInfo
,
HandlerMethod
>
handlerMethodMap
)
{
Map
<
String
,
Set
<
String
>>
anonymousUrls
=
new
HashMap
<>(
6
);
Set
<
String
>
get
=
new
HashSet
<>();
...
...
@@ -182,8 +185,4 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
anonymousUrls
.
put
(
RequestMethodEnum
.
ALL
.
getType
(),
all
);
return
anonymousUrls
;
}
private
TokenConfigurer
securityConfigurerAdapter
()
{
return
new
TokenConfigurer
(
tokenProvider
,
properties
,
onlineUserService
,
userCacheClean
);
}
}
eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MonitorServiceImpl.java
View file @
d752b3d9
...
...
@@ -18,6 +18,7 @@ package me.zhengjie.modules.system.service.impl;
import
cn.hutool.core.date.BetweenFormater
;
import
cn.hutool.core.date.DateUtil
;
import
me.zhengjie.modules.system.service.MonitorService
;
import
me.zhengjie.utils.ElAdminConstant
;
import
me.zhengjie.utils.FileUtil
;
import
me.zhengjie.utils.StringUtils
;
import
org.springframework.stereotype.Service
;
...
...
@@ -73,15 +74,24 @@ public class MonitorServiceImpl implements MonitorService {
Map
<
String
,
Object
>
diskInfo
=
new
LinkedHashMap
<>();
FileSystem
fileSystem
=
os
.
getFileSystem
();
List
<
OSFileStore
>
fsArray
=
fileSystem
.
getFileStores
();
String
osName
=
System
.
getProperty
(
"os.name"
);
long
available
=
0
,
total
=
0
;
for
(
OSFileStore
fs
:
fsArray
){
long
available
=
fs
.
getUsableSpace
();
long
total
=
fs
.
getTotalSpace
();
long
used
=
total
-
available
;
diskInfo
.
put
(
"total"
,
total
>
0
?
FileUtil
.
getSize
(
total
)
:
"?"
);
diskInfo
.
put
(
"available"
,
FileUtil
.
getSize
(
available
));
diskInfo
.
put
(
"used"
,
FileUtil
.
getSize
(
used
));
diskInfo
.
put
(
"usageRate"
,
df
.
format
(
used
/(
double
)
fs
.
getTotalSpace
()
*
100
));
// windows 需要将所有磁盘分区累加
if
(
osName
.
toLowerCase
().
startsWith
(
ElAdminConstant
.
WIN
))
{
available
+=
fs
.
getUsableSpace
();
total
+=
fs
.
getTotalSpace
();
}
else
{
available
=
fs
.
getUsableSpace
();
total
=
fs
.
getTotalSpace
();
break
;
}
}
long
used
=
total
-
available
;
diskInfo
.
put
(
"total"
,
total
>
0
?
FileUtil
.
getSize
(
total
)
:
"?"
);
diskInfo
.
put
(
"available"
,
FileUtil
.
getSize
(
available
));
diskInfo
.
put
(
"used"
,
FileUtil
.
getSize
(
used
));
diskInfo
.
put
(
"usageRate"
,
df
.
format
(
used
/(
double
)
total
*
100
));
return
diskInfo
;
}
...
...
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