⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logininteceptor.java

📁 struts2+spring2.0+hibernate3.2与acegi 1.0.7整合实现登陆验证
💻 JAVA
字号:
package com.test.interceptor;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.test.service.UserService;

import java.net.URLDecoder;

public class LoginInteceptor extends AbstractInterceptor {

	private UserService userService;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("login inteceptor_1");
		HttpServletRequest rq = (HttpServletRequest) ActionContext.getContext()
				.get(StrutsStatics.HTTP_REQUEST);
		HttpServletResponse rp = (HttpServletResponse) ActionContext
				.getContext().get(StrutsStatics.HTTP_RESPONSE);
//		HttpSession session = rq.getSession();
		
		rq.setCharacterEncoding("UTF-8");
		rp.setCharacterEncoding("UTF-8");
		
		
		Map map = (Map) ActionContext.getContext().getSession();
		
		Map<String, String> cookieMap = new HashMap<String, String>();

		Cookie[] cookies = rq.getCookies();
		
		//System.out.println("LoginInteceptor_cookies="+cookies);
		//�������
		for(Iterator it = map.keySet().iterator();it.hasNext();){
			String key = (String)it.next();
			System.out.println("LoginInteceptor_sessionKey="+key);
			System.out.println("LoginInteceptor_sessionValue="+map.get(key));
		}
		
		
		for(Cookie cookie : cookies){
			System.out.println("LoginInteceptor_cookieName="+cookie.getName());
			System.out.println("LoginInteceptor_cookieValue="+URLDecoder.decode(cookie.getValue(),"UTF-8"));
		}
		//�������
		if (map == null) {
			
			return "unLoginUser";
		} else {
			if (map.get("userBean") != null) {
				return invocation.invoke();
			} else {
				
				if(cookies!=null){
					for (Cookie cookie : cookies) {

						cookieMap.put(cookie.getName(), URLDecoder.decode(cookie.getValue(),"UTF-8"));
					}

					String userName = cookieMap.get("username");
					String password = cookieMap.get("password");
					
//					System.out.println(userName+","+password);

					if (userService.loginJugeMent(userName, password)) {
						return invocation.invoke();
						
						
					} else {
						return "unLoginUser";
					}
				}else{
					return "unLoginUser";
				}
			}
		}

	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -