Search in sources :

Example 1 with LoginException

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;
}
Also used : User(com.hb0730.boot.admin.security.model.User) LoginException(com.hb0730.boot.admin.exceptions.LoginException) MenuEvent(com.hb0730.boot.admin.event.menu.MenuEvent)

Example 2 with LoginException

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;
}
Also used : TreeMenuDTO(com.hb0730.boot.admin.project.system.menu.model.dto.TreeMenuDTO) User(com.hb0730.boot.admin.security.model.User) LoginException(com.hb0730.boot.admin.exceptions.LoginException) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) MenuEvent(com.hb0730.boot.admin.event.menu.MenuEvent) SneakyThrows(lombok.SneakyThrows)

Example 3 with LoginException

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;
}
Also used : User(com.hb0730.boot.admin.security.model.User) LoginUser(com.hb0730.boot.admin.security.model.LoginUser) Authentication(org.springframework.security.core.Authentication) LoginException(com.hb0730.boot.admin.exceptions.LoginException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) LoginUser(com.hb0730.boot.admin.security.model.LoginUser) LoginException(com.hb0730.boot.admin.exceptions.LoginException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Nullable(org.springframework.lang.Nullable)

Aggregations

LoginException (com.hb0730.boot.admin.exceptions.LoginException)3 User (com.hb0730.boot.admin.security.model.User)3 MenuEvent (com.hb0730.boot.admin.event.menu.MenuEvent)2 TreeMenuDTO (com.hb0730.boot.admin.project.system.menu.model.dto.TreeMenuDTO)1 LoginUser (com.hb0730.boot.admin.security.model.LoginUser)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 SneakyThrows (lombok.SneakyThrows)1 Nullable (org.springframework.lang.Nullable)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1