io.jboot.web.controller.JbootController Java Examples

The following examples show how to use io.jboot.web.controller.JbootController. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NotNullParaInterceptor.java    From jboot-admin with Apache License 2.0 6 votes vote down vote up
private void renderError(Invocation inv, String param, String errorRedirect) {
    if (StrUtils.isNotBlank(errorRedirect)) {
        inv.getController().redirect(errorRedirect);
        return;
    }

    Controller controller = inv.getController();
    if (controller instanceof JbootController) {
        JbootController jc = (JbootController) controller;
        if (jc.isAjaxRequest()) {
            jc.renderJson(RestResult.buildError("参数["+param+"]不可为空"));
            return;
        }
    }
    controller.setAttr(BusinessExceptionInterceptor.MESSAGE_TAG, "参数["+param+"]不可为空").render(exceptionView);
}
 
Example #2
Source File: BusinessExceptionInterceptor.java    From jboot-admin with Apache License 2.0 6 votes vote down vote up
@Override
public void intercept(Invocation inv) {
	try {
		inv.invoke();
	} catch (JbootException e) {
		if (inv.getTarget() instanceof JbootController) {
			JbootController controller = inv.getTarget();

			if (controller.isAjaxRequest()) {
				RestResult<String> restResult = new RestResult<String>();
				restResult.error(e.getMessage());
				controller.renderJson(restResult);
			} else {
				controller.setAttr(MESSAGE_TAG, e.getMessage()).render(exceptionView);
			}
		}
	}
}
 
Example #3
Source File: JwtShiroInvokeListener.java    From jboot-admin with Apache License 2.0 6 votes vote down vote up
@Override
public void onInvokeBefore(FixedInvocation inv) {
    JbootController controller = (JbootController) inv.getController();
    String jwtToken = controller.getHeader(JwtManager.me().getHttpHeaderName());

    if (StrUtils.isBlank(jwtToken)) {
        inv.invoke();
        return;
    }

    Map jwtParas = JwtManager.me().getParas();
    String userId = String.valueOf(jwtParas.get("userId"));

    AuthenticationToken token = new JwtAuthenticationToken(userId, jwtToken);

    try {
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
    } catch (Exception e) {
        log.error(e.getMessage());
    }
}
 
Example #4
Source File: JwtInterceptor.java    From jboot with Apache License 2.0 4 votes vote down vote up
private void processInvoke(Invocation inv, Map oldData) {

        inv.invoke();


        if (!(inv.getController() instanceof JbootController)) {
            return;
        }

        JbootController jbootController = (JbootController) inv.getController();
        Map<String, Object> jwtMap = jbootController.getJwtAttrs();


        if (jwtMap == null || jwtMap.isEmpty()) {
            refreshIfNecessary(inv, oldData);
            return;
        }

        String token = JwtManager.me().createJwtToken(jwtMap);
        HttpServletResponse response = inv.getController().getResponse();
        response.addHeader(JwtManager.me().getHttpHeaderName(), token);
    }
 
Example #5
Source File: JbootAopFactory.java    From jboot with Apache License 2.0 4 votes vote down vote up
@Override
protected void doInject(Class<?> targetClass, Object targetObject) throws ReflectiveOperationException {
    targetClass = getUsefulClass(targetClass);
    Field[] fields = targetClass.getDeclaredFields();

    if (fields.length != 0) {

        for (Field field : fields) {

            Inject inject = field.getAnnotation(Inject.class);
            if (inject != null) {
                Bean bean = field.getAnnotation(Bean.class);
                String beanName = bean != null ? AnnotationUtil.get(bean.name()) : null;
                if (StrUtil.isNotBlank(beanName)) {
                    doInjectByName(targetObject, field, inject, beanName);
                } else {
                    doInjectJFinalOrginal(targetObject, field, inject);
                }
                continue;
            }

            ConfigValue configValue = field.getAnnotation(ConfigValue.class);
            if (configValue != null) {
                doInjectConfigValue(targetObject, field, configValue);
                continue;
            }

            RPCInject rpcInject = field.getAnnotation(RPCInject.class);
            if (rpcInject != null) {
                doInjectRPC(targetObject, field, rpcInject);
                continue;
            }
        }
    }


    // 是否对超类进行注入
    if (injectSuperClass) {
        Class<?> c = targetClass.getSuperclass();
        if (c != JbootController.class
                && c != Controller.class
                && c != JbootServiceBase.class
                && c != Object.class
                && c != JbootModel.class
                && c != Model.class
                && c != null
        ) {
            doInject(c, targetObject);
        }
    }
}
 
