Hibernate Spring Struts2 它们的拦截器
Hibernate拦截
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
@Component
(
"entityInterceptor"
)
public
class
EntityInterceptor
extends
EmptyInterceptor {
private
static
final
long
serialVersionUID = 7319416231145791577L;
// 保存回调方法 保存时更新时间
@Override
public
boolean
onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
if
(entity
instanceof
BaseEntity) {
for
(
int
i =
0
; i < propertyNames.length; i++) {
if
(BaseEntity.CREATE_DATE_PROPERTY_NAME.equals(propertyNames[i]) || BaseEntity.MODIFY_DATE_PROPERTY_NAME.equals(propertyNames[i])) {
state[i] =
new
Date();
}
}
ReflectionUtil.invokeSetterMethod(entity, BaseEntity.CREATE_DATE_PROPERTY_NAME,
new
Date());
//反射调用
}
return
true
;
}
/**
* 调用Setter方法
*
* @param object
* 对象
*
* @param propertyName
* 属性名称
*
* @param propertyValue
* 属性值
*/
public
static
void
invokeSetterMethod(Object object, String propertyName, Object propertyValue) {
Class<?> setterMethodClass = propertyValue.getClass();
invokeSetterMethod(object, propertyName, propertyValue, setterMethodClass);
}
/**
* 调用Setter方法
*
* @param object
* 对象
*
* @param propertyName
* 属性名称
*
* @param propertyValue
* 属性值
*
* @param setterMethodClass
* 参数类型
*/
public
static
void
invokeSetterMethod(Object object, String propertyName, Object propertyValue, Class<?> setterMethodClass) {
String setterMethodName =
"set"
+ StringUtils.capitalize(propertyName);
try
{
Method setterMethod = object.getClass().getMethod(setterMethodName, setterMethodClass);
setterMethod.invoke(object, propertyValue);
}
catch
(Exception e) {
e.printStackTrace();
}
}
|
Sturs2拦截
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
public
class
LogInterceptor
extends
AbstractInterceptor {
private
static
final
long
serialVersionUID = 276741467699160227L;
@Resource
(name =
"logServiceImpl"
)
private
LogService logService;
@Resource
(name =
"adminServiceImpl"
)
private
AdminService adminService;
@Override
public
String intercept(ActionInvocation invocation)
throws
Exception {
String result = invocation.invoke();
Object action = invocation.getAction();
String actionClass = action.getClass().getName();
String actionMethod = invocation.getProxy().getMethod();
if
(action
instanceof
BaseAdminAction) {
if
(!StringUtils.equals(result, BaseAdminAction.ERROR)) {
List<LogConfig> allLogConfigList = LogConfigUtil.getAllLogConfigList();
if
(allLogConfigList !=
null
) {
for
(LogConfig logConfig : allLogConfigList) {
if
(StringUtils.equals(logConfig.getActionClass(), actionClass) && StringUtils.equals(logConfig.getActionMethod(), actionMethod)) {
BaseAdminAction baseAdminAction = (BaseAdminAction) action;
HttpServletRequest request= ServletActionContext.getRequest();
String logInfo = baseAdminAction.getLogInfo();
String operator = adminService.getLoginAdmin().getUsername();
if
(operator ==
null
) {
operator =
"未知"
;
}
String ip = request.getRemoteAddr();
String operation = logConfig.getOperation();
Log log =
new
Log();
log.setOperation(operation);
log.setOperator(operator);
log.setActionClass(actionClass);
log.setActionMethod(actionMethod);
log.setIp(ip);
log.setInfo(logInfo);
logService.save(log);
break
;
}
}
}
}
}
return
null
;
}<br><br>或者
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public
class
MemberVerifyInterceptor
extends
MethodFilterInterceptor {
private
static
final
long
serialVersionUID = -86246303854807787L;
@Override
public
String doIntercept(ActionInvocation actionInvocation)
throws
Exception {
String loginMemberId = (String) actionInvocation.getInvocationContext().getSession().get(Member.MEMBER_ID_SESSION_NAME);
if
(loginMemberId ==
null
) {
HttpServletRequest request = ServletActionContext.getRequest();
String loginRedirectUrl = request.getServletPath();
String queryString = request.getQueryString();
if
(StringUtils.isNotEmpty(queryString)) {
loginRedirectUrl +=
"?"
+ queryString;
}
OgnlValueStack ognlValueStack = (OgnlValueStack)request.getAttribute(
"struts.valueStack"
);
ognlValueStack.set(
"loginRedirectUrl"
, loginRedirectUrl);
return
"login"
;
}
return
actionInvocation.invoke();
}
|
Spring拦截
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
@Component
(
"adminSecurityListener"
)
public
class
AdminSecurityListener
implements
ApplicationListener {
@Resource
(name =
"adminServiceImpl"
)
private
AdminService adminService;
public
void
onApplicationEvent(ApplicationEvent applicationEvent) {
// 登录成功:记录登录IP、清除登录失败次数
if
(applicationEvent
instanceof
AuthenticationSuccessEvent) {
AuthenticationSuccessEvent authenticationSuccessEvent = (AuthenticationSuccessEvent) applicationEvent;
Authentication authentication = (Authentication) authenticationSuccessEvent.getSource();
String loginIp = ((WebAuthenticationDetails)authentication.getDetails()).getRemoteAddress();
Admin admin = (Admin) authentication.getPrincipal();
admin.setLoginIp(loginIp);
admin.setLoginDate(
new
Date());
Setting setting = SettingUtil.getSetting();
if
(setting.getIsLoginFailureLock() ==
false
) {
return
;
}
admin.setLoginFailureCount(
0
);
adminService.update(admin);
}
// 登录失败:增加登录失败次数
if
(applicationEvent
instanceof
AuthenticationFailureBadCredentialsEvent) {
AuthenticationFailureBadCredentialsEvent authenticationFailureBadCredentialsEvent = (AuthenticationFailureBadCredentialsEvent) applicationEvent;
Authentication authentication = (Authentication) authenticationFailureBadCredentialsEvent.getSource();
String loginUsername = authentication.getName();
Setting setting = SettingUtil.getSetting();
if
(setting.getIsLoginFailureLock() ==
false
) {
return
;
}
Admin admin = adminService.getAdminByUsername(loginUsername);
if
(admin !=
null
) {
int
loginFailureCount = admin.getLoginFailureCount() +
1
;
if
(loginFailureCount >= setting.getLoginFailureLockCount()) {
admin.setIsAccountLocked(
true
);
admin.setLockedDate(
new
Date());
}
admin.setLoginFailureCount(loginFailureCount);
adminService.update(admin);
}
}
}
}
|
上述只是框架中的一小部分功能,总结它们的特点是:
struts2 适合会员是否登录、请求参数等处理 多与前台打交道
spring 需要操作数据库,并且不用返回给用户消息,逻辑处理情景下用
hibernate 操作实体类(表)时的场景用,其与spring拦截相似,hibernate还有给表自动命名的策略