自定义注解-验验证接口权限

如果你不相信努力和时光,那么成果就会是第一个选择辜负你的。不要去否定你自己的过去,也不要用你的过去牵扯你现在的努力和对未来的展望。不是因为拥有希望你才去努力,而是去努力了,你才有可能看到希望的光芒。自定义注解-验验证接口权限,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

有如下需求:在没有权限管理的情况下阻挡普通用户的接口请求,可以做自定义验证来验证接口权限。

 1、拦截器

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 未登录拦截器
 */
public class BusinessLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取HttpSession对象
        HttpSession httpSession = request.getSession();
        // 获取我们登录后存在session中的用户信息,如果为空,表示session已经过期
        Object sessionAttribute = httpSession.getAttribute(CommonConstant.PURCHASE_SHOPKEEPER);
        // 状态码2000 表示没登录
        if (null == sessionAttribute) {
            throw new LoginToException(ReturnCode.NO_LOGIN.getCode(), CommonConstant.NO_LOGIN);
        }
        String storeId = (String) WebUtils.getSessionAttribute(request, CommonConstant.STORE_ID);
        // 不是管理员必须有店铺id
        if (!isAdmin(request) && storeId == null) {
            //非法请求,未选择店铺!
            returnJson(response, ReturnCode.ILLEGAL_REQUEST_NO_STORE_SELECTED);
            return false;
        }
        // 判断是不是管理员 使用自定义注解
        if (handler instanceof HandlerMethod) {
            //自定义注解
            CheckAdmin annotation = AnnotationUtils.findAnnotation(((HandlerMethod) handler).getMethod(), CheckAdmin.class);
            if (annotation != null) {
                if (!isAdmin(request)) {
                    //没有权限
                    returnJson(response, ReturnCode.USER_NO_PERMISSION_LOCKED_ERROR);
                    return false;
                }
            }
        }
        //已经登录
        return true;
    }

    /**
     * @Description: 判断是不是管理员
     * @Param [request]
     * @Return: boolean
     **/
    private boolean isAdmin(HttpServletRequest request) {
        BusinessLoginRequest businessLoginRequest = (BusinessLoginRequest) request.getSession().getAttribute(CommonConstant.PURCHASE_SHOPKEEPER);
        return businessLoginRequest.getAccount().equals(CommonConstant.ADMIN);
    }

    /**
     * @Description: json异常信息
     * @Param [response, result]
     * @Return: void
     **/
    private void returnJson(HttpServletResponse response, ReturnCode result) throws IOException {
        String json = JSON.toJSONString(Result.Error(result));
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        response.getWriter().println(json);
    }
}

2、自定义注解

import java.lang.annotation.*;

/**
 * @Description: 判断是否admin账号
 */
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CheckAdmin {

}

3、接口使用(直接放在接口方法上面) 

自定义注解-验验证接口权限

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/192790.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!