首页 > 解决方案 > 使用 JWT 进行身份验证的 Spring Security OpenID 流

问题描述

我正在尝试使用广泛使用的OpenId Connect流对我的 Web 应用程序进行身份验证。

我了解想要在example1.com上进行身份验证的用户将首先被重定向到身份验证服务器(或OP:“OpenId Provider”)。用户在该服务器上进行身份验证,然后使用签名的 JWT 令牌将他重定向回原始example1.com站点。

技术挑战:

我正在起诉Spring Security作为我的 Web 应用程序的一部分。我采用了一个自定义过滤器将用户重定向到身份验证服务器,然后它使用 valid重定向回我的example1.comJWT (Json Web Token)

我的自定义过滤器:

public class MyCustomFilter extends AbstractAuthenticationProcessingFilter {
   @Override

     public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
        throws AuthenticationException, IOException, ServletException {
         //reading the AWT access token from reqest beahder
         String token = request.getHeader("Authorization");

         if(token == null){
            // redirecting user to authentication server
            String accessCodeUrl = "authentication.com?query string"
            response.sendRedirect(accessCodeUrl);
         } else{
            response.addHeader("Authorization", token);
         }
         //Now how can i inject the above received token to spring security
    }
}

我的春天 xml 配置:

<security:http auto-config="false" entry-point-ref="entryPoint" disable-url-rewriting="true">
    <!-- set disabled to true to remove Cross Site Request Forger support -->
    <security:csrf disabled="true" />
    <security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')" />
    <security:custom-filter ref="${MyCustomFilter }" position="PRE_AUTH_FILTER" />
    <security:access-denied-handler error-page="${security.accessDeniedPage}"/>
    <security:headers>
        <security:frame-options policy="SAMEORIGIN"/>
    </security:headers>        
</security:http>

我的自定义过滤器正在扩展AbstractAuthenticationProcessingFilter。它是否适合我的要求,或者我是否应该考虑扩展任何其他过滤器,例如BasicAuthenticationFilterorOncePerRequestFilterGenericFilterBean

我在这里对使用 1 或 2 个过滤器感到困惑。因为我第一次重定向到授权服务器,但从第二次开始我应该使用身份验证服务器JWT token提供的。

因为我要采用stateless方法。JWT token我每次都需要在example.combrowser之间进行交换。我ExtJs用作前端,在浏览器example.comJWT token之间交换的最佳做法是什么。

根据我的理解,我应该JWT token每次从浏览器发送每个请求。这是否意味着我应该每次都手动注入JWT tokenAjax request或者我们是否有一些方法可以附加到所有传出的Ajax requests.

标签: javaspring-securityjwtopenidspring-security-oauth2

解决方案


推荐阅读