feat: jwt config and tag creator name fetch

main
itao 12 months ago
parent 5fbe76ebd5
commit 823f626227

@ -2,17 +2,31 @@ package com.luoo.tag.client;
import api.Result;
import com.luoo.tag.pojo.UserInfo;
import lombok.Data;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
import java.util.stream.Collectors;
@FeignClient("luoo-user")
public interface UserClient {
@GetMapping("/admin/list")
public Result<List<UserInfo>> queryAdminList(List<String> idList);
@GetMapping("/cms/admin/ids/{ids}")
public Result<List<AdminUser>> queryAdminList(@PathVariable("ids") String ids);
@GetMapping("/admin")
public Result<List<UserInfo>> findAll(List<String> idList);
default List<UserInfo> queryByIds(List<String> idList){
String ids = String.join(",", idList);
Result<List<AdminUser>> result = queryAdminList(ids);
return result.getData().stream()
.map(adminUser -> UserInfo.builder().id(adminUser.getId()).name(adminUser.getLoginname()).build())
.collect(Collectors.toList());
}
@Data
class AdminUser{
private String id;
private String loginname;
}
}

@ -20,12 +20,7 @@ public class RequestContext {
}
public static UserInfo get() {
UserInfo userInfo = THREAD_LOCAL.get();
// TODO 测试值
if(Objects.isNull(userInfo)) {
userInfo = UserInfo.builder().id("1627863701048659968").name("foo").build();
}
return userInfo;
return THREAD_LOCAL.get();
}
public static void remove() {

@ -18,8 +18,7 @@ public class WebMvcConfig extends WebMvcConfigurationSupport {
*/
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(jwtInterceptor).
//addPathPatterns("/**").
excludePathPatterns("/**");
addPathPatterns("/**");
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {

@ -32,6 +32,6 @@ public interface TagDao extends JpaRepository<Tag, String>, JpaSpecificationExec
@Query(value = "select new com.luoo.tag.pojo.TagCountDTO(parentId, count(*)) from Tag where parentId in :parentIds group by parentId")
List<TagCountDTO> countByParentIds(List<String> parentIds);
@Query(value = "select distinct creator_id from tb_tag_info order by create_time desc", nativeQuery = true)
@Query(value = "select distinct creator_id from tb_tag_info", nativeQuery = true)
List<String> queryCreator();
}

@ -1,11 +1,11 @@
package com.luoo.tag.interceptor;
import com.luoo.tag.config.RequestContext;
import exception.BizException;
import io.jsonwebtoken.Claims;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@ -25,9 +25,9 @@ public class JwtInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
log.info("JWT拦截器");
String authHeader = request.getHeader("Authorization");
if(StringUtils.isBlank(authHeader) && !authHeader.startsWith("Bearer ")){
return true;
String authHeader = request.getHeader("Admin-token");
if(StringUtils.isBlank(authHeader) || !authHeader.startsWith("Bearer ")){
throw new BizException("JWT令牌缺失");
}
try {
@ -35,7 +35,7 @@ public class JwtInterceptor implements HandlerInterceptor {
Claims claims = jwtUtil.parseJWT(token);
RequestContext.set(claims.getId(), claims.getSubject());
} catch (Exception e) {
throw new RuntimeException("JWT令牌解析异常");
throw new BizException("JWT令牌解析异常");
}
return true;

@ -57,6 +57,9 @@ public class TagService {
.distinct().collect(toList());
Map<String, Tag> parentTagMap = queryTagMap(parentIdList);
Map<String, TagStatistic> tagStatisticMap = queryTagStatistic(tagIdList);
List<String> creatorIdList = tagPage.stream().map(Tag::getCreatorId).distinct().collect(toList());
List<UserInfo> userInfoList = userClient.queryByIds(creatorIdList);
Map<String, String> userInfoMap = userInfoList.stream().collect(toMap(UserInfo::getId, UserInfo::getName));
Page<TagDTO> pageDTOPage = tagPage.map(tag -> {
TagDTO tagDTO = new TagDTO();
BeanUtils.copyProperties(tag, tagDTO);
@ -68,6 +71,7 @@ public class TagService {
if (Objects.nonNull(parentTag)) {
tagDTO.setParentNameCh(parentTag.getNameCh());
}
tagDTO.setCreatorName(userInfoMap.get(tag.getCreatorId()));
return tagDTO;
});
return new PageResult<>(pageDTOPage.getTotalElements(), pageDTOPage.getContent());
@ -211,15 +215,8 @@ public class TagService {
* @return
*/
public List<UserInfo> queryCreator(){
/*List<String> creatorIdList = tagDao.queryCreator();
Result<List<UserInfo>> userInfoResult = userClient.queryAdminList(creatorIdList);
long resultCode = userInfoResult.getCode();
if(resultCode == 0){
return userInfoResult.getData();
}*/
UserInfo foo = UserInfo.builder().id("1627863701048659968").name("foo").build();
UserInfo other = UserInfo.builder().id("1627863701048659969").name("老左").build();
return Lists.newArrayList(foo, other);
List<String> creatorIdList = tagDao.queryCreator();
return userClient.queryByIds(creatorIdList);
}
/**

Loading…
Cancel
Save