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;
	/**
	 * 管理員登錄
	 */
	@Override
	public 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
	 */
	@Override
	public 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"
				class="dsh.bikegis.interceptor.ManagerInterceptor"></interceptor>
			<interceptor-stack name="adminmanageStack">
				<interceptor-ref name="bikeGISstack"></interceptor-ref>
				<interceptor-ref name="adminLogin"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		<default-interceptor-ref name="adminmanageStack"></default-interceptor-ref>

		<global-results>
			<!-- 重新登錄 -->
			<result name="login" type="redirectAction">
				<param name="actionName">index</param>
				<param name="namespace">/manage</param>
			</result>
		</global-results>

		<action name="index" class="loginAction">
			<result>/manage/index.jsp</result>
		</action>

		<!-- 用戶登錄 -->
		<action name="loginAction" class="loginAction" 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" class="userAction" method="loginOut">
			<result name="success">/manage/index.jsp</result>
		</action>

		<!-- 修改密碼 -->
		<action name="modifyPswAction" class="userAction" method="modifyUserPsw">
			<result name="success" type="redirect" >/manage/main.action</result>
			<result name="error" >/manage/user/modifypsw.jsp</result>
		</action>

	</package>

</struts>
 

你可能感兴趣的:(struts2拦截器实现用户登录管理)