Skip to content

Commit

Permalink
fix($auth-center): read nested JSON object as Java object
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Jun 29, 2021
1 parent e9d935b commit af101b2
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.jmsoftware.maf.common.bean.ResponseBodyBean;
import com.jmsoftware.maf.common.domain.authcenter.permission.GetPermissionListByRoleIdListPayload;
import com.jmsoftware.maf.common.domain.authcenter.permission.GetPermissionListByRoleIdListResponse;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdResponse;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdSingleResponse;
import com.jmsoftware.maf.common.domain.authcenter.security.ParseJwtResponse;
import com.jmsoftware.maf.common.domain.authcenter.user.GetUserByLoginTokenResponse;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -62,7 +62,7 @@ public Mono<GetUserByLoginTokenResponse> getUserByLoginToken(@PathVariable Strin
* @param userId the user id
* @return the role list by user id
*/
public Mono<List<GetRoleListByUserIdResponse.Role>> getRoleListByUserId(@NotNull Long userId) {
public Mono<List<GetRoleListByUserIdSingleResponse>> getRoleListByUserId(@NotNull Long userId) {
return webClientBuilder
.build()
.get()
Expand All @@ -71,7 +71,7 @@ public Mono<List<GetRoleListByUserIdResponse.Role>> getRoleListByUserId(@NotNull
.bodyToMono(ResponseBodyBean.class)
.map(ResponseBodyBean::getData)
.map(data -> JSONUtil.toList(JSONUtil.parseObj(data).getJSONArray("roleList"),
GetRoleListByUserIdResponse.Role.class));
GetRoleListByUserIdSingleResponse.class));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.jmsoftware.maf.common.domain.authcenter.permission.GetPermissionListByRoleIdListPayload;
import com.jmsoftware.maf.common.domain.authcenter.permission.GetPermissionListByRoleIdListResponse;
import com.jmsoftware.maf.common.domain.authcenter.permission.PermissionType;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdResponse;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdSingleResponse;
import com.jmsoftware.maf.common.domain.authcenter.security.UserPrincipal;
import com.jmsoftware.maf.common.exception.SecurityException;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -49,7 +49,7 @@ public class RbacReactiveAuthorizationManagerImpl implements ReactiveAuthorizati
* @param userPrincipalMono the user principal mono
* @return the flux
*/
private Flux<GetRoleListByUserIdResponse.Role> retrieveRoles(Mono<UserPrincipal> userPrincipalMono) {
private Flux<GetRoleListByUserIdSingleResponse> retrieveRoles(Mono<UserPrincipal> userPrincipalMono) {
// Get role list by user ID, and then convert to Flux<?>
return userPrincipalMono
.flatMap(userPrincipal -> authCenterRemoteApi.getRoleListByUserId(userPrincipal.getId()))
Expand All @@ -63,11 +63,11 @@ private Flux<GetRoleListByUserIdResponse.Role> retrieveRoles(Mono<UserPrincipal>
* @param roleFlux the role flux
* @return the mono
*/
private Mono<List<Long>> mapRole(Flux<GetRoleListByUserIdResponse.Role> roleFlux) {
private Mono<List<Long>> mapRole(Flux<GetRoleListByUserIdSingleResponse> roleFlux) {
return roleFlux
.map(GetRoleListByUserIdResponse.Role::getId)
.map(GetRoleListByUserIdSingleResponse::getId)
.collectList()
.switchIfEmpty(roleFlux.map(GetRoleListByUserIdResponse.Role::getId).collectList());
.switchIfEmpty(roleFlux.map(GetRoleListByUserIdSingleResponse::getId).collectList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jmsoftware.maf.authcenter.role.entity.persistence.Role;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdSingleResponse;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
Expand All @@ -22,7 +23,7 @@ public interface RoleMapper extends BaseMapper<Role> {
* @param userId the user id
* @return the list
*/
List<Role> selectRoleListByUserId(Long userId);
List<GetRoleListByUserIdSingleResponse> selectRoleListByUserId(Long userId);

/**
* Select by id role persistence.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.baomidou.mybatisplus.extension.service.IService;
import com.jmsoftware.maf.authcenter.role.entity.persistence.Role;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdResponse;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdSingleResponse;
import lombok.NonNull;
import org.springframework.validation.annotation.Validated;

Expand Down Expand Up @@ -34,7 +35,7 @@ public interface RoleService extends IService<Role> {
* @param userId the user id
* @return the role list by user id
*/
List<Role> getRoleListByUserId(@NonNull Long userId);
List<GetRoleListByUserIdSingleResponse> getRoleListByUserId(@NonNull Long userId);

/**
* Check admin boolean.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package com.jmsoftware.maf.authcenter.role.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jmsoftware.maf.authcenter.role.entity.constant.RoleRedisKey;
import com.jmsoftware.maf.authcenter.role.entity.persistence.Role;
import com.jmsoftware.maf.authcenter.role.mapper.RoleMapper;
import com.jmsoftware.maf.authcenter.role.service.RoleService;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdResponse;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdSingleResponse;
import com.jmsoftware.maf.springcloudstarter.configuration.MafConfiguration;
import com.jmsoftware.maf.springcloudstarter.configuration.MafProjectProperty;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.val;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
Expand All @@ -41,28 +43,26 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements Ro
private final MafProjectProperty mafProjectProperty;
private final MafConfiguration mafConfiguration;
private final RedisTemplate<String, String> redisTemplate;
private final ObjectMapper objectMapper;

@Override
@SneakyThrows({JsonProcessingException.class})
public GetRoleListByUserIdResponse getRoleList(@NotNull Long userId) {
val key = String.format(mafProjectProperty.getProjectParentArtifactId()
+ RoleRedisKey.GET_ROLE_LIST_BY_USER_ID.getKeyInfixFormat(), userId);
val hasKey = redisTemplate.hasKey(key);
if (BooleanUtil.isTrue(hasKey)) {
return JSONUtil.toBean(redisTemplate.opsForValue().get(key), GetRoleListByUserIdResponse.class);
return objectMapper.readValue(redisTemplate.opsForValue().get(key), GetRoleListByUserIdResponse.class);
}
val roleList = this.getRoleListByUserId(userId);
GetRoleListByUserIdResponse response = new GetRoleListByUserIdResponse();
roleList.forEach(rolePersistence -> {
GetRoleListByUserIdResponse.Role role = new GetRoleListByUserIdResponse.Role();
BeanUtil.copyProperties(rolePersistence, role);
response.getRoleList().add(role);
});
redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(response), RandomUtil.randomLong(1, 7), TimeUnit.DAYS);
val response = new GetRoleListByUserIdResponse();
response.setRoleList(this.getRoleListByUserId(userId));
redisTemplate.opsForValue().set(key, objectMapper.writeValueAsString(response), RandomUtil.randomLong(1, 7),
TimeUnit.DAYS);
return response;
}

@Override
public List<Role> getRoleListByUserId(@NonNull Long userId) {
public List<GetRoleListByUserIdSingleResponse> getRoleListByUserId(@NonNull Long userId) {
return this.getBaseMapper().selectRoleListByUserId(userId);
}

Expand Down
10 changes: 4 additions & 6 deletions auth-center/src/main/resources/mapper/role/RoleMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
id, `name`, description, created_by, created_time, modified_by, modified_time, deleted
</sql>

<select id="selectRoleListByUserId" resultMap="BaseResultMap">
SELECT role.id,
role.name,
role.description,
role.created_time,
role.modified_time
<select id="selectRoleListByUserId"
resultType="com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdSingleResponse">
SELECT role.id AS id,
role.name AS name
FROM role
LEFT JOIN user_role ON role.id = user_role.role_id
LEFT JOIN user ON user_role.user_id = user.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.Data;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

Expand All @@ -14,12 +15,7 @@
* @date 5/10/20 10:55 PM
**/
@Data
public class GetRoleListByUserIdResponse {
private final List<Role> roleList = new LinkedList<>();

@Data
public static class Role {
private Long id;
private String name;
}
public class GetRoleListByUserIdResponse implements Serializable {
private static final long serialVersionUID = -8462678958191383914L;
private List<GetRoleListByUserIdSingleResponse> roleList;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.jmsoftware.maf.common.domain.authcenter.role;

import lombok.Data;

import java.io.Serializable;

/**
* Description: GetRoleListByUserIdSingleResponse, change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/29/2021 10:17 AM
**/
@Data
public class GetRoleListByUserIdSingleResponse implements Serializable {
private static final long serialVersionUID = 3758434255123050684L;
private Long id;
private String name;
}

0 comments on commit af101b2

Please sign in to comment.