struts2拦截器 获得请求方法名+获得请求参数

阅读更多

SSI框架为基础开发的,hulian平台

struts2拦截器里如何知道你请求的是那个方法
使用:invocation.getInvocationContext().getName(); //输出Priv_queryPriv,这正是我访问的Action中的方法。

 

1.struts.xml中这么定义的


	
	
	
	
	
	
	
		
			
			
				
					
					
						loginMain,loginTop,loginSwitch,loginRight,login,leftMenuShow,
						queryCityList,queryInOrOutAreaList,queryDistricts
					
				
				
			
		
		
		
		
		
			/errorPrivPage.jsp
			/jsp/phone/xxxx.jsp
			/jsp/phone/login/trunToLogin.jsp
		
		
		
			/errorPage.jsp
			
		
	
	
	

	
		${successPath}
		${errorPath}
		${inputPath}
		${redirectActionPath}
		${chainPath}
		${redirectPath}
		
			application/vnd.ms-excel
			inputStream
			filename="${printFileName}"
			1024
		
	

		

 2.Action这么写

/**
 * 权限信息控制
 * @author  ken
 * @date 2011-9-13 下午15:00:46
 */
@Scope("prototype")
@Controller("PrivAction")
public class PrivAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	static final Logger log = Logger.getLogger(PrivAction.class);
	
	@Autowired
	private PrivService privService;
	
	/* 权限模型 */
	private TEmployeePriv employeePriv;
	
	/**
	 * 权限查询
	 * @return
	 */
	public String queryPriv(){
		if(employeePriv==null){
			employeePriv = new TEmployeePriv();
			successPath = "/jsp/phone/priv/priv/privList.jsp";
			return SUCCESS;
		}
		try {
			entitys = this.privService.queryAllPriv(employeePriv);
		} catch (Exception e) {
			log.error("",e);
		}
		
		successPath = "/jsp/phone/priv/priv/privList.jsp?flag=true";
		return SUCCESS;
	}
}

 3.struts2拦截器

 /**
  * 权限拦截器Interceptor
 * @author mengxianjun
 * @date 2011-4-8 下午03:07:24
 *
 */

@SuppressWarnings("serial")
@Component( "PrivInterceptor" )
@Scope("prototype")
public class PrivInterceptor extends MethodFilterInterceptor{
	
	@Resource(name = "EmployeeService")
	private EmployeeService empSafeService;//工号安全Service
	
	@Resource(name="EmployeeRoleService")
	private EmployeeRoleService empRoleService;
	
	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.interceptor.MethodFilterInterceptor#doIntercept(com.opensymphony.xwork2.ActionInvocation)
	 * @author mengxianjun
	 * @date 2011-4-8 下午03:07:24
	 */
	
