login.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 142 行

JAVA
142
字号
package cn.js.fan.security;import javax.servlet.*;import javax.servlet.http.*;import cn.js.fan.util.*;import cn.js.fan.web.SkinUtil;public class Login {    static final long delaytime = 60000;     static final int maxfailcount = 3;    static final long failtimespan = 30000;     public Login() {    }        public static void initlogin(HttpServletRequest request, String prefix) {        HttpSession session = request.getSession(true);        String c = (String) session.getAttribute(prefix + "_loginfail_count");        if (c == null)            session.setAttribute(prefix + "_loginfail_count", "0");    }    public static boolean canlogin(HttpServletRequest request, String prefix) throws            ErrMsgException {        HttpSession session = request.getSession(true);        String strcount = (String) session.getAttribute(prefix +                "_loginfail_count");        int count = 0;        if (strcount == null) {            throw new ErrMsgException(SkinUtil.LoadString(request, "err_login_invalid"));         } else {            try {                count = Integer.parseInt(strcount);            } catch (Exception e) {                throw new ErrMsgException("登录计数错!");            }        }        if (count <= maxfailcount)            return true;        long first = 0;        long last = 0;        try {            first = Long.parseLong((String) session.getAttribute(                    prefix + "_loginfail_first"));            last = Long.parseLong((String) session.getAttribute(                    prefix + "_loginfail_last"));        } catch (NumberFormatException e) {            throw new ErrMsgException("时间非法!");        }        long timespan = last - first;        if (timespan <= failtimespan) {             long tspan = System.currentTimeMillis() - last;            tspan = (delaytime - tspan) / 1000;            if (tspan > 0) {                                                                                String str = SkinUtil.LoadString(request, "err_login_can_not");                str = str.replaceFirst("\\$s", "" + failtimespan/1000);                str = str.replaceFirst("\\$c", "" + maxfailcount);                str = str.replaceFirst("\\$d", "" + delaytime/1000);                str = str.replaceFirst("\\$t", "" + tspan);                throw new ErrMsgException(str);            }        } else            session.setAttribute(prefix + "_loginfail_count", "0");        return true;    }        public static void afterlogin(HttpServletRequest request,                                  boolean isloginsuccess, String prefix,                                  boolean keepsession) throws ErrMsgException {        HttpSession session = request.getSession(true);        if (isloginsuccess) {            if (!keepsession)                session.invalidate();             return;        }        String strcount = (String) session.getAttribute(prefix +                "_loginfail_count");        int count = 0;        if (strcount == null) {            throw new ErrMsgException("After:" + SkinUtil.LoadString(request, "err_login_invalid"));         } else {            try {                count = Integer.parseInt(strcount);            } catch (Exception e) {                throw new ErrMsgException("登录计数错!");            }        }        count++;        session.setAttribute(prefix + "_loginfail_count", "" + count);        long t = System.currentTimeMillis();                if (count == 1) {            session.setAttribute(prefix + "_loginfail_first", "" + t);            session.setAttribute(prefix + "_loginfail_last", "" + t);        } else            session.setAttribute(prefix + "_loginfail_last",                                 "" + System.currentTimeMillis());        long timespan = 0;        long first = 0, last = 0;        if (count == 1) {                                                            String str = SkinUtil.LoadString(request, "err_login_fail_one");            str = str.replaceFirst("\\$c", "" + count);            str = str.replaceFirst("\\$s", "" + failtimespan/1000);            str = str.replaceFirst("\\$m", "" + maxfailcount);            str = str.replaceFirst("\\$d", "" + delaytime/1000);            throw new ErrMsgException(str);        }        else            last = t;        try {            first = Long.parseLong((String) session.getAttribute(                    prefix + "_loginfail_first"));        } catch (NumberFormatException e) {            throw new ErrMsgException("时间格式错!");        }        timespan = (last - first) / 1000;                                        String str = SkinUtil.LoadString(request, "err_login_fail");        str = str.replaceFirst("\\$t", "" + timespan);        str = str.replaceFirst("\\$c", "" + count);        str = str.replaceFirst("\\$f", "" + failtimespan/1000);        str = str.replaceFirst("\\$m", "" + maxfailcount);        str = str.replaceFirst("\\$s", "" + delaytime/1000);        throw new ErrMsgException(str);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?