Search in sources :

Example 1 with UserDto

use of com.besscroft.aurora.mall.common.domain.UserDto in project aurora-mall by besscroft.

the class UserServiceImpl method loadUserByUsername.

@Override
public UserDto loadUserByUsername(String username) {
    AuthUser authUser = this.baseMapper.selectAuthUserByUsername(username);
    if (authUser != null) {
        List<AuthRole> authRoles = authRoleMapper.selectAuthRoleListByAdminId(authUser.getId());
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(authUser, userDto);
        if (CollUtil.isNotEmpty(authRoles)) {
            List<String> roleStrList = authRoles.stream().map(item -> item.getId() + "_" + item.getName()).collect(Collectors.toList());
            userDto.setRoles(roleStrList);
        }
        return userDto;
    }
    return null;
}
Also used : BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) AuthUserExcelDto(com.besscroft.aurora.mall.common.domain.AuthUserExcelDto) UserDto(com.besscroft.aurora.mall.common.domain.UserDto) AuthFeignClient(com.besscroft.aurora.mall.admin.api.AuthFeignClient) RequiredArgsConstructor(lombok.RequiredArgsConstructor) LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) EasyExcel(com.alibaba.excel.EasyExcel) AdminParam(com.besscroft.aurora.mall.admin.domain.param.AdminParam) JSONUtil(cn.hutool.json.JSONUtil) HttpServletRequest(javax.servlet.http.HttpServletRequest) UserService(com.besscroft.aurora.mall.admin.service.UserService) Service(org.springframework.stereotype.Service) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) Map(java.util.Map) AuthRole(com.besscroft.aurora.mall.common.entity.AuthRole) ServiceImpl(com.baomidou.mybatisplus.extension.service.impl.ServiceImpl) MenuService(com.besscroft.aurora.mall.admin.service.MenuService) AuthConstants(com.besscroft.aurora.mall.common.constant.AuthConstants) HttpServletResponse(javax.servlet.http.HttpServletResponse) UserConverterMapper(com.besscroft.aurora.mall.admin.converter.UserConverterMapper) PageHelper(com.github.pagehelper.PageHelper) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) AjaxResult(com.besscroft.aurora.mall.common.result.AjaxResult) Slf4j(lombok.extern.slf4j.Slf4j) URLEncoder(java.net.URLEncoder) List(java.util.List) AuthRoleMapper(com.besscroft.aurora.mall.admin.mapper.AuthRoleMapper) AuthUser(com.besscroft.aurora.mall.common.entity.AuthUser) AuthUserMapper(com.besscroft.aurora.mall.admin.mapper.AuthUserMapper) BeanUtils(org.springframework.beans.BeanUtils) Transactional(org.springframework.transaction.annotation.Transactional) AuthRole(com.besscroft.aurora.mall.common.entity.AuthRole) UserDto(com.besscroft.aurora.mall.common.domain.UserDto) AuthUser(com.besscroft.aurora.mall.common.entity.AuthUser)

Example 2 with UserDto

use of com.besscroft.aurora.mall.common.domain.UserDto in project aurora-mall by besscroft.

the class UserServiceImpl method getCurrentAdmin.

@Override
public AuthUser getCurrentAdmin() {
    String header = request.getHeader(AuthConstants.USER_TOKEN_HEADER);
    if (StrUtil.isEmpty(header)) {
        log.error("暂未登录或token已经过期");
    }
    UserDto userDto = JSONUtil.toBean(header, UserDto.class);
    return this.baseMapper.selectById(userDto.getId());
}
Also used : UserDto(com.besscroft.aurora.mall.common.domain.UserDto)

Example 3 with UserDto

use of com.besscroft.aurora.mall.common.domain.UserDto in project aurora-mall by besscroft.

the class UserDetailsServiceImpl method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    String clientId = request.getParameter(AuthConstants.JWT_CLIENT_ID_KEY);
    UserDto userDto = null;
    switch(clientId) {
        case // 后台用户
        AuthConstants.ADMIN_CLIENT_ID:
            userDto = adminFeignClient.loadUserByUsername(username);
            break;
        case // 前台会员
        AuthConstants.PORTAL_CLIENT_ID:
            userDto = userFeignClient.loadUserByUsername(username);
            break;
    }
    if (userDto == null) {
        throw new UsernameNotFoundException(MessageConstant.USERNAME_PASSWORD_ERROR);
    }
    userDto.setClientId(clientId);
    User user = new User(userDto);
    if (!user.isEnabled()) {
        throw new DisabledException(MessageConstant.ACCOUNT_DISABLED);
    } else if (!user.isAccountNonLocked()) {
        throw new LockedException(MessageConstant.ACCOUNT_LOCKED);
    } else if (!user.isAccountNonExpired()) {
        throw new AccountExpiredException(MessageConstant.ACCOUNT_EXPIRED);
    } else if (!user.isCredentialsNonExpired()) {
        throw new CredentialsExpiredException(MessageConstant.CREDENTIALS_EXPIRED);
    }
    return user;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(com.besscroft.aurora.mall.auth.domain.User) LockedException(org.springframework.security.authentication.LockedException) CredentialsExpiredException(org.springframework.security.authentication.CredentialsExpiredException) AccountExpiredException(org.springframework.security.authentication.AccountExpiredException) UserDto(com.besscroft.aurora.mall.common.domain.UserDto) DisabledException(org.springframework.security.authentication.DisabledException)

Example 4 with UserDto

use of com.besscroft.aurora.mall.common.domain.UserDto in project aurora-mall by besscroft.

the class UserServiceTest method loadUserByUsername.

@Test
void loadUserByUsername() throws Exception {
    String username = "admin";
    UserDto dto = userService.loadUserByUsername(username);
    assertNotNull(dto, "获取当前用户信息失败!");
    LOGGER.info("当前用户的信息:{}", objectMapper.writeValueAsString(dto));
}
Also used : UserDto(com.besscroft.aurora.mall.common.domain.UserDto) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

UserDto (com.besscroft.aurora.mall.common.domain.UserDto)4 CollUtil (cn.hutool.core.collection.CollUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 JSONUtil (cn.hutool.json.JSONUtil)1 EasyExcel (com.alibaba.excel.EasyExcel)1 ServiceImpl (com.baomidou.mybatisplus.extension.service.impl.ServiceImpl)1 AuthFeignClient (com.besscroft.aurora.mall.admin.api.AuthFeignClient)1 UserConverterMapper (com.besscroft.aurora.mall.admin.converter.UserConverterMapper)1 AdminParam (com.besscroft.aurora.mall.admin.domain.param.AdminParam)1 AuthRoleMapper (com.besscroft.aurora.mall.admin.mapper.AuthRoleMapper)1 AuthUserMapper (com.besscroft.aurora.mall.admin.mapper.AuthUserMapper)1 MenuService (com.besscroft.aurora.mall.admin.service.MenuService)1 UserService (com.besscroft.aurora.mall.admin.service.UserService)1 User (com.besscroft.aurora.mall.auth.domain.User)1 AuthConstants (com.besscroft.aurora.mall.common.constant.AuthConstants)1 AuthUserExcelDto (com.besscroft.aurora.mall.common.domain.AuthUserExcelDto)1 AuthRole (com.besscroft.aurora.mall.common.entity.AuthRole)1 AuthUser (com.besscroft.aurora.mall.common.entity.AuthUser)1 AjaxResult (com.besscroft.aurora.mall.common.result.AjaxResult)1 PageHelper (com.github.pagehelper.PageHelper)1