在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");
}
}
}
完成。
分享到:
相关推荐
全基于annotaion的一个应用.通个注解的方式控制事务等 对于之前分要的太高了。给于补尝. 有兴趣的欢迎来群:41229007 共同学习
Spring3.0中annotaion的运用
该项目使用了JBPM4,并对JBPM4进行预研,掌握JBPM4的使用,主要是JPDL和JBPM4常用API,并完成Extjs3+Struts2+Spring+Hibernate+JBPM4+Annotaion框架的搭建和规划,并对故障单部分进行设计和编码,应用工作流JBPM4...
主要介绍了 Spring Annotaion Support详细介绍及简单实例的相关资料,需要的朋友可以参考下
hibernate annotaion api 中文与英文版 及 hibernate api
详细介绍hibernate annotaion的书写,一个简单的程序,适合初学者
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
以前spring开发需要配置一大堆的xml,后台spring加入了annotaion,使得xml配置简化了很多,当然还是有些配 置需要使用xml,比如申明component scan等。前段时间发现了spring开了一个新的model spring boot,主要思想是...
前沿技术,大家可以学习这项技术 这只是一个例子而已,自己研究吧
SpringMVC服务器验证一种是有两种方式,一种是基于Validator接口,一种是使用Annotaion JSR-303标准的验证,本篇文章主要介绍,有兴趣的可以了解一下。
smartTrinity_script_9_trinity_fa_annotaion_GPL2.0_20230324.zip
这是利用java Annotation实现的菜单和工具栏国际化的实例,是《java学习脚印: 反射与注释(Annotation)》一文中综合实例的例子。博客参见:http://blog.csdn.net/wangdingqiaoit/article/details/20130539
struts2 annotation Convention plugin允许action类通过注解来改变映射的url
自定义注解,快速实现具有springboot 和swagger开发resapi
Iono.Container关于扩展照明/容器的Spring框架样式容器库Iono.Container 是一个 PHP 服务容器(带注解) 像 Spring 框架样式注释 (java) 安装composer.json " require " : { " php " : " >=5.4 " , " ext-tokenizer ...
abhc “ABHC”指Annotaion Binding Http Cilent。 这是和俊英一起学习的
1、Annotaion 这无疑是最简洁、优雅的方式 2、控制台配置 3、java原生 项目如何接入统一配置? 主要通过三种方式:这三种都需要添加相关的mvn依赖(jar包), 然后就可以在你的项目使用Annotation了 1、spring接入 2...
获取注释方法行家插件 要运行Maven插件: 将插件包括在您的项目中。 例如,从cmd行执行mvn get-annotation-methods:find -DinterfaceNames =“ Event,XmlElement,Event3”。 这将返回包含以上任何注释名称的...
jackson的注解包 2.2.3版本 jackson-annotaion-2.2.3.jar