Пример #1
0
  public static void main(String[] args) {
    try {
      String s = new String("投之家tzj".getBytes(UTF_8_CHARSET), UTF_8_CHARSET);

      System.out.println(MD5.toMD5(s));
      System.out.println(s.length());
      try {
        System.out.println(EncryptMD5.toMD5(s));
      } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      System.out.println(s.length());
    } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Пример #2
0
  /**
   * Description:登录方法<br>
   *
   * @author justin.xu
   * @version 0.1 2014年5月7日
   * @param request
   * @param session
   * @param response
   * @param memberLoginCnd
   * @return String
   */
  @RequestMapping(value = "/login")
  @ValidateToken
  @ResponseBody
  public MessageBox login(
      HttpSession session, HttpServletResponse response, MemberLoginCnd memberLoginCnd)
      throws AuthenticationException {
    try {
      Object object = session.getAttribute(CSRFTokenManager.CSRF_PARAM_COUNTER);
      if (object != null && ((Integer) (object)).intValue() > 2) {
        if (memberLoginCnd.getCheckCode() == null || "".equals(memberLoginCnd.getCheckCode())) {
          return MessageBox.build("0", "请你输入验证码");
        }
        String randomCode = (String) currentSession().getAttribute("randomCode");
        if (!memberLoginCnd.getCheckCode().equals(randomCode)) {
          return MessageBox.build("0", "验证码不正确");
        }
      }

      long t1 = System.currentTimeMillis();
      logger.info(memberLoginCnd.getUsername() + " ----- login ----- begin time : " + t1);

      // shiro登录
      UsernamePasswordToken token =
          new UsernamePasswordToken(
              memberLoginCnd.getUsername(),
              MD5.toMD5(memberLoginCnd.getPasswd()),
              BusinessConstants.MEMBER_OPERATE_ON_PORTAL);
      Subject subject = SecurityUtils.getSubject();
      subject.login(token);

      long t2 = System.currentTimeMillis();
      logger.info(
          memberLoginCnd.getUsername() + " ----- login ----- shiro login cost : " + (t2 - t1));

      LoginCnd loginCnd = new LoginCnd();
      loginCnd.setUserId(currentUser().getUserId());
      loginCnd.setUserName(currentUser().getUserName());
      loginCnd.setIp(HttpTookit.getRealIpAddr(currentRequest()));
      loginCnd.setSessionId(session.getId());
      loginCnd.setPlatform(currentUser().getPlatform());
      // 调用登录逻辑
      String msg = memberService.saveLogin(loginCnd);

      long t3 = System.currentTimeMillis();
      logger.info(
          memberLoginCnd.getUsername() + " ----- login ----- save login cost : " + (t3 - t2));

      // 用于sso
      cookieRetrievingCookieGenerator.addCookie(
          currentRequest(), response, TicketCryptor.encrypt(currentUser().getSsoTicket()));

      long t4 = System.currentTimeMillis();
      logger.error(
          memberLoginCnd.getUsername() + " ----- login ----- add sso cookie cost : " + (t4 - t3));
      System.out.println(memberLoginCnd.getSaveid() + "hhe");
      // 保存cookie
      if ("1".equals(memberLoginCnd.getSaveid())) {
        CookieGenerator cookieGenerator = new CookieGenerator();
        cookieGenerator.setCookieMaxAge(2147483647);
        cookieGenerator.setCookieName(COOKIE_LOGIN_USERID);
        cookieGenerator.addCookie(response, memberLoginCnd.getCookieusername());
      } else {
        CookieGenerator cookieGenerator = new CookieGenerator();
        cookieGenerator.setCookieMaxAge(0);
        cookieGenerator.setCookieName(COOKIE_LOGIN_USERID);
        cookieGenerator.addCookie(response, memberLoginCnd.getCookieusername());
      }

      long t5 = System.currentTimeMillis();
      logger.error(
          memberLoginCnd.getUsername()
              + " ----- login ----- add username cookie cost : "
              + (t5 - t4));

      // 小红点提示状态
      RedAccount redAccount = redAccountService.queryRedDotState(currentUser().getUserId());
      if (redAccount != null) {
        currentSession().setAttribute("redDot", redAccount);
      }
      // 抽奖机会提醒
      int lotteryChanceCount =
          lotteryChanceInfoService.queryLotteryNumTotal("11", currentUser().getUserId());
      if (lotteryChanceCount > 0) {
        currentSession().setAttribute("lotteryChanceCount", lotteryChanceCount);
      }
      if (BusinessConstants.VISITOR_UNAUTHERIZED.equals(msg)) {
        return MessageBox.build("2", "请先前往认证");
      }

      if (object != null) {
        currentSession().removeAttribute(CSRFTokenManager.CSRF_PARAM_COUNTER);
      }
    } catch (UnknownAccountException e) {
      return MessageBox.build("0", "账户名不存在");
    } catch (IncorrectCredentialsException ice) {
      return MessageBox.build("0", "账户名与密码不匹配");
    } catch (LockedAccountException lae) {
      return MessageBox.build("0", "账户已经锁定");
    } catch (Exception e) {
      if (isLogin()) {
        SecurityUtils.getSubject().logout();
      }
      logger.error("登录失败", e);
      return MessageBox.buildForCounter("3", "账号与密码不匹配");
    }

    String backUrl = memberLoginCnd.getBackUrl();
    if (backUrl != null && !backUrl.trim().equals("")) {
      logger.info("******自动跳转登陆前页面=" + backUrl + "******");
      return MessageBox.build("8", backUrl);
    }

    return MessageBox.build("1", "success");
  }