Spring boot 如何使用spring security和auth0检索访问令牌?

Spring boot 如何使用spring security和auth0检索访问令牌?,spring-boot,spring-security,auth0,Spring Boot,Spring Security,Auth0,我对JavaSpring引导和安全性是完全陌生的。我正在尝试构建一个Spring引导应用程序,该应用程序支持调用api以及在auth0服务的身份验证过程中接收的访问令牌。我一直遵循本教程来实现身份验证,一切正常。例如,我可以通过将accesstoken打印到控制台来读取纯文本形式的accesstoken,如下所示: @RequestMapping(value = "/callback", method = RequestMethod.GET) protected void getCallback

我对JavaSpring引导和安全性是完全陌生的。我正在尝试构建一个Spring引导应用程序,该应用程序支持调用api以及在auth0服务的身份验证过程中接收的访问令牌。我一直遵循本教程来实现身份验证,一切正常。例如,我可以通过将accesstoken打印到控制台来读取纯文本形式的accesstoken,如下所示:

@RequestMapping(value = "/callback", method = RequestMethod.GET)
protected void getCallback(final HttpServletRequest req, final HttpServletResponse res) throws ServletException, IOException {
  try {
      Tokens tokens = controller.handle(req, res);
      System.out.println(tokens.getAccessToken());
      TokenAuthentication tokenAuth = new TokenAuthentication(JWT.decode(tokens.getIdToken()));
      SecurityContextHolder.getContext().setAuthentication(tokenAuth);
      res.sendRedirect(redirectOnSuccess);
 }
 HttpResponse<String> response = Unirest.get(host + "?" + query)
                    .header("Host", hostReference)
                    .header("Authorization", "Bearer "+insert access token here)
                    .asString(); 
我认为对Api的调用应该如下所示:

@RequestMapping(value = "/callback", method = RequestMethod.GET)
protected void getCallback(final HttpServletRequest req, final HttpServletResponse res) throws ServletException, IOException {
  try {
      Tokens tokens = controller.handle(req, res);
      System.out.println(tokens.getAccessToken());
      TokenAuthentication tokenAuth = new TokenAuthentication(JWT.decode(tokens.getIdToken()));
      SecurityContextHolder.getContext().setAuthentication(tokenAuth);
      res.sendRedirect(redirectOnSuccess);
 }
 HttpResponse<String> response = Unirest.get(host + "?" + query)
                    .header("Host", hostReference)
                    .header("Authorization", "Bearer "+insert access token here)
                    .asString(); 
HttpResponse response=Unirest.get(主机+“?”+查询)
.header(“主机”,主机引用)
.header(“授权”、“承载人”+在此插入访问令牌)
.asString();

所以我的问题是,对于我来说,在运行时获取acces令牌并调用Api的最佳方法是什么。我可以通过将accesstoken存储在静态变量中来执行此操作,但这不是一个好的解决方案。在这种情况下,最好的做法是什么

如果您的应用程序不是无状态的,请将其存储在http会话中——如果您使用的是MVC,我假设不是。除非整个应用程序只有一个用户,否则不能使用静态变量!啊-我知道你已经在安全上下文中设置了它。从那里得到它;它已存储在会话中。@谢谢您的回答!如果我使用SecurityContextHolder,我可以接收主体和凭证等,但似乎无法获得访问令牌。我怎样才能从课程中获得它?我在谷歌上搜索过这个,但似乎找不到一个例子。获取
身份验证
并将其转换为
令牌身份验证
。实际上,您已经存储到SecurityContextHolder.getContext().setAuthentication(令牌身份验证)。您可以在spring boot中的任何位置使用(TokenAuthentication)Securitycontextholder.getcontext().getauthentication(),以取回令牌。如果您的应用程序不是无状态的,请执行任何操作将其存储在http会话中-如果您使用的是MVC,我假设不是。除非整个应用程序只有一个用户,否则不能使用静态变量!啊-我知道你已经在安全上下文中设置了它。从那里得到它;它已存储在会话中。@谢谢您的回答!如果我使用SecurityContextHolder,我可以接收主体和凭证等,但似乎无法获得访问令牌。我怎样才能从课程中获得它?我在谷歌上搜索过这个,但似乎找不到一个例子。获取
身份验证
并将其转换为
令牌身份验证
。实际上,您已经存储到SecurityContextHolder.getContext().setAuthentication(令牌身份验证)。您可以在spring boot中的任何位置使用(TokenAuthentication)Securitycontextholder.getcontext().getauthentication()来取回令牌,做任何您想做的事情