使用拦截器实现通用的登陆验证和日志记录
2016-03-16 11:35
579 查看
拦截器手动实现,要实现相应的拦截器接口
登陆验证的拦截器:
@Component
public class CheckLoginInterceptor implements Interceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
Action action = (Action) invocation.getAction();
if (action instanceof LoginAction) {
String method = invocation.getProxy().getMethod();
if (method.equals("logout")) {
HttpServletRequest request = (HttpServletRequest) invocation
.getInvocationContext().get(
ServletActionContext.HTTP_REQUEST);
HttpSession session = request.getSession();
invocation.getInvocationContext().getSession();
session.invalidate();// 清除缓存
return "toLogin";
} else {
return invocation.invoke();
}
}else {
BackUserModel pm = (BackUserModel) invocation.getInvocationContext()
.getSession().get("personLogin");
if (pm != null) {
return invocation.invoke();
} else {
return "toLogin";
}
}
}
日志记录的拦截器:
@Component
public class LogInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 2075536070042451457L;
@Resource
private LogService LogServiceImpl;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// 获取当前用户
BackUserModel user = (BackUserModel) ActionContext.getContext()
.getSession().get(Keys.LOGINUSERKEY);
// 获取请求参数
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
// 获取action
String actionName = invocation.getProxy().getActionName();
// 获取执行前的时间
Long startTime = System.currentTimeMillis();
// 执行方法
String invoke = invocation.invoke();
// 获取执行后的时间
Long endTime = System.currentTimeMillis();
if (!IContents.LOGINGOREACTIONLIST.contains(actionName)) {
// 保存日志
LogServiceImpl.save(new Log(user == null ? "未知用户" : user
.getBuserLoginName(), (actionName != null && actionName
.indexOf("_") >= 0) ? actionName.substring(0,
actionName.indexOf("_")) : actionName, actionName,
toIpAddr(request), String.valueOf(endTime - startTime),
new Date()));
}
return invoke;
}
/**
* 返回用户的IP地址
*
* @param request
* @return
*/
public static String toIpAddr(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
struts文件添加拦截器标签和相应action<package name="xx-default" extends="json-default">
<interceptors>
<interceptor name="checkLogin" class="checkLoginInterceptor" /><!-- 登陆拦截器 -->
<interceptor name="checkPrivilege" class="checkPrivilegeInterceptor" /><!-- 权限拦截器 -->
<interceptor name="log" class="logInterceptor" /><!-- 日志拦截器 -->
<interceptor-stack name="commonStack">
<interceptor-ref name="defaultStack" /><!-- 默认包 -->
<interceptor-ref name="checkLogin" />
<interceptor-ref name="checkPrivilege" />
<interceptor-ref name="log" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="commonStack" />
<global-results><!-- 全局返回结果拦截 -->
<result name="toLogin" type="redirect">/back/login_loginUI.action</result>
<result name="noPrivilegeError">/noPrivilegeError.jsp</result>
<result name="fileNotExists">/fileNotExists.jsp</result>
<result name="error">/error.jsp</result>
<result name="message">/message.jsp</result>
</global-results>
</package>
<pre name="code" class="html"> <!-- 登陆action -->
<package name="xxx" namespace="/back" extends="xxx-default">
<action name="login_*" class="loginAction" method="{1}">
<result name="loginUI">/WEB-INF/back/login.jsp</result>
<result name="logout">login_loginUI.action</result>
<result name="loginOK" type="redirect">home_mainUI.action</result>
<result name="success">/WEB-INF/back/test.jsp</result>
</action>
</package>
登陆验证的拦截器:
@Component
public class CheckLoginInterceptor implements Interceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
Action action = (Action) invocation.getAction();
if (action instanceof LoginAction) {
String method = invocation.getProxy().getMethod();
if (method.equals("logout")) {
HttpServletRequest request = (HttpServletRequest) invocation
.getInvocationContext().get(
ServletActionContext.HTTP_REQUEST);
HttpSession session = request.getSession();
invocation.getInvocationContext().getSession();
session.invalidate();// 清除缓存
return "toLogin";
} else {
return invocation.invoke();
}
}else {
BackUserModel pm = (BackUserModel) invocation.getInvocationContext()
.getSession().get("personLogin");
if (pm != null) {
return invocation.invoke();
} else {
return "toLogin";
}
}
}
日志记录的拦截器:
@Component
public class LogInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 2075536070042451457L;
@Resource
private LogService LogServiceImpl;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// 获取当前用户
BackUserModel user = (BackUserModel) ActionContext.getContext()
.getSession().get(Keys.LOGINUSERKEY);
// 获取请求参数
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
// 获取action
String actionName = invocation.getProxy().getActionName();
// 获取执行前的时间
Long startTime = System.currentTimeMillis();
// 执行方法
String invoke = invocation.invoke();
// 获取执行后的时间
Long endTime = System.currentTimeMillis();
if (!IContents.LOGINGOREACTIONLIST.contains(actionName)) {
// 保存日志
LogServiceImpl.save(new Log(user == null ? "未知用户" : user
.getBuserLoginName(), (actionName != null && actionName
.indexOf("_") >= 0) ? actionName.substring(0,
actionName.indexOf("_")) : actionName, actionName,
toIpAddr(request), String.valueOf(endTime - startTime),
new Date()));
}
return invoke;
}
/**
* 返回用户的IP地址
*
* @param request
* @return
*/
public static String toIpAddr(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
struts文件添加拦截器标签和相应action<package name="xx-default" extends="json-default">
<interceptors>
<interceptor name="checkLogin" class="checkLoginInterceptor" /><!-- 登陆拦截器 -->
<interceptor name="checkPrivilege" class="checkPrivilegeInterceptor" /><!-- 权限拦截器 -->
<interceptor name="log" class="logInterceptor" /><!-- 日志拦截器 -->
<interceptor-stack name="commonStack">
<interceptor-ref name="defaultStack" /><!-- 默认包 -->
<interceptor-ref name="checkLogin" />
<interceptor-ref name="checkPrivilege" />
<interceptor-ref name="log" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="commonStack" />
<global-results><!-- 全局返回结果拦截 -->
<result name="toLogin" type="redirect">/back/login_loginUI.action</result>
<result name="noPrivilegeError">/noPrivilegeError.jsp</result>
<result name="fileNotExists">/fileNotExists.jsp</result>
<result name="error">/error.jsp</result>
<result name="message">/message.jsp</result>
</global-results>
</package>
<pre name="code" class="html"> <!-- 登陆action -->
<package name="xxx" namespace="/back" extends="xxx-default">
<action name="login_*" class="loginAction" method="{1}">
<result name="loginUI">/WEB-INF/back/login.jsp</result>
<result name="logout">login_loginUI.action</result>
<result name="loginOK" type="redirect">home_mainUI.action</result>
<result name="success">/WEB-INF/back/test.jsp</result>
</action>
</package>
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树