Search in sources :

Example 11 with CoreConfig

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

the class Capts method captcha.

/**
 * 生成验证码
 * @param h 图片高(px)
 * @param b 背景色
 * @param f 前景色
 * @return
 */
public static Capts captcha(int h, String b, String f) {
    if (h < 24 || h > 96) {
        throw new HongsExemption(400, "h must be 24~96 (px)");
    }
    // 获取配置
    CoreConfig cc = CoreConfig.getInstance();
    String ff = cc.getProperty("core.capts.font.file", "!Capts.ttf");
    String cs = cc.getProperty("core.capts.code.dict", "1234567890");
    int cn = cc.getProperty("core.capts.code.count", 4);
    int mn = cc.getProperty("core.capts.mask.count", 8);
    float sr = cc.getProperty("core.capts.size.ratio", 0.40f);
    float fr = cc.getProperty("core.capts.font.ratio", 0.80f);
    float mr = cc.getProperty("core.capts.mend.ratio", 0.10f);
    float xr = cc.getProperty("core.capts.mask.ratio", 0.05f);
    int w = (int) ((float) h * sr * (cn + 1));
    char[] cd = cs.toCharArray();
    Color bc = "".equals(b) ? new Color(0xffffff, true) : new Color(Integer.parseInt(b, 16));
    Color fc = "".equals(f) ? new Color(0x000000, false) : new Color(Integer.parseInt(f, 16));
    // 构建实例
    Capts vc = new Capts();
    vc.setSize(w, h);
    vc.setCodeCount(cn);
    vc.setMaskCount(mn);
    vc.setFontRatio(fr);
    vc.setMendRatio(mr);
    vc.setMaskRatio(xr);
    vc.setBackColor(bc);
    vc.setFontColor(fc);
    vc.setFontFile(ff);
    vc.setCodeDict(cd);
    return vc;
}
Also used : CoreConfig(io.github.ihongs.CoreConfig) Color(java.awt.Color) HongsExemption(io.github.ihongs.HongsExemption)

Example 12 with CoreConfig

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

the class AuthKit method redirect.

/**
 * 登录失败后跳转
 * 依此检查 Parameters,Cookies,Session 中是否有指定返回路径
 * 都没有指定时则跳转到默认地址
 * 默认地址缺失则跳转到网站首页
 * 如指定特殊值则会返回错误信息
 * @param helper
 * @param err
 * @throws HongsException
 */
public static void redirect(ActionHelper helper, HongsCause err) 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) || "_sign_info_".equals(r) || "-".equals(r)) {
        // 输出 JSON
        String errno = "Ex" + Integer.toHexString(err.getErrno());
        helper.reply(Synt.mapOf("ok", false, "ern", errno, "err", err.getMessage(), "msg", err.getLocalizedMessage()));
    } else {
        // 输出 HTML
        String m = err.getLocalizedMessage();
        helper.ensue(401, v, m);
    }
}
Also used : CoreConfig(io.github.ihongs.CoreConfig)

Example 13 with CoreConfig

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

the class WXAction method inWap.

/**
 * 微信 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.wx.wap.app.id");
    String appSk = cc.getProperty("oauth2.wx.wap.app.key");
    String code = helper.getParameter("code");
    if (appId == null || appSk == null) {
        helper.error(400, "Not support this mode");
        return;
    }
    try {
        Map info = getUserInfo(code, appId, appSk);
        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, "wx", Synt.defoult(opuId, opnId), name, head);
        // 登记 openId
        if (opnId != null && opuId != null) {
            String usrId = (String) back.get(Cnst.UID_SES);
            setUserSign("wx.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 14 with CoreConfig

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

the class WXAction method inWeb.

/**
 * 微信 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.wx.web.app.id");
    String appSk = cc.getProperty("oauth2.wx.web.app.key");
    String code = helper.getParameter("code");
    if (appId == null || appSk == null) {
        helper.error(400, "Not support this mode");
        return;
    }
    try {
        Map info = getUserInfo(code, appId, appSk);
        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, "wx", Synt.defoult(opuId, opnId), name, head);
        // 登记 openId
        if (opnId != null && opuId != null) {
            String usrId = (String) back.get(Cnst.UID_SES);
            setUserSign("wx.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 15 with CoreConfig

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

the class SignAction method signCreate.

/**
 * 登录
 * @param ah
 * @throws HongsException
 */
