【发布时间】:2017-11-21 17:46:29
【问题描述】:
我在 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 来访问 webapp 的某些部分。这是很多愚蠢的不必要的代码。我知道我无法理解如何将 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 服务中的资源或示例?
谢谢!
【问题讨论】:
-
@Jeff 为此,我需要将我当前的设置与 UI 服务中的 Spring Cloud Security 集成。我不知道该怎么做。
-
当前端向后端发出请求时,它必须传递令牌,这是在授权标头中完成的。我会试着找到一个例子
-
Thymeleaf 似乎提供了集成:thymeleaf.org/doc/articles/springsecurity.html。但我在那里没有经验..
-
@Jeff 对不起,这是另一个问题。当我拥有 Oauth Spring 云授权微服务时,我需要了解如何设置自定义 Spring Cloud 安全性。
标签: java spring-mvc spring-security spring-cloud