关闭

spring自定义注解完成登录功能

标签: spring自定义注解登录spring切面
137人阅读 评论(0) 收藏 举报
package com.smi.am.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.smilife.core.common.valueobject.BaseValueObject;

/**
 * 拦截controller登录认证注解类
 * 
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface LoginAuth {
	@SuppressWarnings("rawtypes")
	Class value() default BaseValueObject.class;
}

package com.smi.am.aspect;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import com.smi.am.constant.SmiConstants;
import com.smi.am.utils.SessionManager;
import com.smilife.core.exception.NoLoginException;


/**
 * 登录认证切面类
 * 
 */
@Aspect
@Component
public class LoginAuthAspect {

	@Around(value = "@annotation( com.smi.am.annotation.LoginAuth)")
	public Object aroundManager(ProceedingJoinPoint pj) throws Exception {
		Object amuser = SessionManager.getAttribute(getRequest(pj.getArgs()), SmiConstants.USERNAME);
		if(amuser == null){
			//用户未登录
			throw new NoLoginException("用户未登录");
		}
		try {
			amuser = pj.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return amuser;
	}


	private HttpServletRequest getRequest(Object[] args) {
		HttpServletRequest result = null;
		if (args != null && args.length > 0) {
			for (Object arg : args) {
				if (arg instanceof HttpServletRequest) {
					result = (HttpServletRequest) arg;
					break;
				}
			}
		}
		return result;
	}
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4194次
    • 积分:127
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:6篇
    • 译文:0篇
    • 评论:1条
    文章分类
  • 3(1)
    最新评论