Search in sources :

Example 1 with CoreConfig

use of io.github.ihongs.CoreConfig in project HongsCORE by ihongs.

the class AuthKit method redirect.

/**
 * 登录成功后跳转
 * 依此检查 Parameters,Cookies,Session 中是否有指定返回路径
 * 都没有指定时则跳转到默认地址
 * 默认地址缺失则跳转到网站首页
 * 也可用特殊值要求返回特定数据
 *  _mine_info 用户信息
 *  _sign_info 会话信息
 *  - 无返回信息
 * @param helper
 * @param rst
 * @throws HongsException
 */
public static void redirect(ActionHelper helper, Map rst) throws HongsException {
    String k, v, r;
    CoreConfig cc = CoreConfig.getInstance("oauth2");
    do {
        k = cc.getProperty("oauth2.bak.prm", "r");
        r = v = helper.getParameter(k);
        if (v != null && !v.isEmpty()) {
            break;
        }
        k = cc.getProperty("oauth2.bak.cok");
        if (k != null && !k.isEmpty()) {
            v = (String) helper.getCookibute(k);
            if (v != null && !v.isEmpty()) {
                // 清除 Cookies
                helper.setCookibute(k, null);
                break;
            }
        }
        k = cc.getProperty("oauth2.bak.ses");
        if (k != null && !k.isEmpty()) {
            v = (String) helper.getSessibute(k);
            if (v != null && !v.isEmpty()) {
                // 清除 Session
                helper.setSessibute(k, null);
                break;
            }
        }
        v = cc.getProperty("oauth2.bak.url", Core.SERV_PATH + "/");
    } while (false);
    if ("_mine_info_".equals(r)) {
        Object id = helper.getSessibute(Cnst.UID_SES);
        Map rd = helper.getRequestData();
        rd.put(Cnst.ID_KEY, id);
        new UserAction().getInfo(helper);
    } else if ("_sign_info_".equals(r)) {
        helper.reply(Synt.mapOf("info", rst));
    } else if ("-".equals(r)) {
        helper.reply("");
    } else {
        helper.ensue(v);
    }
}
Also used : UserAction(io.github.ihongs.serv.master.UserAction) CoreConfig(io.github.ihongs.CoreConfig) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with CoreConfig

use of io.github.ihongs.CoreConfig in project HongsCORE by ihongs.

the class QQAction method inWeb.

/**
 * QQ Web 登录回调
 * @param helper
 * @throws HongsException
 */
@Action("web/create")
@CommitSuccess
public void inWeb(ActionHelper helper) throws HongsException {
    CoreConfig cc = CoreConfig.getInstance("oauth2");
    String appId = cc.getProperty("oauth2.qq.web.app.id");
    String appSk = cc.getProperty("oauth2.qq.web.app.key");
    String rurl = cc.getProperty("oauth2.qq.wap.bak.url");
    String code = helper.getParameter("code");
    if (appId == null || appSk == null) {
        helper.error(400, "Not support this mode");
    }
    try {
        Map info = getUserInfo(code, appId, appSk, rurl, false);
        String opnId = (String) info.get("opnid");
        String opuId = (String) info.get("opuid");
        String name = (String) info.get("name");
        String head = (String) info.get("head");
        Map back = AuthKit.openSign(helper, "qq", Synt.defoult(opuId, opnId), name, head);
        // 登记 openId
        if (opnId != null && opuId != null) {
            String usrId = (String) back.get(Cnst.UID_SES);
            setUserSign("qq.web", opnId, usrId);
        }
        AuthKit.redirect(helper, back);
    } catch (HongsException ex) {
        AuthKit.redirect(helper, ex);
    }
}
Also used : CoreConfig(io.github.ihongs.CoreConfig) HongsException(io.github.ihongs.HongsException) HashMap(java.util.HashMap) Map(java.util.Map) Action(io.github.ihongs.action.anno.Action) CommitSuccess(io.github.ihongs.action.anno.CommitSuccess)

Example 3 with CoreConfig

use of io.github.ihongs.CoreConfig in project HongsCORE by ihongs.

the class QQAction method inWap.

/**
 * QQ WAP 登录回调
 * @param helper
 * @throws HongsException
 */
@Action("wap/create")
@CommitSuccess
public void inWap(ActionHelper helper) throws HongsException {
    CoreConfig cc = CoreConfig.getInstance("oauth2");
    String appId = cc.getProperty("oauth2.qq.wap.app.id");
    String appSk = cc.getProperty("oauth2.qq.wap.app.key");
    String rurl = cc.getProperty("oauth2.qq.wap.bak.url");
    String code = helper.getParameter("code");
    if (appId == null || appSk == null) {
        helper.error(400, "Not support this mode");
    }
    try {
        Map info = getUserInfo(code, appId, appSk, rurl, true);
        String opnId = (String) info.get("opnid");
        String opuId = (String) info.get("opuid");
        String name = (String) info.get("name");
        String head = (String) info.get("head");
        Map back = AuthKit.openSign(helper, "qq", Synt.defoult(opuId, opnId), name, head);
        // 登记 openId
        if (opnId != null && opuId != null) {
            String usrId = (String) back.get(Cnst.UID_SES);
            setUserSign("qq.wap", opnId, usrId);
        }
        AuthKit.redirect(helper, back);
    } catch (HongsException ex) {
        AuthKit.redirect(helper, ex);
    }
}
Also used : CoreConfig(io.github.ihongs.CoreConfig) HongsException(io.github.ihongs.HongsException) HashMap(java.util.HashMap) Map(java.util.Map) Action(io.github.ihongs.action.anno.Action) CommitSuccess(io.github.ihongs.action.anno.CommitSuccess)

