java 注解 权限_[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

*/

}

不需要验证登录的注解

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拦截器配置

...............

.................

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值