All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fastchar.openapi.interceptor.FastOpenAPIInterceptor Maven / Gradle / Ivy

The newest version!
package com.fastchar.openapi.interceptor;

import com.fastchar.core.FastAction;
import com.fastchar.core.FastChar;
import com.fastchar.core.FastDispatcher;
import com.fastchar.interfaces.IFastRootInterceptor;
import com.fastchar.openapi.FastCharOpenAPI;
import com.fastchar.openapi.FastCharOpenAPIConfig;
import com.fastchar.openapi.core.FastOpenAPIHelper;
import com.fastchar.openapi.rapidoc.FastRAPIDocHelper;
import com.fastchar.servlet.http.FastHttpServletRequest;
import com.fastchar.servlet.http.FastHttpServletResponse;
import com.fastchar.utils.FastMD5Utils;

public class FastOpenAPIInterceptor implements IFastRootInterceptor {

    @Override
    public void onInterceptor(FastHttpServletRequest request, FastHttpServletResponse response, FastDispatcher dispatcher) throws Exception {
        FastCharOpenAPIConfig config = FastChar.getConfig(FastCharOpenAPIConfig.class);

        String contentUrl = dispatcher.getContentUrl();
        if (contentUrl.equalsIgnoreCase(config.getOpenApiRoute())) {
            FastAction fastAction = FastChar.getActions().newAction(contentUrl, request, response);
            if (checkLogin(config, fastAction)) {
                fastAction.responseHtml(FastRAPIDocHelper.buildHtml(fastAction.getProjectHost(),
                        fastAction.getProjectHost() + "openapi/json",
                        config.getTitle(), config.getUiPrimaryColor(), config.getUiLogo()));
            }


        } else if (contentUrl.equalsIgnoreCase(config.getOpenApiRoute() + "/json")) {
            FastAction fastAction = FastChar.getActions().newAction(contentUrl, request, response);
            if (checkLogin(config, fastAction)) {
                FastCharOpenAPI fastCharOpenAPI = FastChar.getOverrides().newInstance(FastCharOpenAPI.class);
                fastAction.responseJson(fastCharOpenAPI.buildOpenAPI(fastAction.getProjectHost()));
            }
        } else if (contentUrl.equalsIgnoreCase(config.getOpenApiRoute() + "/login")) {
            FastAction fastAction = FastChar.getActions().newAction(contentUrl, request, response);

            String user = fastAction.getParam("user");
            String pwd = fastAction.getParam("pwd");
            if (FastMD5Utils.MD5(config.getUsername()).equalsIgnoreCase(user)
                    && FastMD5Utils.MD5(config.getPassword()).equalsIgnoreCase(pwd)) {
                fastAction.setSession(config.getLoginKey(), "true");
                fastAction.responseJson(0, "登录成功!");
            } else {
                fastAction.responseJson(-1, "登录失败!用户名或密码错误!");
            }
        } else {
            dispatcher.invoke();
        }
    }


    private boolean checkLogin(FastCharOpenAPIConfig config, FastAction fastAction) {
        if (config.isNeedLogin()) {
            String login = fastAction.getSession(config.getLoginKey());
            if (login == null) {
                fastAction.responseHtml(FastOpenAPIHelper.buildLoginHtml(fastAction.getProjectHost(),
                        config.getTitle(), config.getUiPrimaryColor(), config.getUiLogo()));
                return false;
            }
        }
        return true;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy