spring security本质是一个过滤器链,在项目启动时会进行加载过滤器,当过滤到请求内容时,会执行对应的过滤器并实现对应的功能。
重点三个过滤器:
一、FilterSecurityInterceptor:是一个方法级权限过滤器,基本位于过滤器最底端。
dofilter中获取对象并执行,
invoke中做了各种判断,
InterceptorStatusToken token = super.beforeInvocation(fi);
先执行之前的过滤器,如果之前的过滤器做了放行操作,才往下执行本身的方法。
二、ExceptionTranslationFilter :异常的过滤器,处理认证过程中抛出的异常
ExceptionTranslationFilter 中会判断异常,并对每个异常做不同的处理
三、UsernamePasswordAuthenticationFilter :对/login中post请求做拦截,校验表单中用户名密码。
#UsernamePasswordAuthenticationFilter父类为AbstractAuthenticationProcessingFilter。
dofliter方法中会调用attemptAuthentication方法
attemptAuthentication方法中会进行判断,并返回Authentication对象
postOnly属性默认为true,所以当方法为post方法会获取用户名密码,并进行验证。
获取到返回的authResult对象后调用sessionStrategy.onAuthentication(authResult, request, response);
方法进行session的处理。
最后执行
successfulAuthentication(request, response, chain, authResult);
进行登录成功后的处理。
实际开发过程中会自己定义登录认证的方法。