Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fgqyxxlr
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
yaru
fgqyxxlr
Commits
e93e0c4c
Commit
e93e0c4c
authored
Aug 15, 2018
by
RuoYi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
format
parent
f9c82e1f
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
752 additions
and
754 deletions
+752
-754
src/main/java/com/ruoyi/common/utils/SystemLogUtils.java
+0
-69
src/main/java/com/ruoyi/framework/aspectj/LogAspect.java
+140
-126
src/main/java/com/ruoyi/framework/manager/AsyncManager.java
+31
-14
src/main/java/com/ruoyi/framework/manager/factory/AsyncFactory.java
+103
-94
src/main/java/com/ruoyi/framework/shiro/service/LoginService.java
+99
-97
src/main/java/com/ruoyi/framework/shiro/service/PasswordService.java
+66
-65
src/main/java/com/ruoyi/framework/shiro/session/OnlineSessionDAO.java
+87
-78
src/main/java/com/ruoyi/framework/shiro/web/filter/LogoutFilter.java
+59
-50
src/main/java/com/ruoyi/project/monitor/online/domain/UserOnline.java
+167
-161
No files found.
src/main/java/com/ruoyi/common/utils/SystemLogUtils.java
deleted
100644 → 0
View file @
f9c82e1f
package
com
.
ruoyi
.
common
.
utils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.ruoyi.common.constant.Constants
;
import
com.ruoyi.common.utils.security.ShiroUtils
;
import
com.ruoyi.common.utils.spring.SpringUtils
;
import
com.ruoyi.project.monitor.logininfor.domain.Logininfor
;
import
com.ruoyi.project.monitor.logininfor.service.LogininforServiceImpl
;
import
eu.bitwalker.useragentutils.UserAgent
;
/**
* 记录用户日志信息
*
* @author ruoyi
*/
@Deprecated
// 加入异步功能之后,该类已无意义
public
class
SystemLogUtils
{
private
static
final
Logger
sys_user_logger
=
LoggerFactory
.
getLogger
(
"sys-user"
);
/**
* 记录格式 [ip][用户名][操作][错误消息]
* <p/>
* 注意操作如下: loginError 登录失败 loginSuccess 登录成功 passwordError 密码错误
* changePassword 修改密码 changeStatus 修改状态
*
* @param username
* @param op
* @param msg
* @param args
*/
public
static
void
log
(
String
username
,
String
status
,
String
msg
,
Object
...
args
)
{
StringBuilder
s
=
new
StringBuilder
();
s
.
append
(
LogUtils
.
getBlock
(
ShiroUtils
.
getIp
()));
s
.
append
(
AddressUtils
.
getRealAddressByIP
(
ShiroUtils
.
getIp
()));
s
.
append
(
LogUtils
.
getBlock
(
username
));
s
.
append
(
LogUtils
.
getBlock
(
status
));
s
.
append
(
LogUtils
.
getBlock
(
msg
));
sys_user_logger
.
info
(
s
.
toString
(),
args
);
if
(
Constants
.
LOGIN_SUCCESS
.
equals
(
status
)
||
Constants
.
LOGOUT
.
equals
(
status
))
{
saveOpLog
(
username
,
msg
,
Constants
.
SUCCESS
);
}
else
if
(
Constants
.
LOGIN_FAIL
.
equals
(
status
))
{
saveOpLog
(
username
,
msg
,
Constants
.
FAIL
);
}
}
public
static
void
saveOpLog
(
String
username
,
String
message
,
String
status
)
{
UserAgent
userAgent
=
UserAgent
.
parseUserAgentString
(
ServletUtils
.
getRequest
().
getHeader
(
"User-Agent"
));
// 获取客户端操作系统
String
os
=
userAgent
.
getOperatingSystem
().
getName
();
// 获取客户端浏览器
String
browser
=
userAgent
.
getBrowser
().
getName
();
LogininforServiceImpl
logininforService
=
SpringUtils
.
getBean
(
LogininforServiceImpl
.
class
);
Logininfor
logininfor
=
new
Logininfor
();
logininfor
.
setLoginName
(
username
);
logininfor
.
setStatus
(
status
);
logininfor
.
setIpaddr
(
ShiroUtils
.
getIp
());
logininfor
.
setLoginLocation
(
AddressUtils
.
getRealAddressByIP
(
ShiroUtils
.
getIp
()));
logininfor
.
setBrowser
(
browser
);
logininfor
.
setOs
(
os
);
logininfor
.
setMsg
(
message
);
logininforService
.
insertLogininfor
(
logininfor
);
}
}
src/main/java/com/ruoyi/framework/aspectj/LogAspect.java
View file @
e93e0c4c
...
...
@@ -2,7 +2,6 @@ package com.ruoyi.framework.aspectj;
import
java.lang.reflect.Method
;
import
java.util.Map
;
import
org.aspectj.lang.JoinPoint
;
import
org.aspectj.lang.Signature
;
import
org.aspectj.lang.annotation.AfterReturning
;
...
...
@@ -15,7 +14,6 @@ import org.slf4j.LoggerFactory;
import
org.springframework.scheduling.annotation.Async
;
import
org.springframework.scheduling.annotation.EnableAsync
;
import
org.springframework.stereotype.Component
;
import
com.alibaba.fastjson.JSONObject
;
import
com.ruoyi.common.utils.ServletUtils
;
import
com.ruoyi.common.utils.StringUtils
;
...
...
@@ -35,128 +33,144 @@ import com.ruoyi.project.system.user.domain.User;
@Aspect
@Component
@EnableAsync
public
class
LogAspect
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
LogAspect
.
class
);
// 配置织入点
@Pointcut
(
"@annotation(com.ruoyi.framework.aspectj.lang.annotation.Log)"
)
public
void
logPointCut
()
{
}
/**
* 前置通知 用于拦截操作
*
* @param joinPoint
* 切点
*/
@AfterReturning
(
pointcut
=
"logPointCut()"
)
public
void
doBefore
(
JoinPoint
joinPoint
)
{
handleLog
(
joinPoint
,
null
);
}
/**
* 拦截异常操作
*
* @param joinPoint
* @param e
*/
@AfterThrowing
(
value
=
"logPointCut()"
,
throwing
=
"e"
)
public
void
doAfter
(
JoinPoint
joinPoint
,
Exception
e
)
{
handleLog
(
joinPoint
,
e
);
}
@Async
protected
void
handleLog
(
final
JoinPoint
joinPoint
,
final
Exception
e
)
{
try
{
// 获得注解
Log
controllerLog
=
getAnnotationLog
(
joinPoint
);
if
(
controllerLog
==
null
)
{
return
;
}
// 获取当前的用户
User
currentUser
=
ShiroUtils
.
getUser
();
// *========数据库日志=========*//
OperLog
operLog
=
new
OperLog
();
operLog
.
setStatus
(
BusinessStatus
.
SUCCESS
);
// 请求的地址
String
ip
=
ShiroUtils
.
getIp
();
operLog
.
setOperIp
(
ip
);
operLog
.
setOperUrl
(
ServletUtils
.
getRequest
().
getRequestURI
());
if
(
currentUser
!=
null
)
{
operLog
.
setOperName
(
currentUser
.
getLoginName
());
if
(
StringUtils
.
isNotNull
(
currentUser
.
getDept
())
&&
StringUtils
.
isNotEmpty
(
currentUser
.
getDept
().
getDeptName
()))
{
operLog
.
setDeptName
(
currentUser
.
getDept
().
getDeptName
());
}
}
if
(
e
!=
null
)
{
operLog
.
setStatus
(
BusinessStatus
.
FAIL
);
operLog
.
setErrorMsg
(
StringUtils
.
substring
(
e
.
getMessage
(),
0
,
2000
));
}
// 设置方法名称
String
className
=
joinPoint
.
getTarget
().
getClass
().
getName
();
String
methodName
=
joinPoint
.
getSignature
().
getName
();
operLog
.
setMethod
(
className
+
"."
+
methodName
+
"()"
);
// 处理设置注解上的参数
getControllerMethodDescription
(
controllerLog
,
operLog
);
// 保存数据库
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordOper
(
operLog
));
}
catch
(
Exception
exp
)
{
// 记录本地异常日志
log
.
error
(
"==前置通知异常=="
);
log
.
error
(
"异常信息:{}"
,
exp
.
getMessage
());
exp
.
printStackTrace
();
}
}
/**
* 获取注解中对方法的描述信息 用于Controller层注解
*
* @param joinPoint
* 切点
* @return 方法描述
* @throws Exception
*/
public
void
getControllerMethodDescription
(
Log
log
,
OperLog
operLog
)
throws
Exception
{
// 设置action动作
operLog
.
setAction
(
log
.
action
());
// 设置标题
operLog
.
setTitle
(
log
.
title
());
// 设置channel
operLog
.
setChannel
(
log
.
channel
());
// 是否需要保存request,参数和值
if
(
log
.
isSaveRequestData
())
{
// 获取参数的信息,传入到数据库中。
setRequestValue
(
operLog
);
}
}
/**
* 获取请求的参数,放到log中
*
* @param operLog
* @param request
*/
private
void
setRequestValue
(
OperLog
operLog
)
{
Map
<
String
,
String
[]>
map
=
ServletUtils
.
getRequest
().
getParameterMap
();
String
params
=
JSONObject
.
toJSONString
(
map
);
operLog
.
setOperParam
(
StringUtils
.
substring
(
params
,
0
,
255
));
}
/**
* 是否存在注解,如果存在就获取
*/
private
Log
getAnnotationLog
(
JoinPoint
joinPoint
)
throws
Exception
{
Signature
signature
=
joinPoint
.
getSignature
();
MethodSignature
methodSignature
=
(
MethodSignature
)
signature
;
Method
method
=
methodSignature
.
getMethod
();
if
(
method
!=
null
)
{
return
method
.
getAnnotation
(
Log
.
class
);
}
return
null
;
}
public
class
LogAspect
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
LogAspect
.
class
);
// 配置织入点
@Pointcut
(
"@annotation(com.ruoyi.framework.aspectj.lang.annotation.Log)"
)
public
void
logPointCut
()
{
}
/**
* 前置通知 用于拦截操作
*
* @param joinPoint 切点
*/
@AfterReturning
(
pointcut
=
"logPointCut()"
)
public
void
doBefore
(
JoinPoint
joinPoint
)
{
handleLog
(
joinPoint
,
null
);
}
/**
* 拦截异常操作
*
* @param joinPoint
* @param e
*/
@AfterThrowing
(
value
=
"logPointCut()"
,
throwing
=
"e"
)
public
void
doAfter
(
JoinPoint
joinPoint
,
Exception
e
)
{
handleLog
(
joinPoint
,
e
);
}
@Async
protected
void
handleLog
(
final
JoinPoint
joinPoint
,
final
Exception
e
)
{
try
{
// 获得注解
Log
controllerLog
=
getAnnotationLog
(
joinPoint
);
if
(
controllerLog
==
null
)
{
return
;
}
// 获取当前的用户
User
currentUser
=
ShiroUtils
.
getUser
();
// *========数据库日志=========*//
OperLog
operLog
=
new
OperLog
();
operLog
.
setStatus
(
BusinessStatus
.
SUCCESS
);
// 请求的地址
String
ip
=
ShiroUtils
.
getIp
();
operLog
.
setOperIp
(
ip
);
operLog
.
setOperUrl
(
ServletUtils
.
getRequest
().
getRequestURI
());
if
(
currentUser
!=
null
)
{
operLog
.
setOperName
(
currentUser
.
getLoginName
());
if
(
StringUtils
.
isNotNull
(
currentUser
.
getDept
())
&&
StringUtils
.
isNotEmpty
(
currentUser
.
getDept
().
getDeptName
()))
{
operLog
.
setDeptName
(
currentUser
.
getDept
().
getDeptName
());
}
}
if
(
e
!=
null
)
{
operLog
.
setStatus
(
BusinessStatus
.
FAIL
);
operLog
.
setErrorMsg
(
StringUtils
.
substring
(
e
.
getMessage
(),
0
,
2000
));
}
// 设置方法名称
String
className
=
joinPoint
.
getTarget
().
getClass
().
getName
();
String
methodName
=
joinPoint
.
getSignature
().
getName
();
operLog
.
setMethod
(
className
+
"."
+
methodName
+
"()"
);
// 处理设置注解上的参数
getControllerMethodDescription
(
controllerLog
,
operLog
);
// 保存数据库
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordOper
(
operLog
));
}
catch
(
Exception
exp
)
{
// 记录本地异常日志
log
.
error
(
"==前置通知异常=="
);
log
.
error
(
"异常信息:{}"
,
exp
.
getMessage
());
exp
.
printStackTrace
();
}
}
/**
* 获取注解中对方法的描述信息 用于Controller层注解
*
* @param joinPoint 切点
* @return 方法描述
* @throws Exception
*/
public
void
getControllerMethodDescription
(
Log
log
,
OperLog
operLog
)
throws
Exception
{
// 设置action动作
operLog
.
setAction
(
log
.
action
());
// 设置标题
operLog
.
setTitle
(
log
.
title
());
// 设置channel
operLog
.
setChannel
(
log
.
channel
());
// 是否需要保存request,参数和值
if
(
log
.
isSaveRequestData
())
{
// 获取参数的信息,传入到数据库中。
setRequestValue
(
operLog
);
}
}
/**
* 获取请求的参数,放到log中
*
* @param operLog
* @param request
*/
private
void
setRequestValue
(
OperLog
operLog
)
{
Map
<
String
,
String
[]>
map
=
ServletUtils
.
getRequest
().
getParameterMap
();
String
params
=
JSONObject
.
toJSONString
(
map
);
operLog
.
setOperParam
(
StringUtils
.
substring
(
params
,
0
,
255
));
}
/**
* 是否存在注解,如果存在就获取
*/
private
Log
getAnnotationLog
(
JoinPoint
joinPoint
)
throws
Exception
{
Signature
signature
=
joinPoint
.
getSignature
();
MethodSignature
methodSignature
=
(
MethodSignature
)
signature
;
Method
method
=
methodSignature
.
getMethod
();
if
(
method
!=
null
)
{
return
method
.
getAnnotation
(
Log
.
class
);
}
return
null
;
}
}
src/main/java/com/ruoyi/framework/manager/AsyncManager.java
View file @
e93e0c4c
...
...
@@ -9,18 +9,35 @@ import java.util.concurrent.TimeUnit;
*
* @author liuhulu
*/
public
class
AsyncManager
{
// 操作延迟
private
final
int
OPERATE_DELAY_TIME
=
10
;
// 异步操作此案城池
private
ScheduledThreadPoolExecutor
executor
=
new
ScheduledThreadPoolExecutor
(
5
);
// 单例
private
static
AsyncManager
me
=
new
AsyncManager
();
public
static
AsyncManager
me
()
{
return
me
;
}
// 执行任务
public
void
execute
(
TimerTask
task
)
{
executor
.
schedule
(
task
,
OPERATE_DELAY_TIME
,
TimeUnit
.
MILLISECONDS
);
}
public
class
AsyncManager
{
/**
* 操作延迟10毫秒
*/
private
final
int
OPERATE_DELAY_TIME
=
10
;
/**
* 异步操作任务调度线程池
*/
private
ScheduledThreadPoolExecutor
executor
=
new
ScheduledThreadPoolExecutor
(
5
);
/**
* 单例模式
*/
private
static
AsyncManager
me
=
new
AsyncManager
();
public
static
AsyncManager
me
()
{
return
me
;
}
/**
* 执行任务
*
* @param 任务task
*/
public
void
execute
(
TimerTask
task
)
{
executor
.
schedule
(
task
,
OPERATE_DELAY_TIME
,
TimeUnit
.
MILLISECONDS
);
}
}
src/main/java/com/ruoyi/framework/manager/factory/AsyncFactory.java
View file @
e93e0c4c
package
com
.
ruoyi
.
framework
.
manager
.
factory
;
import
java.util.TimerTask
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.ruoyi.common.constant.Constants
;
import
com.ruoyi.common.utils.AddressUtils
;
import
com.ruoyi.common.utils.LogUtils
;
...
...
@@ -18,7 +16,6 @@ import com.ruoyi.project.monitor.online.domain.UserOnline;
import
com.ruoyi.project.monitor.online.service.IUserOnlineService
;
import
com.ruoyi.project.monitor.operlog.domain.OperLog
;
import
com.ruoyi.project.monitor.operlog.service.IOperLogService
;
import
eu.bitwalker.useragentutils.UserAgent
;
/**
...
...
@@ -27,99 +24,111 @@ import eu.bitwalker.useragentutils.UserAgent;
* @author liuhulu
*
*/
public
class
AsyncFactory
{
private
static
final
Logger
sys_user_logger
=
LoggerFactory
.
getLogger
(
"sys-user"
);
public
class
AsyncFactory
{
private
static
final
Logger
sys_user_logger
=
LoggerFactory
.
getLogger
(
"sys-user"
);
/**
* 同步session到数据库
*
* @param operLog
* @return
*/
public
static
TimerTask
syncSessionToDb
(
final
OnlineSession
session
)
{
return
new
TimerTask
()
{
@Override
public
void
run
()
{
UserOnline
online
=
new
UserOnline
();
online
.
setSessionId
(
String
.
valueOf
(
session
.
getId
()));
online
.
setDeptName
(
session
.
getDeptName
());
online
.
setLoginName
(
session
.
getLoginName
());
online
.
setStartTimestamp
(
session
.
getStartTimestamp
());
online
.
setLastAccessTime
(
session
.
getLastAccessTime
());
online
.
setExpireTime
(
session
.
getTimeout
());
online
.
setIpaddr
(
session
.
getHost
());
online
.
setLonginLocation
(
AddressUtils
.
getRealAddressByIP
(
session
.
getHost
()));
online
.
setBrowser
(
session
.
getBrowser
());
online
.
setOs
(
session
.
getOs
());
online
.
setStatus
(
session
.
getStatus
());
online
.
setSession
(
session
);
SpringUtils
.
getBean
(
IUserOnlineService
.
class
).
saveOnline
(
online
);
/**
* 同步session到数据库
*
* @param session 在线用户会话
* @return 任务task
*/
public
static
TimerTask
syncSessionToDb
(
final
OnlineSession
session
)
{
return
new
TimerTask
()
{
@Override
public
void
run
()
{
UserOnline
online
=
new
UserOnline
();
online
.
setSessionId
(
String
.
valueOf
(
session
.
getId
()));
online
.
setDeptName
(
session
.
getDeptName
());
online
.
setLoginName
(
session
.
getLoginName
());
online
.
setStartTimestamp
(
session
.
getStartTimestamp
());
online
.
setLastAccessTime
(
session
.
getLastAccessTime
());
online
.
setExpireTime
(
session
.
getTimeout
());
online
.
setIpaddr
(
session
.
getHost
());
online
.
setLonginLocation
(
AddressUtils
.
getRealAddressByIP
(
session
.
getHost
()));
online
.
setBrowser
(
session
.
getBrowser
());
online
.
setOs
(
session
.
getOs
());
online
.
setStatus
(
session
.
getStatus
());
online
.
setSession
(
session
);
SpringUtils
.
getBean
(
IUserOnlineService
.
class
).
saveOnline
(
online
);
}
};
}
/**
* 记录 操作log
*
* @param rc
* @return
*/
public
static
TimerTask
recordOper
(
final
OperLog
operLog
)
{
return
new
TimerTask
()
{
@Override
public
void
run
()
{
// 远程查询操作地点
operLog
.
setOperLocation
(
AddressUtils
.
getRealAddressByIP
(
operLog
.
getOperIp
()));
SpringUtils
.
getBean
(
IOperLogService
.
class
).
insertOperlog
(
operLog
);
}
};
}
}
};
}
/**
* 记录登陆信息
*
* @param username
* @param status
* @param message
* @param userAgent
* @param args
* @return
*/
public
static
TimerTask
recordLogininfor
(
final
String
username
,
final
String
status
,
final
String
message
,
final
Object
...
args
)
{
final
UserAgent
userAgent
=
UserAgent
.
parseUserAgentString
(
ServletUtils
.
getRequest
().
getHeader
(
"User-Agent"
));
return
new
TimerTask
()
{
@Override
public
void
run
()
{
StringBuilder
s
=
new
StringBuilder
();
s
.
append
(
LogUtils
.
getBlock
(
ShiroUtils
.
getIp
()));
s
.
append
(
AddressUtils
.
getRealAddressByIP
(
ShiroUtils
.
getIp
()));
s
.
append
(
LogUtils
.
getBlock
(
username
));
s
.
append
(
LogUtils
.
getBlock
(
status
));
s
.
append
(
LogUtils
.
getBlock
(
message
));
// 打印信息到日志
sys_user_logger
.
info
(
s
.
toString
(),
args
);
// 获取客户端操作系统
String
os
=
userAgent
.
getOperatingSystem
().
getName
();
// 获取客户端浏览器
String
browser
=
userAgent
.
getBrowser
().
getName
();
// 封装对象
Logininfor
logininfor
=
new
Logininfor
();
logininfor
.
setLoginName
(
username
);
logininfor
.
setIpaddr
(
ShiroUtils
.
getIp
());
logininfor
.
setLoginLocation
(
AddressUtils
.
getRealAddressByIP
(
ShiroUtils
.
getIp
()));
logininfor
.
setBrowser
(
browser
);
logininfor
.
setOs
(
os
);
logininfor
.
setMsg
(
message
);
// 日志状态
if
(
Constants
.
LOGIN_SUCCESS
.
equals
(
status
)
||
Constants
.
LOGOUT
.
equals
(
status
))
{
logininfor
.
setStatus
(
Constants
.
SUCCESS
);
}
else
if
(
Constants
.
LOGIN_FAIL
.
equals
(
status
))
{
logininfor
.
setStatus
(
Constants
.
FAIL
);
}
// 插入数据
SpringUtils
.
getBean
(
LogininforServiceImpl
.
class
).
insertLogininfor
(
logininfor
);
}
};
}
/**
* 操作日志记录
*
* @param operLog 操作日志信息
* @return 任务task
*/
public
static
TimerTask
recordOper
(
final
OperLog
operLog
)
{
return
new
TimerTask
()
{
@Override
public
void
run
()
{
// 远程查询操作地点
operLog
.
setOperLocation
(
AddressUtils
.
getRealAddressByIP
(
operLog
.
getOperIp
()));
SpringUtils
.
getBean
(
IOperLogService
.
class
).
insertOperlog
(
operLog
);
}
};
}
/**
* 记录登陆信息
*
* @param username 用户名
* @param status 状态
* @param message 消息
* @param args 列表
* @return 任务task
*/
public
static
TimerTask
recordLogininfor
(
final
String
username
,
final
String
status
,
final
String
message
,
final
Object
...
args
)
{
final
UserAgent
userAgent
=
UserAgent
.
parseUserAgentString
(
ServletUtils
.
getRequest
().
getHeader
(
"User-Agent"
));
return
new
TimerTask
()
{
@Override
public
void
run
()
{
StringBuilder
s
=
new
StringBuilder
();
s
.
append
(
LogUtils
.
getBlock
(
ShiroUtils
.
getIp
()));
s
.
append
(
AddressUtils
.
getRealAddressByIP
(
ShiroUtils
.
getIp
()));
s
.
append
(
LogUtils
.
getBlock
(
username
));
s
.
append
(
LogUtils
.
getBlock
(
status
));
s
.
append
(
LogUtils
.
getBlock
(
message
));
// 打印信息到日志
sys_user_logger
.
info
(
s
.
toString
(),
args
);
// 获取客户端操作系统
String
os
=
userAgent
.
getOperatingSystem
().
getName
();
// 获取客户端浏览器
String
browser
=
userAgent
.
getBrowser
().
getName
();
// 封装对象
Logininfor
logininfor
=
new
Logininfor
();
logininfor
.
setLoginName
(
username
);
logininfor
.
setIpaddr
(
ShiroUtils
.
getIp
());
logininfor
.
setLoginLocation
(
AddressUtils
.
getRealAddressByIP
(
ShiroUtils
.
getIp
()));
logininfor
.
setBrowser
(
browser
);
logininfor
.
setOs
(
os
);
logininfor
.
setMsg
(
message
);
// 日志状态
if
(
Constants
.
LOGIN_SUCCESS
.
equals
(
status
)
||
Constants
.
LOGOUT
.
equals
(
status
))
{
logininfor
.
setStatus
(
Constants
.
SUCCESS
);
}
else
if
(
Constants
.
LOGIN_FAIL
.
equals
(
status
))
{
logininfor
.
setStatus
(
Constants
.
FAIL
);
}
// 插入数据
SpringUtils
.
getBean
(
LogininforServiceImpl
.
class
).
insertLogininfor
(
logininfor
);
}
};
}
}
src/main/java/com/ruoyi/framework/shiro/service/LoginService.java
View file @
e93e0c4c
...
...
@@ -3,7 +3,6 @@ package com.ruoyi.framework.shiro.service;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.StringUtils
;
import
com.ruoyi.common.constant.Constants
;
import
com.ruoyi.common.constant.ShiroConstants
;
import
com.ruoyi.common.constant.UserConstants
;
...
...
@@ -27,101 +26,104 @@ import com.ruoyi.project.system.user.service.IUserService;
* @author ruoyi
*/
@Component
public
class
LoginService
{
@Autowired
private
PasswordService
passwordService
;
@Autowired
private
IUserService
userService
;
/**
* 登录
*/
public
User
login
(
String
username
,
String
password
)
{
// 验证码校验
if
(!
StringUtils
.
isEmpty
(
ServletUtils
.
getRequest
().
getAttribute
(
ShiroConstants
.
CURRENT_CAPTCHA
)))
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
username
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"user.jcaptcha.error"
)));
// SystemLogUtils.log(username, Constants.LOGIN_FAIL,
// MessageUtils.message("user.jcaptcha.error"));
throw
new
CaptchaException
();
}
// 用户名或密码为空 错误
if
(
StringUtils
.
isEmpty
(
username
)
||
StringUtils
.
isEmpty
(
password
))
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
username
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"not.null"
)));
// SystemLogUtils.log(username, Constants.LOGIN_FAIL,
// MessageUtils.message("not.null"));
throw
new
UserNotExistsException
();
}
// 密码如果不在指定范围内 错误
if
(
password
.
length
()
<
UserConstants
.
PASSWORD_MIN_LENGTH
||
password
.
length
()
>
UserConstants
.
PASSWORD_MAX_LENGTH
)
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
username
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"user.password.not.match"
)));
// SystemLogUtils.log(username, Constants.LOGIN_FAIL,
// MessageUtils.message("user.password.not.match"));
throw
new
UserPasswordNotMatchException
();
}
// 用户名不在指定范围内 错误
if
(
username
.
length
()
<
UserConstants
.
USERNAME_MIN_LENGTH
||
username
.
length
()
>
UserConstants
.
USERNAME_MAX_LENGTH
)
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
username
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"user.password.not.match"
)));
// SystemLogUtils.log(username, Constants.LOGIN_FAIL,
// MessageUtils.message("user.password.not.match"));
throw
new
UserPasswordNotMatchException
();
}
// 查询用户信息
User
user
=
userService
.
selectUserByLoginName
(
username
);
if
(
user
==
null
&&
maybeMobilePhoneNumber
(
username
))
{
user
=
userService
.
selectUserByPhoneNumber
(
username
);
}
if
(
user
==
null
&&
maybeEmail
(
username
))
{
user
=
userService
.
selectUserByEmail
(
username
);
}
if
(
user
==
null
||
UserStatus
.
DELETED
.
getCode
().
equals
(
user
.
getDelFlag
()))
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
username
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"user.not.exists"
)));
// SystemLogUtils.log(username, Constants.LOGIN_FAIL,
// MessageUtils.message("user.not.exists"));
throw
new
UserNotExistsException
();
}
passwordService
.
validate
(
user
,
password
);
if
(
UserStatus
.
DISABLE
.
getCode
().
equals
(
user
.
getStatus
()))
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
username
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"user.blocked"
,
user
.
getRemark
())));
// SystemLogUtils.log(username, Constants.LOGIN_FAIL,
// MessageUtils.message("user.blocked", user.getRemark()));
throw
new
UserBlockedException
(
user
.
getRemark
());
}
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
username
,
Constants
.
LOGIN_SUCCESS
,
MessageUtils
.
message
(
"user.login.success"
)));
// SystemLogUtils.log(username, Constants.LOGIN_SUCCESS,
// MessageUtils.message("user.login.success"));
recordLoginInfo
(
user
);
return
user
;
}
private
boolean
maybeEmail
(
String
username
)
{
if
(!
username
.
matches
(
UserConstants
.
EMAIL_PATTERN
))
{
return
false
;
}
return
true
;
}
private
boolean
maybeMobilePhoneNumber
(
String
username
)
{
if
(!
username
.
matches
(
UserConstants
.
MOBILE_PHONE_NUMBER_PATTERN
))
{
return
false
;
}
return
true
;
}
/**
* 记录登录信息
*/
public
void
recordLoginInfo
(
User
user
)
{
user
.
setLoginIp
(
ShiroUtils
.
getIp
());
user
.
setLoginDate
(
DateUtils
.
getNowDate
());
userService
.
updateUserInfo
(
user
);
}
public
class
LoginService
{
@Autowired
private
PasswordService
passwordService
;
@Autowired
private
IUserService
userService
;
/**
* 登录
*/
public
User
login
(
String
username
,
String
password
)
{
// 验证码校验
if
(!
StringUtils
.
isEmpty
(
ServletUtils
.
getRequest
().
getAttribute
(
ShiroConstants
.
CURRENT_CAPTCHA
)))
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
username
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"user.jcaptcha.error"
)));
throw
new
CaptchaException
();
}
// 用户名或密码为空 错误
if
(
StringUtils
.
isEmpty
(
username
)
||
StringUtils
.
isEmpty
(
password
))
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
username
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"not.null"
)));
throw
new
UserNotExistsException
();
}
// 密码如果不在指定范围内 错误
if
(
password
.
length
()
<
UserConstants
.
PASSWORD_MIN_LENGTH
||
password
.
length
()
>
UserConstants
.
PASSWORD_MAX_LENGTH
)
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
username
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"user.password.not.match"
)));
throw
new
UserPasswordNotMatchException
();
}
// 用户名不在指定范围内 错误
if
(
username
.
length
()
<
UserConstants
.
USERNAME_MIN_LENGTH
||
username
.
length
()
>
UserConstants
.
USERNAME_MAX_LENGTH
)
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
username
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"user.password.not.match"
)));
throw
new
UserPasswordNotMatchException
();
}
// 查询用户信息
User
user
=
userService
.
selectUserByLoginName
(
username
);
if
(
user
==
null
&&
maybeMobilePhoneNumber
(
username
))
{
user
=
userService
.
selectUserByPhoneNumber
(
username
);
}
if
(
user
==
null
&&
maybeEmail
(
username
))
{
user
=
userService
.
selectUserByEmail
(
username
);
}
if
(
user
==
null
||
UserStatus
.
DELETED
.
getCode
().
equals
(
user
.
getDelFlag
()))
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
username
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"user.not.exists"
)));
throw
new
UserNotExistsException
();
}
passwordService
.
validate
(
user
,
password
);
if
(
UserStatus
.
DISABLE
.
getCode
().
equals
(
user
.
getStatus
()))
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
username
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"user.blocked"
,
user
.
getRemark
())));
throw
new
UserBlockedException
(
user
.
getRemark
());
}
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
username
,
Constants
.
LOGIN_SUCCESS
,
MessageUtils
.
message
(
"user.login.success"
)));
recordLoginInfo
(
user
);
return
user
;
}
private
boolean
maybeEmail
(
String
username
)
{
if
(!
username
.
matches
(
UserConstants
.
EMAIL_PATTERN
))
{
return
false
;
}
return
true
;
}
private
boolean
maybeMobilePhoneNumber
(
String
username
)
{
if
(!
username
.
matches
(
UserConstants
.
MOBILE_PHONE_NUMBER_PATTERN
))
{
return
false
;
}
return
true
;
}
/**
* 记录登录信息
*/
public
void
recordLoginInfo
(
User
user
)
{
user
.
setLoginIp
(
ShiroUtils
.
getIp
());
user
.
setLoginDate
(
DateUtils
.
getNowDate
());
userService
.
updateUserInfo
(
user
);
}
}
src/main/java/com/ruoyi/framework/shiro/service/PasswordService.java
View file @
e93e0c4c
package
com
.
ruoyi
.
framework
.
shiro
.
service
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
javax.annotation.PostConstruct
;
import
org.apache.shiro.cache.Cache
;
import
org.apache.shiro.cache.CacheManager
;
import
org.apache.shiro.crypto.hash.Md5Hash
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
com.ruoyi.common.constant.Constants
;
import
com.ruoyi.common.exception.user.UserPasswordNotMatchException
;
import
com.ruoyi.common.exception.user.UserPasswordRetryLimitExceedException
;
...
...
@@ -25,66 +22,70 @@ import com.ruoyi.project.system.user.domain.User;
* @author ruoyi
*/
@Component
public
class
PasswordService
{
@Autowired
private
CacheManager
cacheManager
;
private
Cache
<
String
,
AtomicInteger
>
loginRecordCache
;
@Value
(
value
=
"${user.password.maxRetryCount}"
)
private
String
maxRetryCount
;
@PostConstruct
public
void
init
()
{
loginRecordCache
=
cacheManager
.
getCache
(
"loginRecordCache"
);
}
public
void
validate
(
User
user
,
String
password
)
{
String
loginName
=
user
.
getLoginName
();
AtomicInteger
retryCount
=
loginRecordCache
.
get
(
loginName
);
if
(
retryCount
==
null
)
{
retryCount
=
new
AtomicInteger
(
0
);
loginRecordCache
.
put
(
loginName
,
retryCount
);
}
if
(
retryCount
.
incrementAndGet
()
>
Integer
.
valueOf
(
maxRetryCount
).
intValue
())
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
loginName
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"user.password.retry.limit.exceed"
),
maxRetryCount
));
// SystemLogUtils.log(loginName, Constants.LOGIN_FAIL,
// MessageUtils.message("user.password.retry.limit.exceed",
// maxRetryCount));
throw
new
UserPasswordRetryLimitExceedException
(
Integer
.
valueOf
(
maxRetryCount
).
intValue
());
}
if
(!
matches
(
user
,
password
))
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
loginName
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"user.password.retry.limit.count"
),
retryCount
,
password
));
// SystemLogUtils.log(loginName, Constants.LOGIN_FAIL,
// MessageUtils.message("user.password.retry.limit.count",
// retryCount, password));
loginRecordCache
.
put
(
loginName
,
retryCount
);
throw
new
UserPasswordNotMatchException
();
}
else
{
clearLoginRecordCache
(
loginName
);
}
}
public
boolean
matches
(
User
user
,
String
newPassword
)
{
return
user
.
getPassword
().
equals
(
encryptPassword
(
user
.
getLoginName
(),
newPassword
,
user
.
getSalt
()));
}
public
void
clearLoginRecordCache
(
String
username
)
{
loginRecordCache
.
remove
(
username
);
}
public
String
encryptPassword
(
String
username
,
String
password
,
String
salt
)
{
return
new
Md5Hash
(
username
+
password
+
salt
).
toHex
().
toString
();
}
public
static
void
main
(
String
[]
args
)
{
// System.out.println(new PasswordService().encryptPassword("admin",
// "admin123", "111111"));
// System.out.println(new PasswordService().encryptPassword("ry",
// "admin123", "222222"));
}
public
class
PasswordService
{
@Autowired
private
CacheManager
cacheManager
;
private
Cache
<
String
,
AtomicInteger
>
loginRecordCache
;
@Value
(
value
=
"${user.password.maxRetryCount}"
)
private
String
maxRetryCount
;
@PostConstruct
public
void
init
()
{
loginRecordCache
=
cacheManager
.
getCache
(
"loginRecordCache"
);
}
public
void
validate
(
User
user
,
String
password
)
{
String
loginName
=
user
.
getLoginName
();
AtomicInteger
retryCount
=
loginRecordCache
.
get
(
loginName
);
if
(
retryCount
==
null
)
{
retryCount
=
new
AtomicInteger
(
0
);
loginRecordCache
.
put
(
loginName
,
retryCount
);
}
if
(
retryCount
.
incrementAndGet
()
>
Integer
.
valueOf
(
maxRetryCount
).
intValue
())
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
loginName
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"user.password.retry.limit.exceed"
),
maxRetryCount
));
throw
new
UserPasswordRetryLimitExceedException
(
Integer
.
valueOf
(
maxRetryCount
).
intValue
());
}
if
(!
matches
(
user
,
password
))
{
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
loginName
,
Constants
.
LOGIN_FAIL
,
MessageUtils
.
message
(
"user.password.retry.limit.count"
),
retryCount
,
password
));
loginRecordCache
.
put
(
loginName
,
retryCount
);
throw
new
UserPasswordNotMatchException
();
}
else
{
clearLoginRecordCache
(
loginName
);
}
}
public
boolean
matches
(
User
user
,
String
newPassword
)
{
return
user
.
getPassword
().
equals
(
encryptPassword
(
user
.
getLoginName
(),
newPassword
,
user
.
getSalt
()));
}
public
void
clearLoginRecordCache
(
String
username
)
{
loginRecordCache
.
remove
(
username
);
}
public
String
encryptPassword
(
String
username
,
String
password
,
String
salt
)
{
return
new
Md5Hash
(
username
+
password
+
salt
).
toHex
().
toString
();
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
new
PasswordService
().
encryptPassword
(
"admin"
,
"admin123"
,
"111111"
));
System
.
out
.
println
(
new
PasswordService
().
encryptPassword
(
"ry"
,
"admin123"
,
"222222"
));
}
}
src/main/java/com/ruoyi/framework/shiro/session/OnlineSessionDAO.java
View file @
e93e0c4c
...
...
@@ -2,12 +2,10 @@ package com.ruoyi.framework.shiro.session;
import
java.io.Serializable
;
import
java.util.Date
;
import
org.apache.shiro.session.Session
;
import
org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
com.ruoyi.framework.manager.AsyncManager
;
import
com.ruoyi.framework.manager.factory.AsyncFactory
;
import
com.ruoyi.project.monitor.online.domain.OnlineSession
;
...
...
@@ -19,90 +17,101 @@ import com.ruoyi.project.monitor.online.service.IUserOnlineService;
*
* @author ruoyi
*/
public
class
OnlineSessionDAO
extends
EnterpriseCacheSessionDAO
{
/**
* 同步session到数据库的周期 单位为毫秒(默认1分钟)
*/
@Value
(
"${shiro.session.dbSyncPeriod}"
)
private
int
dbSyncPeriod
;
public
class
OnlineSessionDAO
extends
EnterpriseCacheSessionDAO
{
/**
* 同步session到数据库的周期 单位为毫秒(默认1分钟)
*/
@Value
(
"${shiro.session.dbSyncPeriod}"
)
private
int
dbSyncPeriod
;
/**
* 上次同步数据库的时间戳
*/
private
static
final
String
LAST_SYNC_DB_TIMESTAMP
=
OnlineSessionDAO
.
class
.
getName
()
+
"LAST_SYNC_DB_TIMESTAMP"
;
/**
* 上次同步数据库的时间戳
*/
private
static
final
String
LAST_SYNC_DB_TIMESTAMP
=
OnlineSessionDAO
.
class
.
getName
()
+
"LAST_SYNC_DB_TIMESTAMP"
;
@Autowired
private
IUserOnlineService
onlineService
;
@Autowired
private
IUserOnlineService
onlineService
;
@Autowired
private
OnlineSessionFactory
onlineSessionFactory
;
@Autowired
private
OnlineSessionFactory
onlineSessionFactory
;
public
OnlineSessionDAO
()
{
super
();
}
public
OnlineSessionDAO
()
{
super
();
}
public
OnlineSessionDAO
(
long
expireTime
)
{
super
();
}
public
OnlineSessionDAO
(
long
expireTime
)
{
super
();
}
/**
* 根据会话ID获取会话
*
* @param sessionId
* 会话ID
* @return ShiroSession
*/
@Override
protected
Session
doReadSession
(
Serializable
sessionId
)
{
UserOnline
userOnline
=
onlineService
.
selectOnlineById
(
String
.
valueOf
(
sessionId
));
if
(
userOnline
==
null
)
{
return
null
;
}
return
onlineSessionFactory
.
createSession
(
userOnline
);
}
/**
* 根据会话ID获取会话
*
* @param sessionId 会话ID
* @return ShiroSession
*/
@Override
protected
Session
doReadSession
(
Serializable
sessionId
)
{
UserOnline
userOnline
=
onlineService
.
selectOnlineById
(
String
.
valueOf
(
sessionId
));
if
(
userOnline
==
null
)
{
return
null
;
}
return
onlineSessionFactory
.
createSession
(
userOnline
);
}
/**
* 更新会话;如更新会话最后访问时间/停止会话/设置超时时间/设置移除属性等会调用
*/
public
void
syncToDb
(
OnlineSession
onlineSession
)
{
Date
lastSyncTimestamp
=
(
Date
)
onlineSession
.
getAttribute
(
LAST_SYNC_DB_TIMESTAMP
);
if
(
lastSyncTimestamp
!=
null
)
{
boolean
needSync
=
true
;
long
deltaTime
=
onlineSession
.
getLastAccessTime
().
getTime
()
-
lastSyncTimestamp
.
getTime
();
if
(
deltaTime
<
dbSyncPeriod
*
60
*
1000
)
{
// 时间差不足 无需同步
needSync
=
false
;
}
boolean
isGuest
=
onlineSession
.
getUserId
()
==
null
||
onlineSession
.
getUserId
()
==
0L
;
/**
* 更新会话;如更新会话最后访问时间/停止会话/设置超时时间/设置移除属性等会调用
*/
public
void
syncToDb
(
OnlineSession
onlineSession
)
{
Date
lastSyncTimestamp
=
(
Date
)
onlineSession
.
getAttribute
(
LAST_SYNC_DB_TIMESTAMP
);
if
(
lastSyncTimestamp
!=
null
)
{
boolean
needSync
=
true
;
long
deltaTime
=
onlineSession
.
getLastAccessTime
().
getTime
()
-
lastSyncTimestamp
.
getTime
();
if
(
deltaTime
<
dbSyncPeriod
*
60
*
1000
)
{
// 时间差不足 无需同步
needSync
=
false
;
}
boolean
isGuest
=
onlineSession
.
getUserId
()
==
null
||
onlineSession
.
getUserId
()
==
0L
;
// session 数据变更了 同步
if
(
isGuest
==
false
&&
onlineSession
.
isAttributeChanged
())
{
needSync
=
true
;
}
// session 数据变更了 同步
if
(
isGuest
==
false
&&
onlineSession
.
isAttributeChanged
())
{
needSync
=
true
;
}
if
(
needSync
==
false
)
{
return
;
}
}
onlineSession
.
setAttribute
(
LAST_SYNC_DB_TIMESTAMP
,
onlineSession
.
getLastAccessTime
());
// 更新完后 重置标识
if
(
onlineSession
.
isAttributeChanged
())
{
onlineSession
.
resetAttributeChanged
();
}
// onlineService.saveOnline(UserOnline.fromOnlineSession(onlineSession));
AsyncManager
.
me
().
execute
(
AsyncFactory
.
syncSessionToDb
(
onlineSession
));
}
if
(
needSync
==
false
)
{
return
;
}
}
onlineSession
.
setAttribute
(
LAST_SYNC_DB_TIMESTAMP
,
onlineSession
.
getLastAccessTime
());
// 更新完后 重置标识
if
(
onlineSession
.
isAttributeChanged
())
{
onlineSession
.
resetAttributeChanged
();
}
AsyncManager
.
me
().
execute
(
AsyncFactory
.
syncSessionToDb
(
onlineSession
));
}
/**
* 当会话过期/停止(如用户退出时)属性等会调用
*/
@Override
protected
void
doDelete
(
Session
session
)
{
OnlineSession
onlineSession
=
(
OnlineSession
)
session
;
if
(
null
==
onlineSession
)
{
return
;
}
onlineSession
.
setStatus
(
OnlineSession
.
OnlineStatus
.
off_line
);
onlineService
.
deleteOnlineById
(
String
.
valueOf
(
onlineSession
.
getId
()));
}
/**
* 当会话过期/停止(如用户退出时)属性等会调用
*/
@Override
protected
void
doDelete
(
Session
session
)
{
OnlineSession
onlineSession
=
(
OnlineSession
)
session
;
if
(
null
==
onlineSession
)
{
return
;
}
onlineSession
.
setStatus
(
OnlineSession
.
OnlineStatus
.
off_line
);
onlineService
.
deleteOnlineById
(
String
.
valueOf
(
onlineSession
.
getId
()));
}
}
src/main/java/com/ruoyi/framework/shiro/web/filter/LogoutFilter.java
View file @
e93e0c4c
...
...
@@ -2,12 +2,10 @@ package com.ruoyi.framework.shiro.web.filter;
import
javax.servlet.ServletRequest
;
import
javax.servlet.ServletResponse
;
import
org.apache.shiro.session.SessionException
;
import
org.apache.shiro.subject.Subject
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.ruoyi.common.constant.Constants
;
import
com.ruoyi.common.utils.MessageUtils
;
import
com.ruoyi.common.utils.StringUtils
;
...
...
@@ -21,58 +19,69 @@ import com.ruoyi.project.system.user.domain.User;
*
* @author ruoyi
*/
public
class
LogoutFilter
extends
org
.
apache
.
shiro
.
web
.
filter
.
authc
.
LogoutFilter
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
LogoutFilter
.
class
);
public
class
LogoutFilter
extends
org
.
apache
.
shiro
.
web
.
filter
.
authc
.
LogoutFilter
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
LogoutFilter
.
class
);
/**
* 退出后重定向的地址
*/
private
String
loginUrl
;
/**
* 退出后重定向的地址
*/
private
String
loginUrl
;
public
String
getLoginUrl
()
{
return
loginUrl
;
}
public
String
getLoginUrl
()
{
return
loginUrl
;
}
public
void
setLoginUrl
(
String
loginUrl
)
{
this
.
loginUrl
=
loginUrl
;
}
public
void
setLoginUrl
(
String
loginUrl
)
{
this
.
loginUrl
=
loginUrl
;
}
@Override
protected
boolean
preHandle
(
ServletRequest
request
,
ServletResponse
response
)
throws
Exception
{
try
{
Subject
subject
=
getSubject
(
request
,
response
);
String
redirectUrl
=
getRedirectUrl
(
request
,
response
,
subject
);
try
{
User
user
=
ShiroUtils
.
getUser
();
if
(
StringUtils
.
isNotNull
(
user
))
{
String
loginName
=
user
.
getLoginName
();
// 记录用户退出日志
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
loginName
,
Constants
.
LOGOUT
,
MessageUtils
.
message
(
"user.logout.success"
)));
// SystemLogUtils.log(loginName, Constants.LOGOUT,
// MessageUtils.message("user.logout.success"));
}
// 退出登录
subject
.
logout
();
}
catch
(
SessionException
ise
)
{
log
.
error
(
"logout fail."
,
ise
);
}
issueRedirect
(
request
,
response
,
redirectUrl
);
}
catch
(
Exception
e
)
{
log
.
error
(
"Encountered session exception during logout. This can generally safely be ignored."
,
e
);
}
return
false
;
}
@Override
protected
boolean
preHandle
(
ServletRequest
request
,
ServletResponse
response
)
throws
Exception
{
try
{
Subject
subject
=
getSubject
(
request
,
response
);
String
redirectUrl
=
getRedirectUrl
(
request
,
response
,
subject
);
try
{
User
user
=
ShiroUtils
.
getUser
();
if
(
StringUtils
.
isNotNull
(
user
))
{
String
loginName
=
user
.
getLoginName
();
// 记录用户退出日志
AsyncManager
.
me
().
execute
(
AsyncFactory
.
recordLogininfor
(
loginName
,
Constants
.
LOGOUT
,
MessageUtils
.
message
(
"user.logout.success"
)));
}
// 退出登录
subject
.
logout
();
}
catch
(
SessionException
ise
)
{
log
.
error
(
"logout fail."
,
ise
);
}
issueRedirect
(
request
,
response
,
redirectUrl
);
}
catch
(
Exception
e
)
{
log
.
error
(
"Encountered session exception during logout. This can generally safely be ignored."
,
e
);
}
return
false
;
}
/**
* 退出跳转URL
*/
@Override
protected
String
getRedirectUrl
(
ServletRequest
request
,
ServletResponse
response
,
Subject
subject
)
{
String
url
=
getLoginUrl
();
if
(
StringUtils
.
isNotEmpty
(
url
))
{
return
url
;
}
return
super
.
getRedirectUrl
(
request
,
response
,
subject
);
}
/**
* 退出跳转URL
*/
@Override
protected
String
getRedirectUrl
(
ServletRequest
request
,
ServletResponse
response
,
Subject
subject
)
{
String
url
=
getLoginUrl
();
if
(
StringUtils
.
isNotEmpty
(
url
))
{
return
url
;
}
return
super
.
getRedirectUrl
(
request
,
response
,
subject
);
}
}
src/main/java/com/ruoyi/project/monitor/online/domain/UserOnline.java
View file @
e93e0c4c
package
com
.
ruoyi
.
project
.
monitor
.
online
.
domain
;
import
java.util.Date
;
import
com.ruoyi.common.utils.AddressUtils
;
import
com.ruoyi.framework.web.domain.BaseEntity
;
import
com.ruoyi.project.monitor.online.domain.OnlineSession.OnlineStatus
;
...
...
@@ -11,164 +9,172 @@ import com.ruoyi.project.monitor.online.domain.OnlineSession.OnlineStatus;
*
* @author ruoyi
*/
public
class
UserOnline
extends
BaseEntity
{
private
static
final
long
serialVersionUID
=
1L
;
/** 用户会话id */
private
String
sessionId
;
/** 部门名称 */
private
String
deptName
;
/** 登录名称 */
private
String
loginName
;
/** 登录IP地址 */
private
String
ipaddr
;
/** 登录地址 */
private
String
longinLocation
;
/** 浏览器类型 */
private
String
browser
;
/** 操作系统 */
private
String
os
;
/** session创建时间 */
private
Date
startTimestamp
;
/** session最后访问时间 */
private
Date
lastAccessTime
;
/** 超时时间,单位为分钟 */
private
Long
expireTime
;
/** 在线状态 */
private
OnlineStatus
status
=
OnlineStatus
.
on_line
;
/** 备份的当前用户会话 */
private
OnlineSession
session
;
/**
* 设置session对象
*/
@Deprecated
public
static
final
UserOnline
fromOnlineSession
(
OnlineSession
session
)
{
UserOnline
online
=
new
UserOnline
();
online
.
setSessionId
(
String
.
valueOf
(
session
.
getId
()));
online
.
setDeptName
(
session
.
getDeptName
());
online
.
setLoginName
(
session
.
getLoginName
());
online
.
setStartTimestamp
(
session
.
getStartTimestamp
());
online
.
setLastAccessTime
(
session
.
getLastAccessTime
());
online
.
setExpireTime
(
session
.
getTimeout
());
online
.
setIpaddr
(
session
.
getHost
());
online
.
setLonginLocation
(
AddressUtils
.
getRealAddressByIP
(
session
.
getHost
()));
online
.
setBrowser
(
session
.
getBrowser
());
online
.
setOs
(
session
.
getOs
());
online
.
setStatus
(
session
.
getStatus
());
online
.
setSession
(
session
);
return
online
;
}
public
String
getSessionId
()
{
return
sessionId
;
}
public
void
setSessionId
(
String
sessionId
)
{
this
.
sessionId
=
sessionId
;
}
public
String
getDeptName
()
{
return
deptName
;
}
public
void
setDeptName
(
String
deptName
)
{
this
.
deptName
=
deptName
;
}
public
String
getLoginName
()
{
return
loginName
;
}
public
void
setLoginName
(
String
loginName
)
{
this
.
loginName
=
loginName
;
}
public
String
getIpaddr
()
{
return
ipaddr
;
}
public
void
setIpaddr
(
String
ipaddr
)
{
this
.
ipaddr
=
ipaddr
;
}
public
String
getLonginLocation
()
{
return
longinLocation
;
}
public
void
setLonginLocation
(
String
longinLocation
)
{
this
.
longinLocation
=
longinLocation
;
}
public
String
getBrowser
()
{
return
browser
;
}
public
void
setBrowser
(
String
browser
)
{
this
.
browser
=
browser
;
}
public
String
getOs
()
{
return
os
;
}
public
void
setOs
(
String
os
)
{
this
.
os
=
os
;
}
public
Date
getStartTimestamp
()
{
return
startTimestamp
;
}
public
void
setStartTimestamp
(
Date
startTimestamp
)
{
this
.
startTimestamp
=
startTimestamp
;
}
public
Date
getLastAccessTime
()
{
return
lastAccessTime
;
}
public
void
setLastAccessTime
(
Date
lastAccessTime
)
{
this
.
lastAccessTime
=
lastAccessTime
;
}
public
Long
getExpireTime
()
{
return
expireTime
;
}
public
void
setExpireTime
(
Long
expireTime
)
{
this
.
expireTime
=
expireTime
;
}
public
OnlineStatus
getStatus
()
{
return
status
;
}
public
void
setStatus
(
OnlineStatus
status
)
{
this
.
status
=
status
;
}
public
OnlineSession
getSession
()
{
return
session
;
}
public
void
setSession
(
OnlineSession
session
)
{
this
.
session
=
session
;
}
@Override
public
String
toString
()
{
return
"UserOnline [sessionId="
+
sessionId
+
", deptName="
+
deptName
+
", loginName="
+
loginName
+
", ipaddr="
+
ipaddr
+
", browser="
+
browser
+
", os="
+
os
+
", startTimestamp="
+
startTimestamp
+
", lastAccessTime="
+
lastAccessTime
+
", expireTime="
+
expireTime
+
", status="
+
status
+
", session="
+
session
+
"]"
;
}
public
class
UserOnline
extends
BaseEntity
{
private
static
final
long
serialVersionUID
=
1L
;
/** 用户会话id */
private
String
sessionId
;
/** 部门名称 */
private
String
deptName
;
/** 登录名称 */
private
String
loginName
;
/** 登录IP地址 */
private
String
ipaddr
;
/** 登录地址 */
private
String
longinLocation
;
/** 浏览器类型 */
private
String
browser
;
/** 操作系统 */
private
String
os
;
/** session创建时间 */
private
Date
startTimestamp
;
/** session最后访问时间 */
private
Date
lastAccessTime
;
/** 超时时间,单位为分钟 */
private
Long
expireTime
;
/** 在线状态 */
private
OnlineStatus
status
=
OnlineStatus
.
on_line
;
/** 备份的当前用户会话 */
private
OnlineSession
session
;
public
String
getSessionId
()
{
return
sessionId
;
}
public
void
setSessionId
(
String
sessionId
)
{
this
.
sessionId
=
sessionId
;
}
public
String
getDeptName
()
{
return
deptName
;
}
public
void
setDeptName
(
String
deptName
)
{
this
.
deptName
=
deptName
;
}
public
String
getLoginName
()
{
return
loginName
;
}
public
void
setLoginName
(
String
loginName
)
{
this
.
loginName
=
loginName
;
}
public
String
getIpaddr
()
{
return
ipaddr
;
}
public
void
setIpaddr
(
String
ipaddr
)
{
this
.
ipaddr
=
ipaddr
;
}
public
String
getLonginLocation
()
{
return
longinLocation
;
}
public
void
setLonginLocation
(
String
longinLocation
)
{
this
.
longinLocation
=
longinLocation
;
}
public
String
getBrowser
()
{
return
browser
;
}
public
void
setBrowser
(
String
browser
)
{
this
.
browser
=
browser
;
}
public
String
getOs
()
{
return
os
;
}
public
void
setOs
(
String
os
)
{
this
.
os
=
os
;
}
public
Date
getStartTimestamp
()
{
return
startTimestamp
;
}
public
void
setStartTimestamp
(
Date
startTimestamp
)
{
this
.
startTimestamp
=
startTimestamp
;
}
public
Date
getLastAccessTime
()
{
return
lastAccessTime
;
}
public
void
setLastAccessTime
(
Date
lastAccessTime
)
{
this
.
lastAccessTime
=
lastAccessTime
;
}
public
Long
getExpireTime
()
{
return
expireTime
;
}
public
void
setExpireTime
(
Long
expireTime
)
{
this
.
expireTime
=
expireTime
;
}
public
OnlineStatus
getStatus
()
{
return
status
;
}
public
void
setStatus
(
OnlineStatus
status
)
{
this
.
status
=
status
;
}
public
OnlineSession
getSession
()
{
return
session
;
}
public
void
setSession
(
OnlineSession
session
)
{
this
.
session
=
session
;
}
@Override
public
String
toString
()
{
return
"UserOnline [sessionId="
+
sessionId
+
", deptName="
+
deptName
+
", loginName="
+
loginName
+
", ipaddr="
+
ipaddr
+
", browser="
+
browser
+
", os="
+
os
+
", startTimestamp="
+
startTimestamp
+
", lastAccessTime="
+
lastAccessTime
+
", expireTime="
+
expireTime
+
", status="
+
status
+
", session="
+
session
+
"]"
;
}
}
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