Search in sources :

Example 1 with AuthorizeType

use of com.alibaba.otter.shared.common.model.user.AuthorizeType in project otter by alibaba.

the class AuthContextValve method invoke.

public void invoke(PipelineContext pipelineContext) throws Exception {
    TurbineRunData rundata = getTurbineRunData(request);
    // TODO 走 api 的验证
    if (isAPI(rundata)) {
        if (apiAuthService.auth(rundata)) {
            pipelineContext.invokeNext();
        } else {
            // TODO 需要转跳到 json 格式的 link
            redirect(pipelineContext, rundata, forbiddenLink);
        }
        return;
    }
    // 得到请求URL相对路径(不包含域名/端口信息)
    String requestUrl = rundata.getRequest().getRequestURI();
    List<AuthorizeType> result = urlAnalyze.check(requestUrl);
    String action = StringUtil.toCamelCase(trimToNull(rundata.getParameters().getString(actionParam)));
    String eventName = getEventName();
    // 首先判断是否登录
    User user = (User) rundata.getRequest().getSession().getAttribute(WebConstant.USER_SESSION_KEY);
    if (StringUtils.isNotEmpty(action)) {
        result.addAll(urlAnalyze.check(action, eventName));
    }
    if (result.isEmpty()) {
        // 访问的连接不符合权限匹配规则,跳转到登录页面
        redirect(pipelineContext, rundata, forbiddenLink);
    } else {
        if (null == user) {
            // 2.如果集合中只包含匿名权限,则通过。
            if (result.contains(AuthorizeType.OPERATOR) || result.contains(AuthorizeType.ADMIN)) {
                redirect(pipelineContext, rundata, loginLink);
            } else {
                pipelineContext.invokeNext();
            }
        } else {
            // 2.如果权限集合有高于用户权限,则跳转到登录页面。
            if (compareAuth(user.getAuthorizeType(), result)) {
                pipelineContext.invokeNext();
            } else {
                redirect(pipelineContext, rundata, forbiddenLink);
            }
        }
    }
}
Also used : AuthorizeType(com.alibaba.otter.shared.common.model.user.AuthorizeType) User(com.alibaba.otter.shared.common.model.user.User) TurbineRunData(com.alibaba.citrus.turbine.TurbineRunData) TurbineUtil.getTurbineRunData(com.alibaba.citrus.turbine.util.TurbineUtil.getTurbineRunData)

Aggregations

TurbineRunData (com.alibaba.citrus.turbine.TurbineRunData)1 TurbineUtil.getTurbineRunData (com.alibaba.citrus.turbine.util.TurbineUtil.getTurbineRunData)1 AuthorizeType (com.alibaba.otter.shared.common.model.user.AuthorizeType)1 User (com.alibaba.otter.shared.common.model.user.User)1