`
www-hello
  • 浏览: 99051 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

flex + Spring + Annotaion 权限(登录)验证

阅读更多

     在Flex开发中,Flex端后台(Java)时,不仅要在前台根据权限对一些功能进行屏蔽限制,也要在后台进行拦截,下面使用spring的aop和自定义annotation来演示登录管理功能。
    自定义annotation: AuthLogin,为了对一些不需要登录验证的方法进行标记。

 

package test.auth;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//注解保持到运行时
@Retention(value=RetentionPolicy.RUNTIME)
//注解可以加在方法上
@Target(value={ElementType.METHOD})
public @interface AuthLogin {
	boolean ignore() default false;
	//当ignore为true时,不对调用进行是否登录的验证
}
 

Spring拦截器类AuthPermission,对前台调用进行拦截,如果没有登录,抛出NoLoginException,如果被调用的方法存在注解AuthLogin并且ignore的值为true则进行放行。

package test.auth;

import java.lang.reflect.Method;
import test.constant.TscConstant;
import test.exception.NoLoginException;
import test.user.model.UserVO;
import test.util.HttpServletUtils;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

public class AuthPermission {

	private static final Logger log = Logger.getLogger(AuthPermission.class);

	public void doAccessCheck(JoinPoint joinPoint) throws Throwable {

		// 获取连接点的方法签名对象
		MethodSignature joinPointObject = (MethodSignature) joinPoint.getSignature();
		// 连接点对象的方法
		Method method = joinPointObject.getMethod();

		AuthLogin loginAnnotion = method.getAnnotation(AuthLogin.class);
		//取被调用方法上的注解
		System.out.println("------" + joinPoint.getTarget().getClass().getName() + "." + method.getName() + "(...)");
		if (loginAnnotion != null) {
			//注解 AuthLogin存在,并且ignore的值为true,忽略验证
			if (loginAnnotion.ignore() == true) {
				// 忽略登录验证
			} else {
				//验证登录
				isLogin();
			}
		} else {
				//验证登录
			isLogin();
		}
	}

	private void isLogin() {
		//session中取登录成功后,放入的标记对象
UserVO user = (UserVO) HttpServletUtils.getSession().getAttribute(TscConstant.LOGIN_USER);
		if (user == null) {
			log.error("登录验证失败,IP:" + HttpServletUtils.getRequestIp());
			throw new NoLoginException("请重新登录");
		}
	}
}
 

没有登录Exception:

 

package test.exception;

public class NoLoginException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	public NoLoginException() {
		super();
	}

	public NoLoginException(String message, Throwable cause) {
		super("[" + message + "]", cause);
	}

	public NoLoginException(String message) {
		super("[" + message + "]");
	}

	public NoLoginException(Throwable cause) {
		super(cause);
	}

}
 

 

spring配制文件中对aop进行配制:对所有business包中的方法进行拦截(Before前置通知)。

<bean id="checklogin" class="test.auth.AuthPermission" />
	
<aop:config>  
	<aop:pointcut id="invokeAuth"   
		  expression="execution(* test.*.business.*.*(..))"/>  
	<aop:aspect id="loginAspect" ref="checklogin">  
		<aop:before pointcut-ref="invokeAuth" method="doAccessCheck"/>  
	</aop:aspect>  
</aop:config>  
 

 

Flex端对异常进行捕获,并进行提示:
如使用RemoteObject对象的fault引用的方法进行提示(fault=”MyAlert.show(event.fault.faultString);”),
自定义的Alert类

package test.swf.util
{
	import flash.display.Sprite;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	
	public class MyAlert extends Alert
	{
		private static const NO_LOGIN_EXCEPTION:String = "test.exception.NoLoginException";
		
		public static function show(text:String = "", title:String = "",
									flags:uint = 0x4 /* Alert.OK */, 
									parent:Sprite = null, 
									closeHandler:Function = null, 
									iconClass:Class = null, 
									defaultButtonFlag:uint = 0x4 /* Alert.OK */):Alert
		{
			if(text == null || text.length ==0){
				text = "";
			}
			else
			{
				if(text.indexOf(NO_LOGIN_EXCEPTION,0)!= -1){
				//捕获到NoLoginException,改变消息框关闭函数,使其跳转到登录页
					closeHandler = toLoginPage;
				}
				
				var startIndex:int = text.indexOf("[",0);
				var endIndex:int = text.indexOf("]",0);
				if(startIndex != -1 && endIndex != -1 && startIndex < endIndex){
					text= text.substring(text.indexOf("[") ,text.indexOf("]"));
				}
			}
			return Alert.show( text, title,
				flags, 
				parent, 
				closeHandler, 
				iconClass, 
				defaultButtonFlag);                    	
		}
		//跳转到登录页,简单实现
		private static function toLoginPage(event:CloseEvent = null):void{
			var url:URLRequest=new URLRequest("index.html");
			navigateToURL(url, "_self");
		}
	}
}
 

 

完成。

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics