Spring Security内置过滤器详解_springsecurity过滤器(1)

本文深入解析了Spring Security的过滤器工作原理,重点介绍了`doFilter`方法,包括认证流程、异常处理及OAuth2授权过程。通过实例展示了如何处理请求,以及在认证成功与失败时的处理策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注大数据)
img

正文

}

private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
//通过requestMatcher判断request请求是否需要处理
if (!requiresAuthentication(request, response)) {
chain.doFilter(request, response);
return;
}
try {
//获取身份认证结果,并创建认证对象,由子类实现,OAuth2 使用的是OAuth2LoginAuthenticationFilter
Authentication authenticationResult = attemptAuthentication(request, response);
if (authenticationResult == null) {
// return immediately as subclass has indicated that it hasn’t completed
return;
}
//对会话进行处理,防止会话固定攻击(session-fixation详情可网上查询)
this.sessionStrategy.onAuthentication(authenticationResult, request, response);
//认证成功后,是否继续执行后面的过滤器
if (this.continueChainBeforeSuccessfulAuthentication) {
chain.doFilter(request, response);
}
//处理认证成功后的处理逻辑,委托给AuthenticationSuccessHandler,此处为SavedRequestAwareAuthenticationSuccessHandler
successfulAuthentication(request, response, chain, authenticationResult);
}
catch (InternalAuthenticationServiceException failed) {
this.logger.error(“An internal error occurred while trying to authenticate the user.”, failed);
//认证失败处理
unsuccessfulAuthentication(request, response, failed);
}
catch (AuthenticationException ex) {
// Authentication failed
//认证失败处理
unsuccessfulAuthentication(request, response, ex);
}
}


其中attemptAuthentication处的代码量很大,作用是:获取身份认证结果,并创建认证对象,由子类实现,OAuth2 使用的是OAuth2LoginAuthenticationFilter,我们来看看这部分



public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException {
//从授权服务器回调的URL请求中,将请求参数转换为map格式,key=参数名,value=参数值
MultiValueMap<String, String> params = OAuth2AuthorizationResponseUtils.toMultiMap(request.getParameterMap());
//根据code、status、error字段判断是否为返回请求
if (!OAuth2AuthorizationResponseUtils.isAuthorizationResponse(params)) {
OAuth2Error oauth2Error = new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST);
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
}
//返回OAuth2AuthorizationRequest,该对象在OAuth2LoginAuthenticationFilter中构建
//removeAut

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值