master
Sheyu-yao 2024-01-17 19:26:26 +08:00
parent 3dfe0ea650
commit 4935a02b59
10 changed files with 110 additions and 43 deletions

View File

@ -2,6 +2,7 @@ package com.ysy.auth.service.impl;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.ysy.common.contant.TokenConstants;
import com.ysy.common.domain.dto.RiderDTO; import com.ysy.common.domain.dto.RiderDTO;
import com.ysy.common.domain.po.Rider; import com.ysy.common.domain.po.Rider;
import com.ysy.common.domain.result.R; import com.ysy.common.domain.result.R;
@ -17,6 +18,7 @@ import org.springframework.util.Assert;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@Service @Service
@ -46,11 +48,15 @@ public class AuthServiceImpl implements AuthService {
"密码错误"); "密码错误");
//生成token //生成token
Map<String, Object> claim = BeanUtil.beanToMap(rider, new HashMap<>(), false, false); Map<String, Object> claim = new HashMap<>();
String tokenKey = UUID.randomUUID().toString();
claim.put(TokenConstants.TOKEN_KEY, tokenKey);
claim.put(TokenConstants.USER_ID, rider.getRiderId());
claim.put(TokenConstants.USER_NAME, rider.getRiderName());
String token = JwtUtil.createToken(claim); String token = JwtUtil.createToken(claim);
//登录成功存入缓存 //登录成功存入缓存
stringRedisTemplate.opsForValue().set(token, stringRedisTemplate.opsForValue().set(TokenConstants.TOKEN_LOGIN + tokenKey,
JSON.toJSONString(rider), JSON.toJSONString(rider),
30, TimeUnit.MINUTES); 30, TimeUnit.MINUTES);

View File

@ -0,0 +1,9 @@
package com.ysy.common.contant;
public class TokenConstants {
public final static String TOKEN = "token";
public final static String TOKEN_LOGIN = "tokenLogin";
public final static String TOKEN_KEY = "tokenKey";
public final static String USER_ID = "userId";
public final static String USER_NAME = "username";
}

View File

@ -1,5 +1,8 @@
package com.ysy.gateway.filter; package com.ysy.gateway.filter;
import com.auth0.jwt.interfaces.Claim;
import com.ysy.common.contant.TokenConstants;
import com.ysy.common.domain.po.Rider;
import com.ysy.gateway.config.AuthProperties; import com.ysy.gateway.config.AuthProperties;
import com.ysy.common.utils.gateway.GatewayUtils; import com.ysy.common.utils.gateway.GatewayUtils;
import com.ysy.common.utils.jwt.JwtUtil; import com.ysy.common.utils.jwt.JwtUtil;
@ -10,10 +13,14 @@ import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.util.Map;
@Component @Component
@Order(1) @Order(1)
@Slf4j @Slf4j
@ -26,9 +33,12 @@ public class GlobalAuthFilter implements GlobalFilter {
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
//获取访问路径 //获取访问路径
String path = exchange.getRequest().getPath().toString(); String path = request.getPath().toString();
log.info("访问服务路径:" + path); HttpMethod method = request.getMethod();
log.info("访问服务路径:[{}], 请求方式:[{}]", path, method);
//匹配白名单 //匹配白名单
boolean matches = StringUtils.matches(path, authProperties.getExcludePaths()); boolean matches = StringUtils.matches(path, authProperties.getExcludePaths());
@ -39,21 +49,31 @@ public class GlobalAuthFilter implements GlobalFilter {
} }
//获取token //获取token
String token = exchange.getRequest().getHeaders().get("token").get(0); String token = request.getHeaders().getFirst(TokenConstants.TOKEN);
if (token == null) if (token == null)
GatewayUtils.errorResponse(exchange, "请先登录"); return GatewayUtils.errorResponse(exchange, "请先登录");
//jwt解析出错 //jwt解析出错
Claim claim = null;
try { try {
JwtUtil.decodedJWT(token); claim = JwtUtil.getClaim(token);
} catch (Exception e) { } catch (Exception e) {
GatewayUtils.errorResponse(exchange, "身份不合法"); return GatewayUtils.errorResponse(exchange, "身份不合法");
} }
//缓存时间失效 Map<String, Object> map = claim.asMap();
if (Boolean.FALSE.equals(stringRedisTemplate.hasKey(token))) String tokenKey = map.get(TokenConstants.TOKEN_KEY).toString();
GatewayUtils.errorResponse(exchange, "会话已过期,请重新登录");
return chain.filter(exchange); //缓存时间失效
if (Boolean.FALSE.equals(stringRedisTemplate.hasKey(TokenConstants.TOKEN_LOGIN + tokenKey)))
return GatewayUtils.errorResponse(exchange, "会话已过期,请重新登录");
ServerHttpRequest.Builder mutate = request.mutate();
GatewayUtils.addHeader(mutate, TokenConstants.TOKEN_KEY, tokenKey);
GatewayUtils.addHeader(mutate, TokenConstants.USER_ID, map.get(TokenConstants.USER_ID));
GatewayUtils.addHeader(mutate, TokenConstants.USER_NAME, map.get(TokenConstants.USER_NAME));
GatewayUtils.removeHeader(mutate, TokenConstants.TOKEN);
return chain.filter(exchange.mutate().request(mutate.build()).build());
} }
} }