Example #6
Source File: ApiInterceptor.java    From jpress with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void intercept(Invocation inv) {

    // API 功能未启用
    if (apiEnable == false) {
        inv.getController().renderJson(Ret.fail().set("message", "API功能已经关闭,请管理员在后台进行开启"));
        return;
    }

    if (StrUtil.isBlank(apiAppId)) {
        inv.getController().renderJson(Ret.fail().set("message", "后台配置的 APP ID 不能为空,请先进入后台的接口管理进行配置。"));
        return;
    }

    if (StrUtil.isBlank(apiSecret)) {
        inv.getController().renderJson(Ret.fail().set("message", "后台配置的 API 密钥不能为空,请先进入后台的接口管理进行配置。"));
        return;
    }

    JbootController controller = (JbootController) inv.getController();
    String queryString = controller.getRequest().getQueryString();
    if (StrUtil.isBlank(queryString)) {
        inv.getController().renderJson(Ret.fail().set("message", "请求参数错误。"));
        return;
    }
    Map<String, String> parasMap = paramToMap(queryString);

    String appId = parasMap.get("appId");
    if (StrUtil.isBlank(appId)) {
        inv.getController().renderJson(Ret.fail().set("message", "在Url中未获取到appId内容,请注意Url是否正确。"));
        return;
    }


    if (!appId.equals(apiAppId)) {
        inv.getController().renderJson(Ret.fail().set("message", "客户端配置的AppId和服务端配置的不一致。"));
        return;
    }

    String sign = parasMap.get("sign");
    if (StrUtil.isBlank(sign)) {
        controller.renderJson(Ret.fail("message", "签名数据不能为空,请提交 sign 数据。"));
        return;
    }

    String timeStr = parasMap.get("t");
    Long time = timeStr == null ? null : Long.valueOf(timeStr);
    if (time == null) {
        controller.renderJson(Ret.fail("message", "时间参数不能为空,请提交 t 参数数据。"));
        return;
    }

    // 时间验证,可以防止重放攻击
    if (Math.abs(System.currentTimeMillis() - time) > TIMEOUT) {
        controller.renderJson(Ret.fail("message", "请求超时,请重新请求。"));
        return;
    }

    String localSign = createLocalSign(controller);
    if (sign.equals(localSign) == false) {
        inv.getController().renderJson(Ret.fail().set("message", "数据签名错误。"));
        return;
    }

    Object userId = controller.getJwtPara(JPressConsts.JWT_USERID);
    if (userId != null) {
        controller.setAttr(JPressConsts.ATTR_LOGINED_USER, userService.findById(userId));
    }

    inv.invoke();
}
 
Example #7
Source File: UTMInterceptor.java    From jpress with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void doRecordUTM(Invocation inv) {
    Controller controller = inv.getController();

    Utm utm = new Utm();
    utm.setId(StrUtil.uuid());
    utm.setActionKey(controller.getRequest().getRequestURI());
    utm.setActionQuery(controller.getRequest().getQueryString());
    utm.setIp(RequestUtil.getIpAddress(controller.getRequest()));
    utm.setAgent(RequestUtil.getUserAgent(controller.getRequest()));
    utm.setReferer(RequestUtil.getReferer(controller.getRequest()));


    String uid = CookieUtil.get(controller, JPressConsts.COOKIE_UID);
    if (StrUtil.isNotBlank(uid)) {
        utm.setUserId(Long.valueOf(uid));
    }

    /**
     * 可能是API的用户,API 通过 jwt 获取用户信息
     */
    else if (controller instanceof JbootController) {
        JbootController c = (JbootController) controller;
        Number userId = c.getJwtPara(JPressConsts.JWT_USERID);
        if (userId != null) {
            utm.setUserId(userId.longValue());
        }
    }

    /**
     * 当用户未登录的情况下,创建匿名记录
     */
    else {
        //anonym
        String anonym = CookieUtil.get(controller, JPressConsts.COOKIE_ANONYM);
        if (StrUtil.isNotBlank(anonym)) {
            utm.setAnonym(anonym);
        } else {
            CookieUtil.put(controller, JPressConsts.COOKIE_ANONYM, StrUtil.uuid(), 60 * 60 * 24 * 365);
        }
    }

    utm.setSource(controller.getPara("source"));
    utm.setMedium(controller.getPara("medium"));
    utm.setCampaign(controller.getPara("campaign"));
    utm.setContent(controller.getPara("content"));
    utm.setTerm(controller.getPara("term"));

    utmService.doRecord(utm);
}