Пример #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);
      }
    }
  }
  public void onApplicationEvent(ApplicationEvent e) {
    if (e instanceof AuthenticationSuccessEvent) {
      // 登录成功后的事件处理
      AuthenticationSuccessEvent event = (AuthenticationSuccessEvent) e;
      Authentication authentication = event.getAuthentication();

      loginUser(authentication);
    } else if (e instanceof HttpSessionCreatedEvent) {
      HttpSession session = ((HttpSessionCreatedEvent) e).getSession();

      OnLineInfo onlineInfo = onLineManager.getOnlineUser(session.getId());
      if (onlineInfo == null) return;

      Person person = onlineInfo.getPerson();
      String username = person.getUsername();
      //
      //			//把当前登录用户的CSS主题写入Session中
      //			String cssTheme = settingManager.getPersonSettingValue(username, MyConstants.CSS_THEME);
      //			if (cssTheme == null)
      //				cssTheme = "";
      //
      //			session.setAttribute(MyConstants.CSS_THEME, cssTheme);
      //
      //			//把当前登录用户的在线消息定时接收时间间隔写入Session中
      //			int messageCheckInterval = settingManager.getPersonSettingIntValue(username,
      // MyConstants.MESSAGE_CHECK_INTERVAL);
      //			session.setAttribute(MyConstants.MESSAGE_CHECK_INTERVAL, messageCheckInterval);
      //
      //			//把当前登录用户的电子邮件定时接收时间间隔写入Session中
      //			int mailCheckInterval = settingManager.getPersonSettingIntValue(username,
      // MyConstants.MAIL_CHECK_INTERVAL);
      //			session.setAttribute(MyConstants.MAIL_CHECK_INTERVAL, mailCheckInterval);
    } else if (e instanceof HttpSessionDestroyedEvent) {
      SecurityContext securityContext = ((HttpSessionDestroyedEvent) e).getSecurityContext();
      if (securityContext == null) return;

      Authentication authentication = securityContext.getAuthentication();
      if (authentication == null) return;

      if (authentication.getDetails() instanceof WebAuthenticationDetails) {
        WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails();
        String sessionId = details.getSessionId();

        logoutUser(sessionId);
      }
    }
  }
 @Override
 public void onApplicationEvent(final AuthenticationSuccessEvent e) {
   final WebAuthenticationDetails auth =
       (WebAuthenticationDetails) e.getAuthentication().getDetails();
   if (auth != null) {
     loginAttemptService.loginSucceeded(auth.getRemoteAddress());
   }
 }
  @Override
  public void onApplicationEvent(AuthenticationSuccessEvent event) {
    Authentication authentication = event.getAuthentication();
    Object principal = authentication.getPrincipal();
    if (principal instanceof CustomUserDetails) {
      Serializable id = ((CustomUserDetails<?, ?>) principal).getId();
      User user = userService.findOne((Long) id);
      user.setLastLoginDate(new Date());

      WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails();
      user.setLastLoginIp(details.getRemoteAddress());
      userService.update(user);
    }
  }
  @Override
  public void onApplicationEvent(AuthenticationSuccessEvent event) {
    Authentication authentication = event.getAuthentication();
    Object principal = authentication.getPrincipal();
    if (principal instanceof CustomUserDetails) {
      @SuppressWarnings("unchecked")
      UserEntity userEntity = ((CustomUserDetails<?, UserEntity>) principal).getCustomUser();
      userEntity.setLastLoginDate(new Date());

      WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails();
      userEntity.setLastLoginIp(details.getRemoteAddress());
      userEntityService.save(userEntity);
    }
  }