繁体   English   中英

Spring Security Cloud:通过ZUUL网关安全设置的UI服务

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM