struts拦截器(实现权限验证)

拦截器的使用:

package com.beiruan.xitongguanli.interceptor;

import java.util.Map;

import com.beiruan.xitongguanli.entity.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class QuanXianInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Map<String, Object> session = invocation.getInvocationContext().getSession();
		User user = (User) session.get("loginUser");
		if(user == null){
			return Action.LOGIN;
		}else{
			return invocation.invoke();
		}
	}

}

<interceptors>
			<!-- 声明这个类是拦截器 -->
			<interceptor name="myTimerInterceptor" class="com.beiruan.xitongguanli.interceptor.MyTimerInterceptor"/>
			<interceptor name="QuanXianInterceptor" class="com.beiruan.xitongguanli.interceptor.QuanXianInterceptor"/>
			<!-- 拦截器栈 -->
			<interceptor-stack name="myDefaultInterceptor">
				<interceptor-ref name="myTimerInterceptor"/>
				<interceptor-ref name="QuanXianInterceptor"/>
				<interceptor-ref name="defaultStack"/>
			</interceptor-stack>
		</interceptors>
		<!-- 修改默认拦截器 -->
		<default-interceptor-ref name="myDefaultInterceptor"/>
			

<action name="login" class="com.beiruan.xitongguanli.action.UserAction"
			method="login">
			<result name="login">/login.jsp</result>
			<result name="success">/success.jsp</result>
			<result name="error">/error.jsp</result>
			<interceptor-ref name="defaultStack"/>
			<!-- 使拦截器作用于这个action -->
		</action>

package com.beiruan.xitongguanli.action;

import java.util.List;

import com.beiruan.util.Constants;
import com.beiruan.xitongguanli.entity.User;
import com.beiruan.xitongguanli.service.UserService;
import com.beiruan.xitongguanli.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	private String username;
	private String password;
	public void setUsername(String username) {
		this.username = username;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	private int userId;
	public void setUserId(int userId) {
		this.userId = userId;
	}

	private UserService userService = new UserServiceImpl();
	private List<User> userList;
	public List<User> getUserList() {
		return userList;
	}
	
	
	public String login() throws Exception {
		User user = userService.login(username, password);
		if(user != null){
			ActionContext.getContext().getSession().put(Constants.LOGIN_USER, user);
			userList = userService.getAllUserList();
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	
	public String delete() throws Exception {
		userService.deleteUserByUserId(userId);
		return SUCCESS;
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值