我正在使用Spring Boot构建REST API。我添加了Swagger-ui来处理文档。我在实现客户端身份验证流到swagger时遇到问题,问题是我可以通过基本auth获取swagger-ui以授权提供的client-id(用户名)和client-secret(密码),但是swagger UI不会似乎随后将其应用到最终的访问令牌以进行端点调用。
要确认,我的授权流程; -使用基本身份验证将base64编码的用户名/密码和grant_type = client_credentials发送到/ oauth / token。 Spring返回一个access_token -在以后的API调用中,将提供的access_token用作承载令牌
我认为问题可能是因为我需要在控制器中的每个方法上放置一些内容,以告诉Swagger端点需要身份验证以及哪种类型,但是我找不到有关如何执行此操作的清晰文档,并且我不知道是否需要对我的招摇配置进行任何进一步的更改。
这是控制器的示例(已删除大多数方法以减小尺寸);
layout/index.js
这是我当前的招摇配置;
@Api(value="Currencies", description="Retrieve, create, update and delete currencies", tags = "Currencies")
@RestController
@RequestMapping("/currency")
public class CurrencyController {
private CurrencyService currencyService;
public CurrencyController(@Autowired CurrencyService currencyService) {
this.currencyService = currencyService;
}
/**
* Deletes the requested currency
* @param currencyId the Id of the currency to delete
* @return 200 OK if delete successful
*/
@ApiOperation(value = "Deletes a currency item", response = ResponseEntity.class)
@RequestMapping(value="/{currencyId}", method=RequestMethod.DELETE)
public ResponseEntity<?> deleteCurrency(@PathVariable("currencyId") Long currencyId) {
try {
currencyService.deleteCurrencyById(currencyId);
} catch (EntityNotFoundException e) {
return new ErrorResponse("Unable to delete, currency with Id " + currencyId + " not found!").response(HttpStatus.NOT_FOUND);
}
return new ResponseEntity(HttpStatus.OK);
}
/**
* Returns a single currency by it's Id
* @param currencyId the currency Id to return
* @return the found currency item or an error
*/
@ApiOperation(value = "Returns a currency item", response = CurrencyResponse.class)
@RequestMapping(value="/{currencyId}", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<RestResponse> getCurrency(@PathVariable("currencyId") Long currencyId) {
Currency currency = null;
try {
currency = currencyService.findById(currencyId);
} catch (EntityNotFoundException e) {
return new ErrorResponse("Currency with Id " + currencyId + " could not be found!").response(HttpStatus.NOT_FOUND);
}
return new CurrencyResponse(currency).response(HttpStatus.OK);
}
/**
* Returns a list of all currencies available in the system
* @return Rest response of all currencies
*/
@ApiOperation(value = "Returns a list of all currencies ordered by priority", response = CurrencyListResponse.class)
@RequestMapping(value="", method=RequestMethod.GET, produces="application/json")
public ResponseEntity<RestResponse> getCurrencies() {
return new CurrencyListResponse(currencyService.getAllCurrencies()).response(HttpStatus.OK);
}
}
通过spring进行身份验证目前可以完美地工作,我唯一的问题是使其与Swagger UI一起使用。
答案 0 :(得分:0)
在阅读了此链接的内容(表明这是2.8.0版本存在问题)后,我设法通过将swagger-ui版本2.8.0还原到2.7.0来解决了此问题
答案 1 :(得分:0)
I think that you need to add "Bearer " in front of your key, just like it is shown at this post: Spring Boot & Swagger UI. Set JWT token