Example 4 with CoreConfig

use of io.github.ihongs.CoreConfig in project HongsCORE by ihongs.

the class IsFile method stores.

/**
 * 远程文件预先下载到本地
 * 返回临时名称
 * @param href 文件链接
 * @param temp 临时目录
 * @return
 * @throws Wrong
 */
protected String stores(String href, String temp) throws Wrong {
    // 允许不加 http 或 https
    if (href.startsWith("/"))
        href = "http:" + href;
    URL url = null;
    try {
        url = new URL(href);
    } catch (MalformedURLException ex) {
        throw new Wrong(ex, "file.url.has.error", href);
    }
    URLConnection cnn;
    InputStream ins = null;
    FileOutputStream out = null;
    try {
        cnn = url.openConnection();
        ins = cnn.getInputStream();
        // 从响应头中获取到名称
        String name;
        do {
            Pattern pat;
            Matcher mat;
            name = cnn.getHeaderField("Content-Disposition");
            if (name != null) {
                pat = Pattern.compile("filename=\"(.*?)\"");
                mat = pat.matcher(name);
                if (mat.find()) {
                    name = mat.group(1);
                    break;
                }
            }
            name = cnn.getHeaderField("Content-Type");
            if (name != null) {
                pat = Pattern.compile("^\\w+/\\w+");
                mat = pat.matcher(name);
                if (mat.find()) {
                    name = mat.group(0).replace("/", ".");
                    break;
                }
            }
            name = cnn.getURL().getPath().replaceAll("[\\?#].*", "");
        } while (false);
        // 重组名称避免无法存储
        int i = name.lastIndexOf('/');
        if (i != -1) {
            name = name.substring(i + 1);
        }
        int j = name.lastIndexOf('.');
        if (j != -1) {
            String extn;
            extn = name.substring(j + 1);
            name = name.substring(0, j);
            // 检查扩展名
            CoreConfig c = CoreConfig.getInstance("default");
            String d = c.getProperty("fore.upload.deny.extns");
            if (Synt.toTerms(d).contains(extn)) {
                throw new Wrong("fore.form.upload.failed");
            }
            name = URLEncoder.encode(name, "UTF-8") + "." + URLEncoder.encode(extn, "UTF-8");
        } else {
            name = URLEncoder.encode(name, "UTF-8");
        }
        // 加上编号避免重名
        name = Core.newIdentity() + "!" + name;
        // 检查目录避免写入失败
        File file = new File(temp + File.separator + name);
        File fdir = file.getParentFile();
        if (!fdir.exists()) {
            fdir.mkdirs();
        }
        // 将上传的存入临时文件
        out = new FileOutputStream(file);
        byte[] buf = new byte[1024];
        int ovr;
        while ((ovr = ins.read(buf)) != -1) {
            out.write(buf, 0, ovr);
        }
        return name;
    } catch (IOException ex) {
        throw new Wrong(ex, "core.file.can.not.fetch", href, temp);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException ex) {
                throw new Wrong(ex, "core.file.can.not.close", temp);
            }
        }
        if (ins != null) {
            try {
                ins.close();
            } catch (IOException ex) {
                throw new Wrong(ex, "core.file.can.not.close", href);
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) MalformedURLException(java.net.MalformedURLException) Matcher(java.util.regex.Matcher) CoreConfig(io.github.ihongs.CoreConfig) InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 5 with CoreConfig

use of io.github.ihongs.CoreConfig in project HongsCORE by ihongs.

the class ApisAction method init.

@Override
public void init(ServletConfig conf) throws ServletException {
    super.init(conf);
    CoreConfig cc = CoreConfig.getInstance();
    // 请求数据
    dataKey = cc.getProperty("core.api.data", "__data__");
    // 封装模式
    modeKey = cc.getProperty("core.api.mode", "__mode__");
}
Also used : CoreConfig(io.github.ihongs.CoreConfig)

Aggregations

CoreConfig (io.github.ihongs.CoreConfig)23 Map (java.util.Map)12 HashMap (java.util.HashMap)11 HongsException (io.github.ihongs.HongsException)10 Action (io.github.ihongs.action.anno.Action)9 CommitSuccess (io.github.ihongs.action.anno.CommitSuccess)7 IOException (java.io.IOException)4 HongsExemption (io.github.ihongs.HongsExemption)3 Verify (io.github.ihongs.action.anno.Verify)3 File (java.io.File)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ActionHelper (io.github.ihongs.action.ActionHelper)1 Cmdlet (io.github.ihongs.cmdlet.anno.Cmdlet)1 DB (io.github.ihongs.db.DB)1 Table (io.github.ihongs.db.Table)1