将请求从 swagger 端发送到 401 和未经授权的 rest webServices
Sending requests to rest webServices from swagger ends to 401 and unauthorized
我是这个项目的新手,我接到了一个任务,要为项目添加一些服务。
首先,我在其他 Rest 包旁边创建了一个包 NCDM
并创建了我的 class NCDMMemberController
:
package ir.anarestan.ipc.controller.NCDM;
import ir.anarestan.ipc.controller.helper.NCDM.NCDMMemberDTO;
import ir.anarestan.ipc.controller.helper.ResponseDTO;
import ir.anarestan.ipc.service.NCDM.NCDMMemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.mobile.device.Device;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(path = "/ncdmMember")
public class NCDMMemberController {
private final static Logger logger = LoggerFactory.getLogger(NCDMMemberController.class);
@Autowired
private NCDMMemberService ncdmMemberService;
@ResponseBody
@RequestMapping(value = "/getMember", method = RequestMethod.GET, produces = "application/hal+json")
public Object get(@RequestBody NCDMMemberDTO memberDTO, Device device) {
ResponseDTO responseDTO = new ResponseDTO();
try {
responseDTO.setSuccess(true);
responseDTO.setHttpStatus(HttpStatus.OK.value());
responseDTO.setResponseBody(ncdmMemberService.getMemberByMemberIdAndImei(memberDTO));
return responseDTO;
} catch (Exception e) {
logger.info("error occurred!", e);
responseDTO.setSuccess(false);
responseDTO.setErrorMessage(e.getMessage());
responseDTO.setHttpStatus(HttpStatus.EXPECTATION_FAILED.value());
responseDTO.setResponseBody(null);
return responseDTO;
}
}
@ResponseBody
@RequestMapping(value = "/saveMember", method = RequestMethod.POST, produces = "application/hal+json")
public Object save(@RequestBody NCDMMemberDTO member, Device device) {
ResponseDTO responseDTO = new ResponseDTO();
try {
ncdmMemberService.saveMember(member);
responseDTO.setSuccess(true);
responseDTO.setHttpStatus(HttpStatus.OK.value());
responseDTO.setResponseBody(null);
return responseDTO;
} catch (Exception e) {
logger.info("error occurred!", e);
responseDTO.setSuccess(false);
responseDTO.setErrorMessage(e.getMessage());
responseDTO.setHttpStatus(HttpStatus.EXPECTATION_FAILED.value());
responseDTO.setResponseBody(null);
return responseDTO;
}
}
}
但是当我尝试从 swagger 发送请求时,我得到了以下响应:
{
"timestamp": 1579957861338,
"status": 401,
"error": "Unauthorized",
"message": "Unauthorized",
"path": "/ncdmMember/saveMember"
}
当向以前的服务发送请求没有错误时,就会发生这种情况。
有谁知道是什么问题吗?我需要在什么地方介绍一下我的package/class吗?
当我将其中一项书面服务复制到原始 classes 时,一切正常。
如有任何帮助,我们将不胜感激。
这是因为,您在项目中设置了 Spring 安全性。基于身份验证的类型(OAuth2
、Basic Auth
、Digest Auth
、...等),您需要在定义为 SwaggerConfiguration 的 Docket
bean 中添加 securityScheme。
示例:
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).
... // more configurations
.securitySchemes(/*List of Your SecuritySchemes*/)
.build();
然后您可以使用 Swagger 文档的 授权 按钮。下面是 OAuth2
的例子
经过一些研究,当然还有答案给我的线索,我发现我的问题是由 SpringSecurity 引起的,我唯一需要做的就是添加我的基础 URL控制器 class 到允许的 WebSecurityConfig URL,如下所示:
.antMatchers("/ncdmMember/**").permitAll()
我是这个项目的新手,我接到了一个任务,要为项目添加一些服务。
首先,我在其他 Rest 包旁边创建了一个包 NCDM
并创建了我的 class NCDMMemberController
:
package ir.anarestan.ipc.controller.NCDM;
import ir.anarestan.ipc.controller.helper.NCDM.NCDMMemberDTO;
import ir.anarestan.ipc.controller.helper.ResponseDTO;
import ir.anarestan.ipc.service.NCDM.NCDMMemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.mobile.device.Device;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(path = "/ncdmMember")
public class NCDMMemberController {
private final static Logger logger = LoggerFactory.getLogger(NCDMMemberController.class);
@Autowired
private NCDMMemberService ncdmMemberService;
@ResponseBody
@RequestMapping(value = "/getMember", method = RequestMethod.GET, produces = "application/hal+json")
public Object get(@RequestBody NCDMMemberDTO memberDTO, Device device) {
ResponseDTO responseDTO = new ResponseDTO();
try {
responseDTO.setSuccess(true);
responseDTO.setHttpStatus(HttpStatus.OK.value());
responseDTO.setResponseBody(ncdmMemberService.getMemberByMemberIdAndImei(memberDTO));
return responseDTO;
} catch (Exception e) {
logger.info("error occurred!", e);
responseDTO.setSuccess(false);
responseDTO.setErrorMessage(e.getMessage());
responseDTO.setHttpStatus(HttpStatus.EXPECTATION_FAILED.value());
responseDTO.setResponseBody(null);
return responseDTO;
}
}
@ResponseBody
@RequestMapping(value = "/saveMember", method = RequestMethod.POST, produces = "application/hal+json")
public Object save(@RequestBody NCDMMemberDTO member, Device device) {
ResponseDTO responseDTO = new ResponseDTO();
try {
ncdmMemberService.saveMember(member);
responseDTO.setSuccess(true);
responseDTO.setHttpStatus(HttpStatus.OK.value());
responseDTO.setResponseBody(null);
return responseDTO;
} catch (Exception e) {
logger.info("error occurred!", e);
responseDTO.setSuccess(false);
responseDTO.setErrorMessage(e.getMessage());
responseDTO.setHttpStatus(HttpStatus.EXPECTATION_FAILED.value());
responseDTO.setResponseBody(null);
return responseDTO;
}
}
}
但是当我尝试从 swagger 发送请求时,我得到了以下响应:
{
"timestamp": 1579957861338,
"status": 401,
"error": "Unauthorized",
"message": "Unauthorized",
"path": "/ncdmMember/saveMember"
}
当向以前的服务发送请求没有错误时,就会发生这种情况。 有谁知道是什么问题吗?我需要在什么地方介绍一下我的package/class吗?
当我将其中一项书面服务复制到原始 classes 时,一切正常。
如有任何帮助,我们将不胜感激。
这是因为,您在项目中设置了 Spring 安全性。基于身份验证的类型(OAuth2
、Basic Auth
、Digest Auth
、...等),您需要在定义为 SwaggerConfiguration 的 Docket
bean 中添加 securityScheme。
示例:
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).
... // more configurations
.securitySchemes(/*List of Your SecuritySchemes*/)
.build();
然后您可以使用 Swagger 文档的 授权 按钮。下面是 OAuth2
的例子经过一些研究,当然还有答案给我的线索,我发现我的问题是由 SpringSecurity 引起的,我唯一需要做的就是添加我的基础 URL控制器 class 到允许的 WebSecurityConfig URL,如下所示:
.antMatchers("/ncdmMember/**").permitAll()