@Action("create")
@Verify(conf = "master", form = "sign")
@CommitSuccess
public void signCreate(ActionHelper ah) throws HongsException {
    String place = Synt.declare(ah.getParameter("place"), "centre");
    String username = Synt.declare(ah.getParameter("username"), "");
    String password = Synt.declare(ah.getParameter("password"), "");
    String passcode;
    DB db = DB.getInstance("master");
    Table tb = db.getTable("user");
    FetchCase fc;
    Map ud;
    String id;
    String tt;
    int at;
    int rt;
    // 检查账号
    fc = new FetchCase().from(tb.tableName).filter("username = ?", username).select("password, passcode, id, name, head, state");
    ud = db.fetchLess(fc);
    if (ud.isEmpty()) {
        ah.reply(AuthKit.getWrong("username", "core.username.invalid"));
        return;
    }
    // 重试限制
    CoreConfig cc = CoreConfig.getInstance("master");
    id = (String) ud.get("id");
    tt = Synt.declare(cc.getProperty("core.sign.retry.token"), "");
    at = Synt.declare(cc.getProperty("core.sign.retry.times"), 5);
    if (Synt.declare(Record.get("sign.retry.allow." + id), false)) {
        tt = "id";
    }
    switch(tt) {
        case "id":
            break;
        case "ip":
            id = Core.CLIENT_ADDR.get();
            break;
        default:
            id = id + "-" + Core.CLIENT_ADDR.get();
    }
    rt = Synt.declare(Record.get("sign.retry.times." + id), 0);
    if (rt >= at) {
        ah.reply(AuthKit.getWrong("password", "core.password.timeout"));
        ah.getResponseData().put("allow_times", at);
        ah.getResponseData().put("retry_times", rt);
        return;
    } else {
        rt++;
    }
    // 校验密码
    passcode = Synt.declare(ud.get("passcode"), "");
    password = AuthKit.getCrypt(password + passcode);
    if (!password.equals(ud.get("password"))) {
        ah.reply(AuthKit.getWrong("password", "core.password.invalid"));
        ah.getResponseData().put("allow_times", at);
        ah.getResponseData().put("retry_times", rt);
        // 记录错误次数
        Calendar ca;
        long et;
        ca = Calendar.getInstance(Core.getTimezone());
        ca.setTimeInMillis(Core.ACTION_TIME.get());
        ca.set(Calendar.HOUR_OF_DAY, 23);
        ca.set(Calendar.MINUTE, 59);
        ca.set(Calendar.SECOND, 59);
        et = ca.getTimeInMillis() / 1000 + 1;
        Record.set("sign.retry.times." + id, rt, et);
        return;
    } else {
        Record.del("sign.retry.times." + id);
    }
    String uuid = (String) ud.get("id");
    String uname = (String) ud.get("name");
    String uhead = (String) ud.get("head");
    int state = Synt.declare(ud.get("state"), 0);
    // 验证状态
    if (0 >= state) {
        ah.reply(AuthKit.getWrong("state", "core.sign.state.invalid"));
        return;
    }
    // 规避自定 RoleSet 附加判断
    ah.setSessibute(Cnst.UID_SES, null);
    ah.setSessibute(Cnst.USK_SES, null);
    ah.setSessibute(Cnst.UST_SES, null);
    // 验证区域
    Set rs = RoleSet.getInstance(uuid);
    if (rs != null && !place.isEmpty() && !rs.contains(place)) {
        ah.reply(AuthKit.getWrong("place", "core.sign.place.invalid"));
        return;
    }
    // * 表示密码登录
    Map sd = AuthKit.userSign(ah, "*", uuid, uname, uhead);
    ah.reply(Synt.mapOf("info", sd));
}
Also used : Table(io.github.ihongs.db.Table) FetchCase(io.github.ihongs.db.util.FetchCase) Set(java.util.Set) RoleSet(io.github.ihongs.serv.auth.RoleSet) CoreConfig(io.github.ihongs.CoreConfig) Calendar(java.util.Calendar) Map(java.util.Map) DB(io.github.ihongs.db.DB) Action(io.github.ihongs.action.anno.Action) CommitSuccess(io.github.ihongs.action.anno.CommitSuccess) Verify(io.github.ihongs.action.anno.Verify)

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