001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.web.validate.interceptor;
017
018import com.alibaba.fastjson.JSON;
019import com.alibaba.fastjson.JSONObject;
020import com.alibaba.fastjson.JSONPath;
021import com.jfinal.aop.Interceptor;
022import com.jfinal.aop.Invocation;
023import com.jfinal.log.Log;
024import io.jboot.Jboot;
025import io.jboot.utils.AnnotationUtil;
026import io.jboot.utils.ArrayUtil;
027import io.jboot.utils.StrUtil;
028import io.jboot.web.validate.FormType;
029import io.jboot.web.validate.RegexForm;
030import io.jboot.web.validate.RegexValidate;
031
032/**
033 * 验证拦截器
034 */
035public class RegexValidateInterceptor implements Interceptor {
036
037    private static final Log LOG = Log.getLog("Validate");
038
039    @Override
040    public void intercept(Invocation inv) {
041
042        RegexValidate regexValidate = inv.getMethod().getAnnotation(RegexValidate.class);
043        if (regexValidate != null && !validateRegex(inv, regexValidate)) {
044            if (Jboot.isDevMode()){
045                LOG.error(ValidateInterceptorUtil.buildErrorMessage(inv,"@RegexValidate"));
046            }
047            return;
048        }
049
050        inv.invoke();
051    }
052
053
054
055
056    /**
057     * 正则验证
058     *
059     * @param inv
060     * @param regexValidate
061     * @return
062     */
063    private boolean validateRegex(Invocation inv, RegexValidate regexValidate) {
064        RegexForm[] forms = regexValidate.value();
065        if (ArrayUtil.isNullOrEmpty(forms)) {
066            return true;
067        }
068
069
070        for (RegexForm form : forms) {
071            String formName = AnnotationUtil.get(form.name());
072            String formType = AnnotationUtil.get(form.type());
073            if (StrUtil.isBlank(formName)) {
074                throw new IllegalArgumentException("@RegexForm.value must not be empty in " + inv.getController().getClass().getName() + "." + inv.getMethodName());
075            }
076            String value = null;
077            if (FormType.FORM_DATA.equalsIgnoreCase(formType)) {
078                value = inv.getController().getPara(formName);
079            } else if (FormType.RAW_DATA.equalsIgnoreCase(formType)) {
080                try {
081                    JSONObject json = JSON.parseObject(inv.getController().getRawData());
082                    if (json != null) {
083                        Object tmp = JSONPath.eval(json, "$." + formName);
084                        if (tmp != null) {
085                            value = tmp.toString();
086                        }
087                    }
088                } catch (Exception e) {
089                    value = null;
090                }
091            } else {
092                throw new IllegalArgumentException("@RegexValidate not support form type : " + formType + ", " +
093                        "see : io.jboot.web.controller.validate.FormType");
094            }
095
096            if (value == null || !value.trim().matches(form.regex())) {
097                ValidateInterceptorUtil.renderValidException(inv.getController()
098                        , AnnotationUtil.get(regexValidate.renderType())
099                        , formName
100                        , AnnotationUtil.get(form.message())
101                        , AnnotationUtil.get(regexValidate.redirectUrl())
102                        , AnnotationUtil.get(regexValidate.htmlPath())
103                        , form.errorCode()
104                );
105                return false;
106            }
107        }
108
109        return true;
110    }
111
112
113
114
115
116}