View File

@ -19,4 +19,10 @@ public class ApplyController {
IPage<Apply> applyIPage = applyService.getApplyList(applyVO); IPage<Apply> applyIPage = applyService.getApplyList(applyVO);
return R.success("applyList", applyIPage); return R.success("applyList", applyIPage);
} }
@PostMapping("/add")
public R applyAdd(@RequestBody Apply apply) {
applyService.applyAdd(apply);
return R.success("添加成功");
}
} }

View File

@ -6,4 +6,5 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper @Mapper
public interface ApplyMapper extends BaseMapper<Apply> { public interface ApplyMapper extends BaseMapper<Apply> {
} }

View File

@ -3,9 +3,10 @@ package com.ysy.apply.service;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.ysy.common.domain.po.Apply; import com.ysy.common.domain.po.Apply;
import com.ysy.common.domain.result.R;
import com.ysy.common.domain.vo.ApplyVO; import com.ysy.common.domain.vo.ApplyVO;
public interface ApplyService extends IService<Apply> { public interface ApplyService extends IService<Apply> {
IPage<Apply> getApplyList(ApplyVO applyVO); IPage<Apply> getApplyList(ApplyVO applyVO);
void applyAdd(Apply apply);
} }

View File

@ -2,13 +2,12 @@ package com.ysy.apply.service.impl;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysy.common.contant.TokenConstants;
import com.ysy.common.domain.po.Apply; import com.ysy.common.domain.po.Apply;
import com.ysy.common.domain.po.Rider; import com.ysy.common.domain.po.Rider;
import com.ysy.common.domain.result.R;
import com.ysy.apply.mapper.ApplyMapper; import com.ysy.apply.mapper.ApplyMapper;
import com.ysy.apply.service.ApplyService; import com.ysy.apply.service.ApplyService;
import com.ysy.common.domain.vo.ApplyVO; import com.ysy.common.domain.vo.ApplyVO;
@ -18,8 +17,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
@Service @Service
public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements ApplyService { public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements ApplyService {
@ -31,10 +28,10 @@ public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements
@Override @Override
public IPage<Apply> getApplyList(ApplyVO applyVO) { public IPage<Apply> getApplyList(ApplyVO applyVO) {
String token = request.getHeader("token"); String userId = request.getHeader(TokenConstants.USER_ID);
String jsonString = stringRedisTemplate.opsForValue().get(token); // String jsonString = stringRedisTemplate.opsForValue().get(token);
Rider rider = JSON.parseObject(jsonString, Rider.class); // Rider rider = JSON.parseObject(jsonString, Rider.class);
IPage<Apply> page = new Page<>(); IPage<Apply> page = new Page<>();
page.setCurrent(applyVO.getPageNum()); page.setCurrent(applyVO.getPageNum());
@ -50,11 +47,16 @@ public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements
.and(a -> { .and(a -> {
a.eq(Apply::getRiderId, 0) a.eq(Apply::getRiderId, 0)
.or(a1 -> .or(a1 ->
a1.eq(Apply::getRiderId, rider.getRiderId()) a1.eq(Apply::getRiderId, userId)
.eq(Apply::getState, 0)); .eq(Apply::getState, 0));
}) })
.orderByDesc(Apply::getOrderTime); .orderByDesc(Apply::getOrderTime);
return page(page, lambdaQueryWrapper); return page(page, lambdaQueryWrapper);
} }
@Override
public void applyAdd(Apply apply) {
save(apply);
}
} }