	@SuppressWarnings("unchecked")
	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {

		System.out.println("============"+invocation.getInvocationContext().getName());
		System.out.println("============"+invocation.getInvocationContext().getLocale());
		System.out.println("============"+invocation.getInvocationContext().getParameters());
		
		
		System.out.println("执行到拦截器里。。。。");
		
		ActionContext act = invocation.getInvocationContext();
		
		//获得session
		Map session = invocation.getInvocationContext().getSession();
		
		TEmployeeInfo sessionInfo = (TEmployeeInfo) session.get("user");
		
		String employee_id="";
		
		/**
		 * 一、是否登录
		 */
		try
		{
			employee_id = sessionInfo.getEmployeeId();
		}
		catch( NullPointerException e )
		{
			act.put("message", "Session过期,请重新登录!");
			return "loginPage";
		}
		
/*=========================================================单点登录判断============================================*/
		HashMap map = (HashMap)  ServletActionContext.getServletContext().getAttribute("userList");
		String sessionID_User = map.get( employee_id ); //登录用户session的ID
		String sessionID_Now = ServletActionContext.getRequest().getSession().getId(); //当前session的ID
		
		if( ! sessionID_User.trim().equals(sessionID_Now) )
		{
			act.put("message", "此账号已登录!");
			return "privError";
		}
/*=========================================================单点登录判断============================================*/
		
		/**
		 * 二、登录成功后,根据URL进行权限判断
		 */
		if( !"".equals(employee_id.trim()) && null!=employee_id )
		{
			/**
			 * 2.1判断工号登录后,业务密码是否为123456,是跳转到商户安全设置,修改业务密码
			 */
			/*TEmployeeSafe empSafe = empSafeService.queryEmployeSafe(employee_id);
			if( null!=empSafe )
			{
				String MD5password = KeyedDigestMD5.getKeyedDigest("123456","").toUpperCase();//获得123456的MD5值
				String employeePass = empSafe.getEmployeePass();//获得登录密码
				String employeePass2 = empSafe.getEmployeePass2();//获得工号业务密码
				if( MD5password.equals(employeePass) || MD5password.equals(employeePass2) )
				{
					act.put("message", "欢迎使用本系统,您的登录密码、业务密码过于简单,请修改!");
					return "updateEmpPassword";
				}
			}*/
			
			
			/**
			 * 2.2截取请求URL
			 */
			HttpServletRequest request = ServletActionContext.getRequest();
			
			String currentURL = request.getRequestURI();
			String targetURL = "";
			
			if( -1 != currentURL.indexOf("?") )//普通
标签是?分隔传来的参数 { String paramURL = currentURL.substring(currentURL.indexOf("?",0), currentURL.length());//参数URL int targetLength = currentURL.length() - paramURL.length();//去掉请求参数Length targetURL = currentURL.substring(currentURL.indexOf("/",1), targetLength); System.out.println("去掉请求参数路径URL:"+targetURL); } else if( -1 != currentURL.indexOf(";") )//struts2标签标签是;分隔传来的参数 { String paramURL = currentURL.substring(currentURL.indexOf(";",0), currentURL.length());//参数URL int targetLength = currentURL.length() - paramURL.length();//去掉请求参数Length targetURL = currentURL.substring(currentURL.indexOf("/",1), targetLength); System.out.println("去掉请求参数路径URL:"+targetURL); } else { targetURL = currentURL.substring(currentURL.indexOf("/",1), currentURL.length()); System.out.println("请求路径URL:"+targetURL); } /** * 2.3必须保证当前用户:1.工号必须开启2.角色已分配 3.角色已启用 4.角色有权限集合 */ if("12".equals(sessionInfo.getState())) { act.put("message", "工号已锁定!"); return "privError"; } else if("15".equals(sessionInfo.getState())) { act.put("message", "工号已注销!"); return "privError"; } else if( sessionInfo.getRoleState()==null || "".equals(sessionInfo.getRoleState()) ) { act.put("message", "未分配角色!"); return "privError"; } else if( !"10".equals(sessionInfo.getRoleState()) ) { act.put("message", "该角色未启用!"); return "privError"; } else { try { /*1.得到中间表TRolePriv集合*/ TRolePriv rp = new TRolePriv(); rp.setRoleNum(sessionInfo.getRoleNum()); List rolePrivList = empRoleService.queryRolePriv(rp); /*2.根据中间表TRolePriv,生成TEmployeePriv集合*/ List privList = new ArrayList(); for( TRolePriv trp : rolePrivList ) { TEmployeePriv myPriv = empRoleService.queryPrivById(trp.getPrivNum()); if(myPriv!=null&&myPriv.getPrivUrl()!=null&&!"".equals(myPriv.getPrivUrl())){ privList.add(myPriv);//去掉一级菜单添加进privList,privUrl为空是一级菜单 } } /*3.权限privUrl与targetURL比较*/ if( privList.size()>0 ) { int privState = 0; for( TEmployeePriv p : privList ) { /** * 对比去掉请求参数后的URL是否一致,即/Login_login */ String privUrl = p.getPrivUrl();//TEmployeePriv中privUrl,可能带参数,可能不带参数 if(-1!=privUrl.indexOf("?",0)){ String paramPrivURL = privUrl.substring(privUrl.indexOf("?",0), privUrl.length());//参数URL int targetPrivLength = privUrl.length() - paramPrivURL.length();//去掉请求参数Length privUrl = privUrl.substring(privUrl.indexOf("/",0), targetPrivLength);//TEmployeePriv中privUrl去掉参数 } if( privUrl.equals(targetURL) ) { privState = 1; } } if( 1 == privState ) { return invocation.invoke(); } else { System.out.println("-------得到Priv权限集合,但是无访问权限---------"); act.put("message", "您没有权限 , 拒绝访问!"); return "privError"; } } else { act.put("message", "您没有相应权限 , 拒绝访问!"); return "privError"; } } catch( NullPointerException e ) { act.put("message", "您没有权限 , 拒绝访问!"); return "privError"; } } } else { act.put("message", "Session过期,请重新登录!"); return "loginPage"; } } }

 输出:

16:05:35,813 INFO  [STDOUT] ============Priv_queryPriv
16:05:35,813 INFO  [STDOUT] ============zh_CN
16:05:35,813 INFO  [STDOUT] ============{}
16:05:35,813 INFO  [STDOUT] 执行到拦截器里。。。。
16:05:35,813 INFO  [STDOUT] 请求路径URL:/Priv_queryPriv

 

=========================================================================================================

 

struts2 拦截拦截器怎么样获得action 后面的参数 例如 login.action? user.user_name='aaaaa'
我想获得到action 后面的参数再做判断,请问怎么样获得呢

问题补充:
我想在拦截器里面得到传入的user.user_name

最佳答案

先在拦截器intercept()方法中获取parameters对象:
  Map paramMap = invocation.getInvocationContext().getParameters();
然后用parameters对象获取参数(是一个字符串数组):
 String[] names = (String[]) paramMap.get("user.user_name");
 String username=names[0];

 

 

 

 

你可能感兴趣的:(struts2拦截器)