Пример #1
0
  @Override
  public void onApplicationEvent(ApplicationEvent event) {

    // shop key unknow
    String k =
        (String)
            servletContext.getAttribute("Z" + "G" + "S" + "H" + "O" + "P" + "_" + "K" + "E" + "Y");
    String shopkey = EncryptUtils.dencrypt(k);
    if (!StringUtils.containsIgnoreCase(shopkey, "z" + "g" + "s" + "h" + "o" + "p")) {
      throw new RuntimeException();
    }

    // 登录成功:记录登录IP、清除登录失败次数
    if (event instanceof AuthenticationSuccessEvent) {
      AuthenticationSuccessEvent authEvent = (AuthenticationSuccessEvent) event;
      Authentication authentication = (Authentication) authEvent.getSource();
      String loginIp = ((WebAuthenticationDetails) authentication.getDetails()).getRemoteAddress();
      Admin admin = (Admin) authentication.getPrincipal();
      admin.setLoginIp(loginIp);
      admin.setLoginDate(new Date());
      SystemConfig systemConfig = SystemConfigUtils.getSystemConfig();
      if (systemConfig.getIsLoginFailureLock() == false) {
        return;
      }
      admin.setLoginFailureCount(0);
      adminService.update(admin);
    }

    // 登录失败:增加登录失败次数
    if (event instanceof AuthenticationFailureBadCredentialsEvent) {
      AuthenticationFailureBadCredentialsEvent authEvent =
          (AuthenticationFailureBadCredentialsEvent) event;
      Authentication authentication = (Authentication) authEvent.getSource();
      String loginUsername = authentication.getName();
      SystemConfig systemConfig = SystemConfigUtils.getSystemConfig();
      if (systemConfig.getIsLoginFailureLock() == false) {
        return;
      }
      Admin admin = adminService.get("username", loginUsername);
      if (admin != null) {
        int loginFailureCount = admin.getLoginFailureCount() + 1;
        if (loginFailureCount >= systemConfig.getLoginFailureLockCount()) {
          admin.setIsAccountLocked(true);
          admin.setLockedDate(new Date());
        }
        admin.setLoginFailureCount(loginFailureCount);
        adminService.update(admin);
      }
    }
  }