一、自媒体后台搭建
1、搭建自媒体平台网关:leadnew-wemedia-gateway
2、搭建自媒体微服务:leadnews-wemedia-service
二、自媒体素材管理
图片上传
1、如何获得用户信息
在自媒体网关过滤器filter中解析token,获取用户id,存入headers
package com.heima.wemedia.gateway.filter; import com.heima.wemedia.gateway.util.AppJwtUtil; import io.jsonwebtoken.Claims; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.Ordered; import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; @Component @Slf4j public class AuthorizeFilter implements Ordered, GlobalFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { //1.获取request和response对象 ServerHttpRequest request = exchange.getRequest(); ServerHttpResponse response = exchange.getResponse(); //2.判断是否是登录 if(request.getURI().getPath().contains("/login")){ //放行 return chain.filter(exchange); } //3.获取token String token = request.getHeaders().getFirst("token"); //4.判断token是否存在 if(StringUtils.isBlank(token)){ response.setStatusCode(HttpStatus.UNAUTHORIZED); return response.setComplete(); } //5.判断token是否有效 try { Claims claimsBody = AppJwtUtil.getClaimsBody(token); //是否是过期 int result = AppJwtUtil.verifyToken(claimsBody); if(result == 1 || result == 2){ response.setStatusCode(HttpStatus.UNAUTHORIZED); return response.setComplete(); } //获取用户信息 Object userId = claimsBody.get("id"); //存入header ServerHttpRequest serverHttpRequest = request.mutate().headers(httpHeaders -> { httpHeaders.add("userId", userId + ""); }).build(); //重置请求 exchange.mutate().request(serverHttpRequest); } catch (Exception e) { e.printStackTrace(); } //6.放行 return chain.filter(exchange); } /** * 优先级设置 值越小 优先级越高 * @return */ @Override public int getOrder() { return 0; } }
在自媒体微服务中创建拦截器,从header中获取用户存入当前线程ThreadLocal。
ThreadLocal:
package com.heima.utils.common.thread; import com.heima.model.wemedia.pojos.WmUser; public class WmThreadLocalUtil { public final static ThreadLocal<WmUser> WM_USER_THREAD_LOCAL = new ThreadLocal<>(); //存入线程 public void setUser(WmUser wmUser){ WM_USER_THREAD_LOCAL.set(wmUser); } //从线程中获取 public static WmUser getUser(){ return WM_USER_THREAD_LOCAL.get(); } //清理 public static void clear(){ WM_USER_THREAD_LOCAL.remove(); } }
拦截器:
package com.heima.wemedia.interceptor; import com.heima.model.wemedia.pojos.WmUser; import com.heima.utils.common.thread.WmThreadLocalUtil; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class WmTokenInterceptor implements HandlerInterceptor { /** * 得到header中的用户信息并且存入当前线程 * @param request * @param response * @param handler * @return * @throws Exception */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String userId = request.getHeader("userId"); if(userId != null){ //存入当前线程 WmUser wmUser = new WmUser(); wmUser.setId(Integer.valueOf(userId)); WmThreadLocalUtil.setUser(wmUser); } return true; } /** * 清理线程中的数据 * @param request * @param response * @param handler * @param modelAndView * @throws Exception */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { WmThreadLocalUtil.clear(); } }
package com.heima.wemedia.config; import com.heima.wemedia.interceptor.WmTokenInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new WmTokenInterceptor()).addPathPatterns("/**"); } }
package com.heima.wemedia.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.heima.file.service.FileStorageService; import com.heima.model.common.dtos.ResponseResult; import com.heima.model.common.enums.AppHttpCodeEnum; import com.heima.model.wemedia.pojos.WmMaterial; import com.heima.utils.common.thread.WmThreadLocalUtil; import com.heima.wemedia.mapper.WmMaterialMapper; import com.heima.wemedia.service.WmMaterialService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.Date; import java.util.UUID; @Slf4j @Service @Transactional public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService { @Autowired private FileStorageService fileStorageService; /** * 素材图片上传 * @param multipartFile * @return */ @Override public ResponseResult uploadPicture(MultipartFile multipartFile) { //1、检查参数 if(multipartFile == null || multipartFile.getSize() == 0){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID); } //2、上传图片到minIO中 String fileName = UUID.randomUUID().toString().replace("-", ""); //aa.jpg String originalFilename = multipartFile.getOriginalFilename(); String postfix = originalFilename.substring(originalFilename.lastIndexOf(".")); String fileId = null; try { fileId = fileStorageService.uploadImgFile("", fileName + postfix, multipartFile.getInputStream()); log.info("上传图片到minIO中, fileId:" + fileId); } catch (IOException e) { e.printStackTrace(); log.error("WmMaterialServiceImpl-上传文件失败"); } //3、保存到数据库中 WmMaterial wmMaterial = new WmMaterial(); wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId()); wmMaterial.setUrl(fileId); wmMaterial.setIsCollection((short)0); wmMaterial.setType((short)0); wmMaterial.setCreatedTime(new Date()); save(wmMaterial); //4、返回结果 return ResponseResult.okResult(wmMaterial); } }
标签:heima,com,springframework,素材,org,后台,import,上传,public From: https://www.cnblogs.com/fxzm/p/17511763.html