Search in sources :

Example 1 with ClientVO

use of com.haier.api.user.domain.ClientVO in project su-sunday-cloud by illeagalName.

the class ResourcePreloadRunner method run.

@Override
public void run(ApplicationArguments args) {
    clearCache(CacheConstants.AUTHORIZATION_USER_ROLE + "*");
    log.info("加载角色菜单资源开始 {}", LocalDateTime.now());
    QueryWrapper<Role> roleWrapper = new QueryWrapper<>();
    roleWrapper.eq("status", 0).eq("is_delete", 0);
    List<Role> roles = roleMapper.selectList(roleWrapper);
    roles.parallelStream().forEach(item -> {
        List<Menu> menus = menuMapper.listMenusByRoleId(item.getRoleId());
        RoleVO role = new RoleVO();
        BeanUtils.copyProperties(item, role);
        role.setMenus(menus.stream().map(Menu::getSymbol).filter(Objects::nonNull).collect(Collectors.toList()));
        // 缓存起来
        redisService.setObject(CacheConstants.AUTHORIZATION_USER_ROLE + role.getRoleId(), role);
    });
    clearCache(CacheConstants.AUTHORIZATION_USER_MENU + "*");
    QueryWrapper<Menu> menuWrapper = new QueryWrapper<>();
    menuWrapper.orderByAsc("parent_id", "menu_sort");
    List<Menu> menus = menuMapper.selectList(menuWrapper);
    menus.forEach(item -> {
        MenuVO menu = new MenuVO();
        BeanUtils.copyProperties(item, menu);
        redisService.setObject(CacheConstants.AUTHORIZATION_USER_MENU + item.getMenuId(), menu);
    });
    clearCache(CacheConstants.AUTHORIZATION_USER_CLIENT + "*");
    log.info("加载client资源开始 {}", LocalDateTime.now());
    List<AuthClient> authClients = authClientMapper.listAuthClients();
    authClients.stream().map(client -> {
        ClientVO clientVO = new ClientVO();
        clientVO.setClientId(client.getClientId());
        clientVO.setTime(client.getAccessTokenValidity());
        clientVO.setRemark(client.getRemark());
        return clientVO;
    }).forEach(m -> {
        redisService.setObject(CacheConstants.AUTHORIZATION_USER_CLIENT + m.getClientId(), m);
    });
    log.info("加载资源结束 {}", LocalDateTime.now());
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) RoleVO(com.haier.api.user.domain.RoleVO) AuthClientMapper(com.haier.user.dao.AuthClientMapper) AuthClient(com.haier.user.domain.AuthClient) ApplicationArguments(org.springframework.boot.ApplicationArguments) ApplicationRunner(org.springframework.boot.ApplicationRunner) Menu(com.haier.user.domain.Menu) MenuVO(com.haier.api.user.domain.MenuVO) LocalDateTime(java.time.LocalDateTime) ClientVO(com.haier.api.user.domain.ClientVO) Autowired(org.springframework.beans.factory.annotation.Autowired) Collectors(java.util.stream.Collectors) RedisService(com.haier.redis.service.RedisService) CacheConstants(com.haier.core.constant.CacheConstants) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) MenuMapper(com.haier.user.dao.MenuMapper) RoleMapper(com.haier.user.dao.RoleMapper) Role(com.haier.user.domain.Role) BeanUtils(org.springframework.beans.BeanUtils) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) AuthClient(com.haier.user.domain.AuthClient) Role(com.haier.user.domain.Role) MenuVO(com.haier.api.user.domain.MenuVO) RoleVO(com.haier.api.user.domain.RoleVO) Objects(java.util.Objects) Menu(com.haier.user.domain.Menu) ClientVO(com.haier.api.user.domain.ClientVO)

Example 2 with ClientVO

use of com.haier.api.user.domain.ClientVO in project su-sunday-cloud by illeagalName.

the class AuthService method createToken.

private Map<String, Object> createToken(UserVO user) {
    // 获取加密盐
    String secret = SecurityUtils.generateSecurityCode();
    // 从header中获取clientId
    String clientId = ServletUtils.getHeader(DETAILS_CLIENT_ID);
    AssertUtils.notEmpty(clientId, "客户端标识不正确");
    ClientVO clientMap = redisService.getObject(CacheConstants.AUTHORIZATION_USER_CLIENT + clientId);
    AssertUtils.notEmpty(clientMap, "客户端标识不正确");
    // 生成token
    Date date = new Date(System.currentTimeMillis() + clientMap.getTime() * 1000);
    String token = SecurityUtils.createToken(new HashMap<>() {

        {
            put("uniqueId", user.getUserId() + "");
            put("clientId", clientId);
        }
    }, secret, date);
    user.setClientId(clientId);
    user.setSecret(secret);
    user.setToken(token);
    user.setIpaddr(IpUtils.getIpAddr(ServletUtils.getRequest()));
    user.setLoginTime(LocalDateTime.now());
    user.setExpireTime(DateUtils.toLocalDateTime(date));
    // 保存或更新用户token
    Map<String, Object> map = new HashMap<>();
    map.put("token", token);
    redisService.setObject(AUTHORIZATION_USER_TOKEN + clientId + ":" + user.getUserId(), user, clientMap.getTime() - 1, TimeUnit.SECONDS);
    return map;
}
Also used : ClientVO(com.haier.api.user.domain.ClientVO)

Aggregations

ClientVO (com.haier.api.user.domain.ClientVO)2 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)1 MenuVO (com.haier.api.user.domain.MenuVO)1 RoleVO (com.haier.api.user.domain.RoleVO)1 CacheConstants (com.haier.core.constant.CacheConstants)1 RedisService (com.haier.redis.service.RedisService)1 AuthClientMapper (com.haier.user.dao.AuthClientMapper)1 MenuMapper (com.haier.user.dao.MenuMapper)1 RoleMapper (com.haier.user.dao.RoleMapper)1 AuthClient (com.haier.user.domain.AuthClient)1 Menu (com.haier.user.domain.Menu)1 Role (com.haier.user.domain.Role)1 LocalDateTime (java.time.LocalDateTime)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 BeanUtils (org.springframework.beans.BeanUtils)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 ApplicationArguments (org.springframework.boot.ApplicationArguments)1