![](/img/trans.png)
[英]Spring Cloud Zuul: RedirectView redirecting to service and not gateway
[英]Spring Security Cloud: UI service through a ZUUL gateway security setup
我在UI服務中正確設置身份驗證和授權時遇到問題。
我目前有以下設置(全部使用Spring。*和Spring Cloud。*):
- Config Service;
- Registry Service;
- Gateway Service (Zuul);
- Authentication Service (Spring Cloud Security, JWT);
- Company backend service (db <-> rest);
- Ui service;
在后端安全性方面,所有事情都按其應有的方式運行:您通過身份驗證服務通過網關請求帶有憑據的JWT令牌,如果所有匹配項都通過REST呈現。
公司服務知道新令牌,並在新令牌出現時對其進行驗證。
問題出在UI服務上。 我目前正在使用Spring Boot和Thymeleaf並手動構造HttpHeaders,HttpEntity和Cookie對象,而無需在前端部分使用Spring Cloud Security來訪問Web應用程序的某些部分。 這是很多愚蠢的不必要的代碼。 我知道我不明白如何將Spring Cloud安全性集成到我的UI服務中。
這是一種控制器方法的示例(非常難看):
@RequestMapping("/firms")
public String firm (Model model,
HttpServletRequest servletRequest,
HttpServletResponse servletResponse,
HttpSession httpSession) throws IOException {
final String returnPage;
Cookie cookie = authService.findCookie(servletRequest, servletResponse);
HttpHeaders httpHeaders = authService.createJwtAuthHeader(cookie);
HttpEntity requestEntity = new HttpEntity(httpHeaders);
ResponseEntity <UserObject> userObjectResponse = authService.createUserResponseEntity(requestEntity, servletResponse);
authService.setUserSessionDetails(userObjectResponse, httpSession);
if (userObjectResponse != null && userObjectResponse.getBody() != null) {
log.info(CommonMessages.GOT_COOKIE_FROM_AUTH_SERVICE.toString(), cookie.getName());
returnPage = "firm";
} else {
log.error(CommonMessages.NO_COOKIES_FOUND_NO_ACCESS_REDIRECTING.toString());
httpSession.setAttribute("authorized", false);
returnPage = "error";
}
return returnPage;
}
也許有人遇到了類似的問題,並找到了可以用來將Spring Cloud Security正確集成到我的UI服務中的資源或示例?
謝謝!
這是一個方便的示例,您可能需要研究一下: https : //github.com/ddewaele/spring-cloud-security-samples/blob/master/sample1/gateway/src/main/resources/application.yml
這里的主要思想是使用@EnableOAuth2Sso
標記您的服務,以便它可以充當OAuth 2.0 Client 。 這意味着它將執行以下操作:
OAuth2RestTemplate
調用其他微服務成為可能,該OAuth2RestTemplate
會自動將訪問令牌注入到即將發出的請求中。 在這種情況下,您正在調用的微服務必須使用@EnableResourceServer
進行注釋,這意味着它將需要訪問令牌才能處理請求。 有關此主題的更多信息,您可以在此處查看我的另一篇文章。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.