首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

struts2拦截器兑现用户登录管理

2012-09-22 
struts2拦截器实现用户登录管理struts2拦截器实现用户登录管理ManagerInterceptor.java?package dsh.bikeg

struts2拦截器实现用户登录管理

struts2拦截器实现用户登录管理

ManagerInterceptor.java

?

package dsh.bikegis.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;import dsh.bikegis.action.ManagerLoginAction;/** * 管理員登錄控制的攔截器 * @author NanGuoCan * */public class ManagerInterceptor extends AbstractInterceptor {private static final long serialVersionUID = 5722672508679011124L;public String intercept(ActionInvocation invocation) throws Exception {HttpServletRequest request = ServletActionContext.getRequest();HttpSession session = request.getSession(true);String username = (String) session.getAttribute("userName");ActionSupport action = (ActionSupport) invocation.getAction();if (action instanceof ManagerLoginAction)return invocation.invoke();if (username != null && !username.equals(""))return invocation.invoke();else {return "login";}}}

?

?ManagerLoginActionImpl.java

package dsh.bikegis.action.impl;import com.opensymphony.xwork2.ActionSupport;import dsh.bikegis.action.ManagerLoginAction;import dsh.bikegis.model.User;import dsh.bikegis.service.ManagerLoginService;import dsh.bikegis.system.SysAction;public class ManagerLoginActionImpl extends SysAction implements ManagerLoginAction {/** * 管理員登錄Action的實現部分 */private static final long serialVersionUID = 1L;private User user;private ManagerLoginService loginService;private String errMesg;/** * 管理員登錄 */@Overridepublic String login() {if(!(this.validateLogin()))return ActionSupport.ERROR;try {if (this.loginService.login(user)) {// 登錄成功request.getSession().setAttribute("userName",user.getUsername());request.getSession().setAttribute("id", user.getId());if (user.getUsername().equals("manager")) {request.getSession().setAttribute("ROLE", "manager");}return SUCCESS;} else {this.errMesg = "帳號或密码错误";return ERROR;}} catch (Exception e) {e.printStackTrace();this.errMesg = "帳號或密码错误";return ERROR;}}/** * 管理员登录 验证 * @return * 驗證成功返回true,否則返回false */@Overridepublic boolean validateLogin() {if (user == null) {this.errMesg ="未能取得用戶信息";return false;}if (user.getUsername() == null || user.getUsername().equals("")) {this.errMesg = "帳號不能為空";return false;}if (user.getUserpsw() == null || user.getUserpsw().equals("")) {this.errMesg = "密碼不能為空";return false;}return true;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}public ManagerLoginService getLoginService() {return loginService;}public void setLoginService(ManagerLoginService loginService) {this.loginService = loginService;}public String getErrMesg() {return errMesg;}public void setErrMesg(String errMesg) {this.errMesg = errMesg;}}

?struts.xml

<!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- 用戶操作類的Action --><package name="adminmanage" namespace="/manage" extends="bikeGIS"><interceptors><!-- 是否登录 --><interceptor name="adminLogin"type="redirectAction"><param name="actionName">index</param><param name="namespace">/manage</param></result></global-results><action name="index" method="login"><result type="redirect">/manage/frame.action</result><result name="error">/manage/index.jsp</result><result name="input">/manage/index.jsp</result></action><!-- 用戶退出 --><action name="loginOut" method="loginOut"><result name="success">/manage/index.jsp</result></action><!-- 修改密碼 --><action name="modifyPswAction" method="modifyUserPsw"><result name="success" type="redirect" >/manage/main.action</result><result name="error" >/manage/user/modifypsw.jsp</result></action></package></struts>
?

热点排行