首页 > 解决方案 > Spring Security 的身份验证问题,资源 API 上的 JWT

问题描述

我遇到了与身份验证相关的问题,当我尝试从通过 POST(HTTP 状态 201)创建的端点进行 GET 时,我从我的应用程序中注销,我看到以下错误。当我登录时,后端将用户视为anonymousUser,但对于同一文件中的另一个端点工作正常:

2018-10-22 11:33:35.251 调试 2124 --- [XNIO-8 task-23] cclink.aop.logging.LoggingAspect:输入:org.springframework.boot.actuate.audit.AuditEventRepository.add() 带参数[s] = [AuditEvent [时间戳=2018 年 10 月 22 日星期一 11:33:35 PDT,principal=anonymousUser,type=AUTHORIZATION_FAILURE,data={details=org.springframework.security.web.authentication.WebAuthenticationDetails@ffffed504:RemoteIpAddress:127.0 .0.1; SessionId:_5aP-S8x27gGSIjtbkR6EwrWOD9Yybnd-4M3z0ol,类型=org.springframework.security.access.AccessDeniedException,消息=访问被拒绝}]] 2018-10-22 11:33:35.258 调试 2124 --- [XNIO-8 任务 23 ] cclink.aop.logging.LoggingAspect:退出:org.springframework.boot.actuate.audit.AuditEventRepository.add() 结果 = null 2018-10-22 11:33:35.343 WARN 2124 --- [XNIO-8 任务-23] ozpspring.web.advice。

相同的资源(DeliveryResource.java)有一个工作正常的端点,两个端点之间的唯一区别是一个是 /getList(这个工作正常)VS。/getPackingListReport/{number} (这个验证失败)。

API 定义如下所示:

@GetMapping("/getPackingListReport/{number}")
@Secured(AuthoritiesConstants.USER)
@Timed
public ArrayList<WebOrder> getPackingListReportFromDB(@PathVariable("number") long packingListNbr)

对比

@GetMapping("/getList")
@Secured(AuthoritiesConstants.USER)
@Timed
public WebOrder getList(@RequestParam(value = "custid") long custid,@RequestParam(value = "pId") long pId) {

标签: javaspring-bootspring-securityangular5jhipster

解决方案


我发现了问题。

我将以下内容用于“/getList”映射:

    // call dao to insert to DB the list and then call the stored proc
    URI location = ServletUriComponentsBuilder.fromCurrentContextPath().path("/api/rest/getPackingListReport/{packingListNumber}").buildAndExpand(packingListNumber).toUri();

    return ResponseEntity.created(location).build();

在 POST 之后返回创建的资源 URI(HTTP STATUS 201)。当 jHipster 当前默认设置为仅处理和查找“/api”时,该路径包含以“ http://localhost ”开头的错误 URI,或者在我的情况下,我在 Angular5 端添加了“/rest/api/*” .

我通过在收到 URL 后过滤掉“/api/”之前的任何字符来解决此问题。


推荐阅读