Skip to content

Commit

Permalink
feat: 增加获取当前登录人订单的接口
Browse files Browse the repository at this point in the history
  • Loading branch information
lichong-a committed Oct 8, 2024
1 parent 109ac54 commit e620721
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.funcode.portal.server.common.core.security.service;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.lang.NonNull;
Expand Down Expand Up @@ -61,7 +62,7 @@ public interface IJwtService {
* @throws IOException 异常
*/
void filterVerifyAccessToken(@NonNull String token, @NonNull HttpServletRequest request,
@NonNull HttpServletResponse response) throws IOException;
@NonNull HttpServletResponse response) throws IOException, ServletException;

/**
* 登录成功后拦截token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.funcode.portal.server.common.core.base.http.response.ResponseResult;
import org.funcode.portal.server.common.core.base.http.response.ResponseStatusEnum;
import org.funcode.portal.server.common.core.config.ApplicationConfig;
import org.funcode.portal.server.common.core.constant.RedisKeyConstant;
import org.funcode.portal.server.common.core.constant.SecurityConstant;
Expand Down Expand Up @@ -82,7 +84,7 @@ public boolean isTokenExpired(String token) {
@Transactional
public void filterVerifyAccessToken(@NonNull String accessToken,
@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response) throws IOException {
@NonNull HttpServletResponse response) throws IOException, ServletException {
SecurityContext context = SecurityContextHolder.createEmptyContext();
try {
String username = this.extractUserName(accessToken);
Expand All @@ -108,7 +110,10 @@ public void filterVerifyAccessToken(@NonNull String accessToken,
if (StringUtils.isBlank(refreshToken)) {
// Redis中不存在说明过期,需要重新登录
SecurityContextHolder.clearContext();
response.sendRedirect(StringUtils.isBlank(applicationConfig.getSecurity().loginPage()) ? "/login" : applicationConfig.getSecurity().loginPage());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setStatus(HttpStatus.UNAUTHORIZED.value());
ObjectMapper mapper = new ObjectMapper();
response.getWriter().write(mapper.writeValueAsString(ResponseResult.fail("请重新登录", ResponseStatusEnum.HTTP_STATUS_401)));
} else if (Objects.equals(accessToken, refreshToken)) {
// 相等情况重新签发token
User userDetails = (User) userDetailsService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.funcode.portal.server.common.core.base.http.response.ResponseResult;
import org.funcode.portal.server.common.domain.base.PageRequestVo;
import org.funcode.portal.server.common.domain.ielts.Order;
import org.funcode.portal.server.module.ielts.order.domain.vo.OrderQueryVo;
import org.funcode.portal.server.module.ielts.order.service.IOrderService;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

/**
* @author 李冲
Expand All @@ -39,4 +37,10 @@ public class OrderController {
public ResponseResult<Page<Order>> pageList(@Valid @RequestBody OrderQueryVo orderQueryVo) {
return ResponseResult.success(orderService.pageList(orderQueryVo));
}

@Operation(summary = "分页查询当前登录人的订单列表")
@GetMapping("/pageList/currentUser")
public ResponseResult<Page<Order>> pageList(@Valid PageRequestVo pageRequestVo) {
return ResponseResult.success(orderService.pageListCurrentUser(pageRequestVo));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

package org.funcode.portal.server.module.ielts.order.service;

import jakarta.validation.Valid;
import org.funcode.portal.server.common.core.base.service.IBaseService;
import org.funcode.portal.server.common.domain.base.PageRequestVo;
import org.funcode.portal.server.common.domain.ielts.Order;
import org.funcode.portal.server.module.ielts.order.domain.vo.OrderQueryVo;
import org.springframework.data.domain.Page;
Expand All @@ -25,4 +27,11 @@ public interface IOrderService extends IBaseService<Order, Long> {
*/
Page<Order> pageList(OrderQueryVo orderQueryVo);

/**
* 分页查询当前用户的订单信息
*
* @param pageRequestVo 分页参数
* @return 分页结果
*/
Page<Order> pageListCurrentUser(@Valid PageRequestVo pageRequestVo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
package org.funcode.portal.server.module.ielts.order.service.impl;

import lombok.RequiredArgsConstructor;
import org.funcode.portal.server.common.core.base.exception.BusinessException;
import org.funcode.portal.server.common.core.base.service.impl.BaseServiceImpl;
import org.funcode.portal.server.common.domain.base.PageRequestVo;
import org.funcode.portal.server.common.domain.ielts.Order;
import org.funcode.portal.server.common.domain.ielts.Order_;
import org.funcode.portal.server.common.domain.security.User;
import org.funcode.portal.server.module.ielts.order.domain.vo.OrderQueryVo;
import org.funcode.portal.server.module.ielts.order.repository.IOrderRepository;
import org.funcode.portal.server.module.ielts.order.service.IOrderService;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -53,4 +57,19 @@ public Page<Order> pageList(OrderQueryVo orderQueryVo) {
orderQueryVo.getPageRequest()
);
}

@Override
public Page<Order> pageListCurrentUser(PageRequestVo pageRequestVo) {
User currentUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (currentUser == null) {
throw new BusinessException("当前用户未登录");
}
return getBaseRepository().findAll(
(Specification<Order>) (root, query, criteriaBuilder) -> query.where(criteriaBuilder.and(
criteriaBuilder.equal(root.get(Order_.user), currentUser)
)
).getRestriction(),
pageRequestVo.getPageRequest()
);
}
}
2 changes: 1 addition & 1 deletion starter/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ application:
# 注销成功跳转地址,默认为"/login?logout"
logout-success-url:
# 登录页地址,默认:/login
login-page: http://127.0.0.1:8080/api/v1/auth/unauthorized
login-page: /pages/login/wechat/index
token:
# 签名密钥
signing-key: portalserver8fas8hage9SHVfsd847GD8475fd8880ejf
Expand Down

0 comments on commit e620721

Please sign in to comment.