use of com.zyd.blog.business.entity.User in project OneBlog by zhangyd-c.
the class OAuthController method renderAuth.
@RequestMapping("/social/{source}")
public ModelAndView renderAuth(@PathVariable("source") String source, HttpServletResponse response, HttpServletRequest request) {
SocialConfig socialConfig = sysSocialConfigService.getByPlatform(source);
if (null == socialConfig) {
throw new ZhydException(source + " 平台的配置尚未完成,暂时不支持登录!");
}
SocialStrategy socialStrategy = new SocialStrategy(japUserService, new JapConfig());
JapResponse japResponse = socialStrategy.authenticate(JapUtil.blogSocialConfig2JapSocialConfig(socialConfig, source), request, response);
if (!japResponse.isSuccess()) {
throw new ZhydException(japResponse.getMessage());
}
if (japResponse.isRedirectUrl()) {
return ResultUtil.redirect((String) japResponse.getData());
} else {
JapUser japUser = (JapUser) japResponse.getData();
User user = (User) japUser.getAdditional();
SessionUtil.setUser(user);
return ResultUtil.redirect("/");
}
}
use of com.zyd.blog.business.entity.User in project OneBlog by zhangyd-c.
the class JapSocialUserServiceImpl method createAndGetSocialUser.
/**
* Save the social login user information to the database and return JapUser
* <p>
* It is suitable for the {@code jap-social} module
*
* @param userInfo User information obtained through justauth third-party login, type {@code me.zhyd.oauth.model.AuthUser}
* @return When saving successfully, return {@code JapUser}, otherwise return {@code null}
*/
@Override
public JapUser createAndGetSocialUser(Object userInfo) {
AuthUser authUser = (AuthUser) userInfo;
User newUser = BeanConvertUtil.doConvert(authUser, User.class);
newUser.setSource(authUser.getSource());
if (null != authUser.getGender()) {
newUser.setGender(Integer.valueOf(authUser.getGender().getCode()));
}
User user = userService.getByUuidAndSource(authUser.getUuid(), authUser.getSource());
newUser.setUserType(UserTypeEnum.USER);
if (null != user) {
newUser.setId(user.getId());
userService.updateSelective(newUser);
} else {
userService.insert(newUser);
}
try {
userService.updateUserLastLoginInfo(user);
} catch (Exception e) {
e.printStackTrace();
}
return new JapUser().setUserId(newUser.getId() + "").setUsername(newUser.getUsername()).setAdditional(newUser);
}
use of com.zyd.blog.business.entity.User in project OneBlog by zhangyd-c.
the class RemoverServiceImpl method saveArticles.
private void saveArticles(Long typeId, HunterConfig config, HunterPrintWriter writerUtil, CopyOnWriteArrayList<VirtualArticle> list) {
// 获取数据库中的标签列表
List<Tags> tags = tagsService.listAll();
Map<String, Long> originalTags = tags.stream().collect(Collectors.toMap(tag -> tag.getName().toUpperCase(), Tags::getId));
User user = SessionUtil.getUser();
// 添加文章到数据库
Article article = null;
for (VirtualArticle spiderVirtualArticle : list) {
article = this.saveArticle(typeId, config.isConvertImg(), writerUtil, user, spiderVirtualArticle);
this.saveTags(writerUtil, originalTags, article, spiderVirtualArticle);
}
}
use of com.zyd.blog.business.entity.User in project OneBlog by zhangyd-c.
the class RememberAuthenticationInterceptor method preHandle.
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Subject subject = SecurityUtils.getSubject();
if (subject.isAuthenticated()) {
return true;
}
Session session = subject.getSession(true);
if (session.getAttribute(SessionConst.USER_SESSION_KEY) != null) {
return true;
}
if (!subject.isRemembered()) {
log.warn("未设置“记住我”,跳转到登录页...");
response.sendRedirect(request.getContextPath() + "/passport/login");
return false;
}
try {
Long userId = Long.parseLong(subject.getPrincipal().toString());
User user = userService.getByPrimaryKey(userId);
UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), PasswordUtil.decrypt(user.getPassword(), user.getUsername()), true);
subject.login(token);
session.setAttribute(SessionConst.USER_SESSION_KEY, user);
log.info("[{}] - 已自动登录", user.getUsername());
} catch (Exception e) {
log.error("自动登录失败", e);
response.sendRedirect(request.getContextPath() + "/passport/login");
return false;
}
return true;
}
use of com.zyd.blog.business.entity.User in project OneBlog by zhangyd-c.
the class RetryLimitCredentialsMatcher method doCredentialsMatch.
@Override
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
Long userId = (Long) info.getPrincipals().getPrimaryPrincipal();
User user = userService.getByPrimaryKey(userId);
String username = user.getUsername();
// 访问一次,计数一次
ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
String loginCountKey = SHIRO_LOGIN_COUNT + username;
String isLockKey = SHIRO_IS_LOCK + username;
opsForValue.increment(loginCountKey, 1);
if (redisTemplate.hasKey(isLockKey)) {
String unit = "分钟";
long time = TimeUnit.SECONDS.toMinutes(redisTemplate.getExpire(isLockKey));
if (time <= 0) {
unit = "秒";
time = TimeUnit.SECONDS.toSeconds(redisTemplate.getExpire(isLockKey));
} else if (time > 60) {
unit = "小时";
time = TimeUnit.SECONDS.toHours(redisTemplate.getExpire(isLockKey));
}
throw new ExcessiveAttemptsException("帐号[" + username + "]已被禁止登录!剩余" + time + unit);
}
Map<String, Object> configs = configService.getConfigs();
Object loginRetryNumObj = configs.get("loginRetryNum");
Object sessionTimeOutObj = configs.get("sessionTimeOut");
Object sessionTimeOutUnitObj = configs.get("sessionTimeOutUnit");
int loginRetryNum = StringUtils.isEmpty(loginRetryNumObj) ? DEFAULT_RETRY_NUM : Integer.parseInt(String.valueOf(loginRetryNumObj));
int sessionTimeOut = StringUtils.isEmpty(sessionTimeOutObj) ? DEFAULT_SESSIONTIME_OUT : Integer.parseInt(String.valueOf(sessionTimeOutObj));
TimeUnit sessionTimeOutUnit = StringUtils.isEmpty(sessionTimeOutUnitObj) ? DEFAULT_SESSIONTIME_OUT_UNIT : TimeUnit.valueOf(String.valueOf(sessionTimeOutUnitObj));
String loginCount = String.valueOf(opsForValue.get(loginCountKey));
int retryCount = ((loginRetryNum + 1) - Integer.parseInt(loginCount));
if (retryCount <= 0) {
opsForValue.set(isLockKey, "LOCK");
redisTemplate.expire(isLockKey, sessionTimeOut, sessionTimeOutUnit);
redisTemplate.expire(loginCountKey, sessionTimeOut, sessionTimeOutUnit);
throw new ExcessiveAttemptsException("由于密码输入错误次数过多,帐号[" + username + "]已被禁止登录!");
}
boolean matches = super.doCredentialsMatch(token, info);
if (!matches) {
throw new AccountException("帐号或密码不正确!您还剩" + retryCount + "次重试的机会");
}
// 清空登录计数
redisTemplate.delete(loginCountKey);
try {
userService.updateUserLastLoginInfo(user);
} catch (Exception e) {
e.printStackTrace();
}
// 当验证都通过后,把用户信息放在session里
// 注:User必须实现序列化
SecurityUtils.getSubject().getSession().setAttribute(SessionConst.USER_SESSION_KEY, user);
return true;
}
Aggregations