View File

@ -1,12 +1,10 @@
package com.ysy.system.controller; package com.ysy.system.controller;
import com.ysy.common.domain.po.Rider;
import com.ysy.common.domain.result.R; import com.ysy.common.domain.result.R;
import com.ysy.system.service.RiderService; import com.ysy.system.service.RiderService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping("/rider") @RequestMapping("/rider")
@ -16,11 +14,23 @@ public class RiderController {
@GetMapping("/get/{riderName}") @GetMapping("/get/{riderName}")
public R getRiderByRiderName(@PathVariable String riderName) { public R getRiderByRiderName(@PathVariable String riderName) {
return riderService.getRiderByRiderName(riderName); return R.success(
"rider",
riderService.getRiderByRiderName(riderName)
);
} }
@GetMapping("/rider") @GetMapping("/info")
public R rider() { public R rider() {
return riderService.getRiderById(); return R.success(
"rider",
riderService.getRiderById()
);
}
@PutMapping("/modify")
public R riderModify(@RequestParam Integer takeKilo, @RequestParam Integer takePrice) {
riderService.modify(takeKilo, takePrice);
return R.success();
} }
} }

View File

@ -5,7 +5,9 @@ import com.ysy.common.domain.po.Rider;
import com.ysy.common.domain.result.R; import com.ysy.common.domain.result.R;
public interface RiderService extends IService<Rider> { public interface RiderService extends IService<Rider> {
R getRiderByRiderName(String riderName); Rider getRiderByRiderName(String riderName);
R getRiderById(); Rider getRiderById();
void modify(Integer takeKilo, Integer takePrice);
} }

View File

@ -2,8 +2,10 @@ package com.ysy.system.service.impl;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysy.common.contant.TokenConstants;
import com.ysy.common.domain.po.Rider; import com.ysy.common.domain.po.Rider;
import com.ysy.common.domain.result.R; import com.ysy.common.domain.result.R;
import com.ysy.system.mapper.RiderMapper; import com.ysy.system.mapper.RiderMapper;
@ -19,24 +21,32 @@ public class RiderServiceImpl extends ServiceImpl<RiderMapper, Rider> implements
@Autowired @Autowired
private HttpServletRequest request; private HttpServletRequest request;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override @Override
public R getRiderByRiderName(String riderName) { public Rider getRiderByRiderName(String riderName) {
LambdaQueryWrapper<Rider> lambdaQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<Rider> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(StringUtils.isNotBlank(riderName), Rider::getRiderName, riderName); lambdaQueryWrapper.eq(
Rider one = getOne(lambdaQueryWrapper); StringUtils.isNotBlank(riderName),
return R.success("rider", one); Rider::getRiderName,
riderName
);
return getOne(lambdaQueryWrapper);
} }
@Override @Override
public R getRiderById() { public Rider getRiderById() {
String token = request.getHeader("token"); String userId = request.getHeader(TokenConstants.USER_ID);
return getById(userId);
}
String jsonString = stringRedisTemplate.opsForValue().get(token); @Override
Rider rider = JSON.parseObject(jsonString, Rider.class); public void modify(Integer takeKilo, Integer takePrice) {
String userId = request.getHeader(TokenConstants.USER_ID);
return R.success("rider", rider); LambdaUpdateWrapper<Rider> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.setSql("kilometer = kilometer + " + takeKilo +
", left_money = left_money + " + takePrice)
.eq(Rider::getRiderId, userId);
boolean update = update(lambdaUpdateWrapper);
System.out.println(update);
} }
} }