use of com.hb0730.boot.admin.exceptions.LoginException in project boot-admin by hb0730.
the class MenuServiceImpl method updateCurrentMenu.
@Override
public boolean updateCurrentMenu() {
User currentUser = SecurityUtils.getCurrentUser();
if (null == currentUser) {
throw new LoginException(ResponseStatusEnum.USE_LOGIN_ERROR, "当前用户未登录,请登录后重试");
}
eventPublisher.publishEvent(new MenuEvent(this, currentUser.getId()));
return true;
}
use of com.hb0730.boot.admin.exceptions.LoginException in project boot-admin by hb0730.
the class MenuServiceImpl method getCurrentMenu.
@SneakyThrows
@Override
public List<TreeMenuDTO> getCurrentMenu() {
User currentUser = SecurityUtils.getCurrentUser();
if (null == currentUser) {
throw new LoginException(ResponseStatusEnum.USE_LOGIN_ERROR, "当前用户未登录,请登录后重试");
}
HashOperations<String, Long, List<TreeMenuDTO>> hash = redisTemplate.opsForHash();
List<TreeMenuDTO> treeMenu = hash.get(RedisConstant.MENU_KEY_PREFIX, currentUser.getId());
if (CollectionUtil.isEmpty(treeMenu)) {
eventPublisher.publishEvent(new MenuEvent(this, currentUser.getId()));
}
treeMenu = hash.get(RedisConstant.MENU_KEY_PREFIX, currentUser.getId());
if (CollectionUtil.isEmpty(treeMenu)) {
return Lists.newArrayList();
}
return treeMenu;
}
use of com.hb0730.boot.admin.exceptions.LoginException in project boot-admin by hb0730.
the class LoginServiceImpl method login.
@Nullable
public LoginUser login(@NonNull String username, @NonNull String password) {
Authentication authenticate = null;
try {
// see com.hb0730.boot.admin.security.service.UserDetailsServiceImpl#loadUserByUsername
authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
} catch (Exception e) {
if (e.getCause() instanceof com.hb0730.boot.admin.exceptions.UsernameNotFoundException) {
AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, StatusEnum.FAIL, "用户不存在"));
throw new LoginException(ResponseStatusEnum.USER_NAME_NOT_FONT, "用户不存在");
} else if (e instanceof BadCredentialsException) {
AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, StatusEnum.FAIL, "用户名或者密码错误"));
throw new LoginException(ResponseStatusEnum.USER_PASSWORD_ERROR, "用户名或者密码错误");
} else {
AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, StatusEnum.FAIL, e.getMessage()));
throw new LoginException(ResponseStatusEnum.USE_LOGIN_ERROR, "登录异常,请稍后尝试", e);
}
}
User user = (User) authenticate.getPrincipal();
String accessToken = tokenService.createAccessToken(user);
LoginUser loginUser = BeanUtil.toBean(user, LoginUser.class);
assert loginUser != null;
loginUser.setAccessToken(accessToken);
AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, StatusEnum.SUCCESS, "登录成功"));
return loginUser;
}
Aggregations