[Java]利用攔截器和自定義註解作登陸以及權限驗證

1.自定義註解html

須要驗證登陸的註解java

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
  */
}

  不須要驗證登陸的註解web

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
  */
}

攔截器實現spring

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

...............
<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>
.................
相關文章
相關標籤/搜索