[Java]利用攔截器和自定義注解做登錄以及權限驗證


1.自定義注解

需要驗證登錄的注解

package com.etaofinance.wap.common;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Retention(RetentionPolicy.RUNTIME)//
@Target({ElementType.METHOD, ElementType.TYPE})//該注解修飾類中的方法
@Inherited
public @interface RequireLogin{
 /**
  * 登錄驗證注解
  * 該注解可以標記Controller 或 Controller 中的方法.
  * 如果Controller 有該標記,那么這個Controller下面所有的方法都會被過濾器
  * 進行驗證
  * 如果Controller 沒有有該標記,但Controller中的某個方法擁有該標記
  * 那么這個方法將被過濾器驗證(其他沒有被標記的不會被驗證)
  * 
  * 特別注意,如果一個Controller 被標記RequireLogin 需要驗證
  * 但是其中某些方法不想被驗證.請參見NoRequireLogin標記
  * 
  * 茹化肖 2016年3月30日10:51:13
  */
}

  不需要驗證登錄的注解

package com.etaofinance.wap.common;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Retention(RetentionPolicy.RUNTIME)//
@Target(ElementType.METHOD)//該注解修飾類中的方法
@Inherited
public @interface NoRequireLogin{
 /**
  * 不需要登錄驗證的方法注解注解
  * 該注解在Controller 標記了 RequireLogin 特性時
  * 某個方法不需要驗證登錄,那么為該方法標記該注解
  * 茹化肖 2016年3月30日10:47:16
  */
}

攔截器實現

package com.etaofinance.wap.common;

import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.etaofinance.core.util.JsonUtil;
import com.etaofinance.core.util.PropertyUtils;
import com.etaofinance.entity.common.HttpResultModel;
/**
 * 權限攔截器
 * @author ofmyi_000
 *
 */
public class AuthInteceptor extends HandlerInterceptorAdapter {

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String basePath =PropertyUtils.getProperty("java.wap.url");
		if (handler instanceof HandlerMethod) {
			HandlerMethod myHandlerMethod = (HandlerMethod) handler;
	        Object bean = myHandlerMethod.getBean();
	        Method method= myHandlerMethod.getMethod();
	        Annotation classAnnotation = bean.getClass().getAnnotation(RequireLogin.class);//類上有該標記
	        Annotation methodAnnotation=method.getAnnotation(RequireLogin.class);//方法上有該標記
	        Annotation methodNologinAnnotation=method.getAnnotation(NoRequireLogin.class);//
	        if((classAnnotation!=null&&methodNologinAnnotation==null)
	        		||(classAnnotation==null&&methodAnnotation!=null))
	        {
	        	boolean isLogin = LoginUtil.checkIsLogin(request,response);
	        	if(isLogin)
	        		return true;
	        	else{//未登錄
	        		if(isAjax(request)){
	        			//Ajax請求返回JSON
	        			HttpResultModel<Object> rep=new HttpResultModel<Object>();
	        			rep.setCode(-1);
	        			rep.setMsg("請登錄后操作!");
	        			String data = JsonUtil.obj2string(rep);
	        	        response.setHeader("content-type", "text/html;charset=UTF-8");
	        	        OutputStream out = response.getOutputStream();
	        	        out.write(data.getBytes("UTF-8"));
	        	        return false;
	        		}
	        		response.sendRedirect(basePath);
	        		
	        	}//IF LOGIN END
	        }//if Annotation end
		}
		return true;
	}
	private boolean isAjax(HttpServletRequest request){
		String requestType = request.getHeader("X-Requested-With");
		if (requestType != null && requestType.equals("XMLHttpRequest")) {
			return true;
		}
		return false;
	}
}

  

XML攔截器配置

...............
<mvc:interceptors>
        <bean class="com.etaofinance.wap.common.GlobalLogInteceptor">
            <property name="sourceSys" value="etaofinancewap"></property>
        </bean>
        <bean class="com.etaofinance.wap.common.AuthInteceptor" />
    </mvc:interceptors>
.................

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM