自定义注解验证是否登录
自定义必须登录注解类
1 @Target(ElementType.METHOD) 2 @Retention(RetentionPolicy.RUNTIME) 3 public @interface MustLogin { 4 5 }
登录拦截器
1 public class LoginInterceptor extends AbstractInterceptor { 2 3 @Override 4 public String intercept(ActionInvocation ai) throws Exception { 5 HttpServletRequest request = ServletActionContext.getRequest(); 6 ActionProxy proxy = ai.getProxy(); 7 String methodString = proxy.getMethod(); 8 Object action1 = proxy.getAction(); 9 Method method = action1.getClass().getMethod(methodString); 10 11 //如果用户未登录自动跳转到登录页面 12 if(AssertUtil.isEmpty(LoginUtil.getAccount())){ 13 for(Annotation annotation : method.getAnnotations()){ 14 if(annotation.annotationType() == MustLogin.class){ 15 return "login"; 16 } 17 } 18 } 19 20 return null; 21 } 22 }
调用自定义注解登录拦截器方法
1 @MustLogin 2 public void test(){ 3 4 }
浙公网安备 33010602011771号