spring自定义注解

7 篇文章 0 订阅

通用功能(比如参数验证,登录验证),每次都要写一串代码,太冗余。所以添加自定义注解,通过注解来实现这些通用功能。核心是利用拦截器。

以登录验证为例,效果如下

        @RequestMapping(value = "/atteststatus")
	@ResponseBody
	@LoginRequired
	public OpEntityResult<UsersAttestStatusDTO> getUserAttestStatus(HttpServletRequest request) {

		OpEntityResult<UsersAttestStatusDTO> result = new OpEntityResult<UsersAttestStatusDTO>();

		String userId = Tools.getUserId(request);

		try {
			UsersAttestStatusDTO attestStatus = usersService.getUsersStatus(userId);
			result.setEntity(attestStatus);
			result.setCode(1);
		} catch (Exception ex) {
			logger.error("getUserAttestStatus userId=" + userId + "\n" + ExceptionHelper.getExceptionDetail(ex));
			result.setCode(-1);
			result.setMsg(String.format("获取用户[%s]的数据异常,请和客服联系", userId));
		}

		return result;
	}

其中@LoginRequired即自定义的注解,代表这个接口需要登录后才能访问,否则返回请登录的提示信息。

第一步:定义一个类,类名就是要使用的注解名

/**
 * 在需要登录验证的Controller的方法上使用此注解
 * @author zpf
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginRequired {}

第二步:定义拦截器

package com.jujin.interceptor;

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.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.jujin.annotation.LoginRequired;
import com.jujin.common.OpResult;
import com.jujin.constants.CommonConstants;
import com.jujin.utils.Tools;
import com.pro.common.util.StringUtils;

/**
 * 拦截器 判断用户是否登录
 * 
 * @author zpf
 */
public class AuthenticationInterceptor implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        // 如果不是映射到方法直接通过
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();

        // 判断接口是否需要登录
        LoginRequired methodAnnotation = method.getAnnotation(LoginRequired.class);

        // 有 @LoginRequired 注解,需要认证
        if (methodAnnotation != null) {

            String userId = Tools.getUserId(request);

            // 未登录
            if (StringUtils.isEmpty(userId)) {
                OpResult result = new OpResult();
                result.setCode(0);
                result.setMsg(CommonConstants.NO_LOGIN);

                String json = JSON.toJSONString(result);

                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json; charset=utf-8");

                response.getWriter().print(json);
                return false;
            }
            // 已登录
            else {
                request.setAttribute("user_id", userId);
                return true;
            }
        }

        return true;
    }

}

第三步:spring配置拦截器

        <!-- 拦截器 -->
	<mvc:interceptors>
		<bean class="com.jujin.interceptor.AuthenticationInterceptor" />
	</mvc:interceptors>
完成,可以在controller中使用了
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值