1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
| @RestController @RequestMapping({"/"}) @Guarded public class AuthAction { private static final Logger log = LogManager.getLogger(AuthAction.class); private static final LoadingCache verificationCodeMap; @Autowired private IUser userService; @Autowired private ILog logService; @Autowired public RedisTemplate redisTemplate; @Autowired public IUpgrade upgradeService; @Autowired public IRole roleService; public static final String IPADDR_LOCK = "ipaddrLock"; public static final String USER_ATTRIBUTE_KEY = "user";
@RequestMapping( value = {"settings"}, method = {RequestMethod.GET} ) public Object setting(HttpServletRequest request) throws Exception { return ProductUtil.isSecret() ? this.setting_secret(request) : this.setting_default(); }
private Object setting_default() throws Exception { JsonObj obj = new JsonObj(true); Map settings = new HashMap(); Object licInfo = new HashMap();
try { licInfo = (Map)LicenseUIData.readInfo().get("license"); } catch (Exception var8) { log.error("read license error", var8); }
StringBuilder version = new StringBuilder(); version.append(CurrentLocale.getMessage("auth.get.normal.msg1-1", MapUtils.getString((Map)licInfo, "licenseSubject", "-"))); StringBuilder version2 = new StringBuilder(); if (ProductUtil.isSecret()) { String licenseVersion = org.apache.commons.collections4.MapUtils.getString(Constant.CONF, "license_version"); if (!Strings.isNullOrEmpty(licenseVersion)) { version2.append(licenseVersion); } else { version2.append(MapUtils.getString((Map)licInfo, "licenseVersion", "正式版")); } } else { version.append(MapUtils.getString((Map)licInfo, "licenseVersion", "正式版")); version.append(" v"); }
Map others = new HashMap(); settings.put("logo", "/logo/logo-cra.png"); version.append(UpgradeHelper.currentVersion()); others.put("title", ""); settings.put("version", version.toString()); settings.put("copyright", Constant.CONF.getProperty("product_copyright")); settings.put("supportUrl", Constant.CONF.getProperty("company_website")); settings.put("contact", Constant.CONF.getProperty("produc_contact")); settings.put("encrypt", "true"); settings.put("login", true); String productName; if (ProductUtil.isSecret()) { productName = SysConfig.INFO.get("product_name", CurrentLocale.getMessage("product_name_secret")); if (Strings.isNullOrEmpty(productName)) { productName = CurrentLocale.getMessage(Constant.CONF.getProperty("product_name_scan")); settings.put("property", true); settings.put("series", CurrentLocale.getMessage("product_series")); settings.put("support", CurrentLocale.getMessage("product_company")); settings.put("holder", CurrentLocale.getMessage("product_series")); } else { settings.put("property", false); settings.put("series", ""); settings.put("support", CurrentLocale.getMessage("product_company")); settings.put("holder", ""); }
settings.put("title", productName + " " + version2.toString()); } else { productName = SysConfig.INFO.get("product_name", ""); if (Strings.isNullOrEmpty(productName) && !ProductUtil.isSecret()) { productName = CurrentLocale.getMessage(Constant.CONF.getProperty("product_name_scan")); settings.put("property", false); settings.put("series", ""); settings.put("support", CurrentLocale.getMessage("product_company")); settings.put("holder", ""); } else { settings.put("property", false); settings.put("series", ""); settings.put("support", SysConfig.INFO.get("product_company", CurrentLocale.getMessage("product_company"))); settings.put("holder", ""); }
settings.put("title", productName); }
others.put("type", "form"); others.put("link", false); others.put("slogan", ""); others.put("forgot_pass", false); others.put("signup", false); Map auth = new HashMap(); auth.put("admin", others); settings.put("auth", auth); settings.put("product_type", SysConfig.INFO.getAsInt("system_type", 1)); settings.put("lang", CurrentLocale.getLocale().toLanguageTag()); obj.put("setting", settings); return obj; }
private Object setting_secret(HttpServletRequest request) { JsonObj obj = new JsonObj(true); Map settings = new HashMap(); Object licInfo = new HashMap();
try { licInfo = (Map)LicenseUIData.readInfo().get("license"); } catch (Exception var11) { log.error("read license error", var11); }
StringBuilder version = new StringBuilder(); version.append(CurrentLocale.getMessage("auth.get.normal.msg1-1", MapUtils.getString((Map)licInfo, "licenseSubject", "-"))); StringBuilder version2 = new StringBuilder(); String licenseVersion = org.apache.commons.collections4.MapUtils.getString(Constant.CONF, "license_version"); if (!Strings.isNullOrEmpty(licenseVersion)) { version2.append(licenseVersion); } else { version2.append(MapUtils.getString((Map)licInfo, "licenseVersion", "正式版")); }
version2.append(" V"); Map others = new HashMap(); settings.put("logo", "/logo/logo-cra.png"); version2.append(Constant.CONF.getProperty("product_version")); others.put("title", CurrentLocale.getMessage("auth.get.normal.msg1-2")); settings.put("version", version.toString()); settings.put("copyright", Constant.CONF.getProperty("product_copyright")); settings.put("supportUrl", Constant.CONF.getProperty("company_website")); settings.put("contact", Constant.CONF.getProperty("produc_contact")); settings.put("encrypt", "true"); settings.put("login", true); String productName = SysConfig.INFO.get("product_name", CurrentLocale.getMessage("product_name_secret")); if (Strings.isNullOrEmpty(productName)) { productName = CurrentLocale.getMessage("product_name_secret"); settings.put("property", true); settings.put("series", CurrentLocale.getMessage("product_series")); settings.put("support", CurrentLocale.getMessage("product_company")); settings.put("holder", CurrentLocale.getMessage("product_series")); } else { settings.put("property", false); settings.put("series", ""); settings.put("support", SysConfig.INFO.get("product_company", "")); settings.put("holder", ""); }
settings.put("title", productName + " " + version2.toString()); others.put("type", "form"); others.put("link", false); others.put("slogan", ""); others.put("forgot_pass", false); others.put("signup", false); Map auth = new HashMap(); auth.put("admin", others); settings.put("auth", auth); settings.put("product_type", SysConfig.INFO.getAsInt("system_type", 1)); settings.put("lang", CurrentLocale.getLocale().toLanguageTag()); obj.put("setting", settings); return obj; }
@IgnoreTokenRestful @RequestMapping( value = {"config"}, method = {RequestMethod.GET} ) public Object config() { Map configMap = new HashMap(); JsonObj obj = new JsonObj(true); obj.appendData(configMap); configMap.put("monitor", true); configMap.put("http401", true); configMap.put("http502", true); configMap.put("monitor_time", 1000); if (this.isCasLoginModel()) { configMap.put("monitor", true); configMap.put("http401", false); configMap.put("http502", false); configMap.put("monitor_time", 10000); }
return obj; }
private boolean isCasLoginModel() { return CasModelManager.isCasModel(); }
@BaseLineAssetAdapterAdvisor.BaseLineLimit @RequestMapping( value = {"profile"}, method = {RequestMethod.GET} ) public Object profile(HttpServletRequest request) throws Exception { Integer userId = (Integer)IdentitySupplier.userIdFromRequest.supplyWith(request); User u = (User)this.userService.queryById(userId); Validate.notNull(u, "user.login.error.notExist"); JsonObj obj = new JsonObj(true); obj.put("name", u.getUsername()); obj.put("role", this.userService.queryRole(userId).getName()); obj.put("avatar", ""); return obj; }
@PostConstruct void init() { UserSessionManagerFactory.getInstance().registerSessionCallback((sid, e) -> { switch(e) { case expiration: try { Integer uid = (Integer)IdentitySupplier.userIdFromSessionId.supplyWith(sid); UserService bean = (UserService)SpringContext.getBean(UserService.class); User u = (User)bean.queryById(uid); LogAdder.add(u.getUsername(), u.getId(), (String)null, "access.log.system.event.65", "access.log.system.desct.65", true, 3); } catch (Exception var5) { } default: } }); }
@IgnoreTokenRestful @RequestMapping( value = {"auth/tokens"}, method = {RequestMethod.POST} ) public Object postTokens(HttpServletRequest req, @RequestHeader String times, @RequestHeader String hash, String username, String password, String captcha, Integer type, String dynamic) throws Exception { Integer product_type = SysConfig.INFO.getAsInt("system_type", 1); SYSTEMTYPE systemtype; if (type == null) { systemtype = SYSTEMTYPE.valueOf(product_type); switch(systemtype) { case DEFAULT: case SECRET: type = LOGIN.TRADITION.getValue(); break; case FACTOR: type = LOGIN.TWOFACTOR.getValue(); } }
systemtype = null; LOGIN login = LOGIN.fromByValue(type); Object o; switch(login) { case TRADITION: o = this.loginByTradition(req, times, hash, username, password, captcha); break; case TWOFACTOR: o = this.loginByTwoFactor(req, username, password, dynamic, times, hash); break; default: throw new AppException("user.login.error.unknownLogin", "user.login.error.unknownLogin"); }
req.getSession().invalidate(); if (ArrayUtils.isNotEmpty(req.getCookies())) { Cookie cookie = req.getCookies()[0]; cookie.setMaxAge(0); }
return o; }
private Object loginByTradition(HttpServletRequest req, String times, String hash, String username, String password, String captcha) throws Exception { String logEvent = "access.log.system.55.event";
try { boolean code = SysConfig.DEFAULT.getAsInt(SysSetAction.SetItem.verification_code.name(), 1) == 1; if (!code || !((String)verificationCodeMap.get(hash)).equals("") && captcha.toLowerCase().equals(((String)verificationCodeMap.get(hash)).toLowerCase())) { return this.logining(req, username, password, times, hash, logEvent, "access.log.system.55.desct"); } else { verificationCodeMap.invalidate(hash); throw new AppException("user.login.warn.codeError"); } } catch (Exception var14) { String msg = "access.log.system.56.desct"; if (var14 instanceof AppException) { msg = var14.getMessage(); StringBuffer sb = new StringBuffer(); Object[] params = ((AppException)var14).getParams(); if (params != null && params.length > 0) { msg = msg + "____";
for(int i = 0; i < params.length; ++i) { Object ob = params[i]; sb.append(ob).append(","); }
msg = msg + sb.substring(0, sb.length() - 1); } }
if (Strings.isNullOrEmpty(username)) { username = "access.log.system.57.event"; }
LogAdder.add(username, -1, req.getRemoteHost(), logEvent, msg, false, 3); throw var14; } }
private Object logining(HttpServletRequest req, String username, String password, String times, String hash, String logEvent, String logDesct) throws Exception { new JsonObj(true); JsonObj token = new JsonObj(true);
JsonObj user; try { user = this.login(req, username, password, "", times); } catch (Exception var17) { verificationCodeMap.invalidate(hash); if (var17 instanceof AppException) { throw var17; }
throw new AppException(var17.getMessage()); }
Integer userId = (Integer)user.get("userId"); User user1 = (User)this.userService.queryById(userId); if (user1 == null) { throw new AppException("system.common.error.illegalLogin", "system.common.error.illegalLogin"); } else { Integer roleId = user1.getRoleId(); String sid = (String)IdentitySupplier.userIdToSessionId.supplyWith(userId); token.put("token", sid); verificationCodeMap.invalidate(hash); if (SysConfig.DEFAULT.getAsLong("last_modify_pwd_" + userId, 0L) == 0L) { SysConfig.DEFAULT.set("last_modify_pwd_" + userId, System.currentTimeMillis()); }
req.setAttribute("userId", userId); LogAdder.add(user1.getUsername(), userId, req.getRemoteHost(), logEvent, logDesct, true, 3); int timeout = SysConfig.DEFAULT.getAsInt(SysSetAction.SetItem.timeout_exit.name(), 30); String k_currLoginClient = "currLoginClient"; UserSessionManager instance = UserSessionManagerFactory.getInstance(); ((Set)IdentitySupplier.sameUserSessionIdsByUid.supplyWith(userId)).forEach((otherSid) -> { log.debug("invalid other sid '{}' with same uid '{}'", otherSid, userId); instance.invalidateSession(otherSid); }); instance.initailizeSession(() -> { return sid; }, (long)timeout, TimeUnit.MINUTES); instance.putIntoSession(sid, "user", user1); instance.putIntoSession(sid, k_currLoginClient, req.getRemoteHost()); return token; } }
private Object loginByTwoFactor(HttpServletRequest request, String username, String password, String dynamic, String times, String hash) throws Exception { String logEvent = "access.log.system.55.event";
try { if (!this.checkCode(dynamic, times, username)) { throw new AppException("user.login.error.failiedCode", "user.login.error.failiedCode"); } else { return this.logining(request, username, password, times, hash, logEvent, "access.log.system.55.desct2"); } } catch (Exception var10) { String msg = var10 instanceof AppException ? var10.getMessage() : "access.log.system.56.desct"; if (Strings.isNullOrEmpty(username)) { username = "access.log.system.57.event"; }
LogAdder.add(username, -1, request.getRemoteHost(), logEvent, msg, false, 3); throw var10; } }
private boolean checkCode(String code, String times, String username) { boolean flag = false; Validate.notEmpty(code, "user.login.error.codeEmpty"); User user = new User(); user.setUsername(username); user = (User)this.userService.queryOne(user); Validate.notNull(user, "user.login.error.notExist"); File seed = null; Role role = (Role)this.roleService.queryById(user.getRoleId()); if (role.getName().contains(CurrentLocale.getMessage("role.admin"))) { seed = new File(Constant.FEITIANCHENGXIN_DIR, "seed.txt"); } else if (role.getName().contains(CurrentLocale.getMessage("role.operator"))) { seed = new File(Constant.FEITIANCHENGXIN_DIR, "seed_oper.txt"); } else if (role.getName().contains(CurrentLocale.getMessage("role.auditor"))) { seed = new File(Constant.FEITIANCHENGXIN_DIR, "seed_auditor.txt"); }
if (seed != null && seed.exists()) { try { code = UserHelper.getInputPassword(username, code, times); FeitianchengxinCode.load(); int validate = FeitianchengxinCode.validate(seed.getAbsolutePath(), code); if (validate == 0) { flag = true; }
return flag; } catch (Exception var9) { log.error(var9.getMessage()); throw new AppException("动态口令验证错误", "user.login.error.verifyError"); } } else { throw new AppException("miss seed file !"); } }
@RequestMapping( value = {"auth/tokens"}, method = {RequestMethod.GET} ) public Object getTokens(HttpServletRequest req) throws Exception { UserSessionManagerFactory.getInstance().touchSession(IdentitySupplier.sessionIdFromRequest.supplyWith(req)); return new JsonObj(true); }
@RequestMapping( value = {"auth/tokens"}, method = {RequestMethod.DELETE} ) public Object deleteTokens(HttpServletRequest req) throws Exception { synchronized(this) { UserSessionManager instance = UserSessionManagerFactory.getInstance(); String sid = (String)IdentitySupplier.sessionIdFromRequest.supplyWith(req); log.warn("request delete sid:{}", sid); if (instance.touchSession(sid)) { User u = (User)JSON.parse(instance.getFromSession(sid, "user"), User.class); if (u != null) { LogAdder.add(u.getUsername(), u.getId(), req.getRemoteAddr(), "access.log.system.58.event", "access.log.system.58.desct", true, 3); }
instance.invalidateSession(sid); } }
return new JsonObj(true); }
@IgnoreTokenRestful @RequestMapping( value = {"auth/captcha"}, method = {RequestMethod.GET} ) public Object verificationCode() throws Exception { JsonObj obj = new JsonObj(true); boolean code = SysConfig.DEFAULT.getAsInt(SysSetAction.SetItem.verification_code.name(), 1) == 1; if (!ProductUtil.isSeed() && code) { obj.put("has_captcha", true); String uuid = UUID.randomUUID().toString(); obj.put("hash", uuid); Object[] info = VerificationCodeUtils.generate(); BASE64Encoder encoder = new BASE64Encoder(); obj.put("base64", encoder.encode((byte[])((byte[])info[1]))); verificationCodeMap.put(uuid, info[0].toString()); } else { obj.put("has_captcha", false); }
return obj; }
public JsonObj login(HttpServletRequest req, @NotNull(message = "oval.verify.normal.msg4-1") String username, @NotNull(message = "oval.verify.normal.msg4-1") String password, @NotNull(message = "oval.verify.normal.msg4-1") String vcode, @NotNull(message = "oval.verify.normal.msg4-1") String times) throws Exception { SysConfig sysConfig = SysConfig.DEFAULT; User u = new User(username); u = (User)this.userService.queryOne(u); if (u == null) { throw new AppException("user.login.error.hasError"); } else { int userId = u.getId(); int lockWay = SysConfig.DEFAULT.getAsInt(SysSetAction.SetItem.login_lock_way.name(), ProductUtil.isSecret() ? 1 : 0); Long timeout = SysConfig.DEFAULT.getAsLong(SysSetAction.SetItem.max_time_loginfailed.name(), 5L) * 60L; if (u.getDisabled() == 1) { throw (new AppException("user.login.error.disableLogin")).setParams(username); } else { if (lockWay == 1) { Long lockTime = u.getLockTime(); if (lockTime != null) { long leftTime = (System.currentTimeMillis() - lockTime) / 1000L - timeout; if (leftTime <= 0L) { throw (new AppException("user.login.error.lockUser")).setParams(Math.abs(leftTime)); } } }
if (lockWay == 2) { Map map = this.redisTemplate.boundHashOps("ipaddrLock").entries(); Long lockTime = map == null ? null : (Long)map.get(req.getRemoteAddr()); if (lockTime != null) { long leftTime = (System.currentTimeMillis() - lockTime) / 1000L - timeout; if (leftTime <= 0L) { throw (new AppException("user.login.error.lockIp")).setParams(Math.abs(leftTime)); }
this.setRedisIpaddr(new HashMap()); } }
if (!UserHelper.decryptGeneratePwd(username, password, times).equals(u.getPassword())) { if (lockWay != 0) { Object obj = WebContextUtil.getObjInSession(req, "lock_times"); int errorTimes = obj == null ? 0 : Integer.parseInt(obj.toString()); ++errorTimes; if (errorTimes == sysConfig.getAsInt(SysSetAction.SetItem.max_num_loginfailed.name(), 5)) { String errorTips = "用户名或者密码错误,"; String msgCode = "user.login.error.hasError"; Long currentTime = System.currentTimeMillis(); u.setLockTime(currentTime); if (lockWay == 1) { errorTips = errorTips + "该账户已被锁定!"; msgCode = "user.login.error.lockUser2"; } else { errorTips = errorTips + "该地址已被锁定!"; msgCode = "user.login.error.lockIp2"; Map newHash = Maps.newHashMap(); newHash.put(req.getRemoteAddr(), currentTime); this.setRedisIpaddr(newHash); }
this.userService.updateById(userId, u); req.getSession().setAttribute("lock_times", 0); throw new AppException(msgCode, msgCode); }
req.getSession().setAttribute("lock_times", errorTimes); }
throw new LoginException("user.login.error.hasError"); } else { this.updateConfig(u); req.getSession().removeAttribute("lock_times"); u.setLastLoginTime(System.currentTimeMillis()); u.setLockTime((Long)null); u.setLastLoginClient(req.getRemoteAddr()); this.userService.updateById(userId, u); JsonObj obj = new JsonObj(true);
try { obj.put("userId", userId); } catch (Exception var17) { log.error("", var17); }
return obj; } } } }
private void updateConfig(User user) { Integer userId = user.getId(); if (userId == 1 && user.getLastLoginTime() == null) { SysConfig.INFO.set("password_modify_AD", 1); } else if (userId == 2 && user.getLastLoginTime() == null) { SysConfig.INFO.set("password_modify_AU", 1); } else if (userId == 3 && user.getLastLoginTime() == null) { SysConfig.INFO.set("password_modify_OP", 1); }
}
public void setRedisIpaddr(Map newHash) { Map map = this.redisTemplate.boundHashOps("ipaddrLock").entries(); Long timeout = SysConfig.DEFAULT.getAsLong(SysSetAction.SetItem.max_time_loginfailed.name(), 5L) * 60L; map.forEach((k, v) -> { long leftTime = timeout - (System.currentTimeMillis() - v) / 1000L; if (leftTime > 0L) { newHash.put(k, v); }
}); if (newHash.size() > 0) { this.redisTemplate.boundHashOps("ipaddrLock").putAll(newHash); } else { this.redisTemplate.delete("ipaddrLock"); }
}
static { verificationCodeMap = CacheBuilder.newBuilder().softValues().maximumSize(20L).expireAfterAccess(3L, TimeUnit.MINUTES).build(new CacheLoader() { public String load(String str) throws Exception { return ""; } }); } }
|