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
*/
}
不需要验证登录的注解
packagecom.etaofinance.wap.common;importjava.lang.annotation.Documented;importjava.lang.annotation.ElementType;importjava.lang.annotation.Inherited;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;
@Documented
@Retention(RetentionPolicy.RUNTIME)//@Target(ElementType.METHOD)//该注解修饰类中的方法
@Inheritedpublic @interfaceNoRequireLogin{/*** 不需要登录验证的方法注解注解
* 该注解在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 rep=new HttpResultModel();
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拦截器配置
...............
.................