java - 使用 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
。它是否适合我的要求,或者我是否应该考虑扩展任何其他过滤器,例如BasicAuthenticationFilter
orOncePerRequestFilter
或GenericFilterBean
。
我在这里对使用 1 或 2 个过滤器感到困惑。因为我第一次重定向到授权服务器,但从第二次开始我应该使用身份验证服务器JWT token
提供的。
因为我要采用stateless
方法。JWT token
我每次都需要在example.com和browser之间进行交换。我ExtJs
用作前端,在浏览器和example.comJWT token
之间交换的最佳做法是什么。
根据我的理解,我应该JWT token
每次从浏览器发送每个请求。这是否意味着我应该每次都手动注入JWT token
,Ajax request
或者我们是否有一些方法可以附加到所有传出的Ajax requests
.
解决方案
推荐阅读
- azure-devops - az devops 管道运行本地存储的 yaml 文件?
- python-3.x - Doubts on openpyxl, python3
- sql - 显示使用 T-SQL 查询关闭的工单
- bash - 搞砸了我的 bash_profile 和 bashrc 文件 - 重置为默认值?
- ios - 从 Swift 中的 NsDIctionary 获取价值
- mysql - 使用 Sequelize.js 的多年数据
- c++ - 为什么向量的变化没有反映在二维向量中?
- web-scraping - Rvest:从 csv 中抓取多个 URL
- c - C 只读取第一个结构,不能读取其他输入
- c# - 在 .NET Standard/Core 中序列化 MethodInfo