spring security 源码分析(1)
文章目录
前言:本文会结合相应的代码示例及框架源码。
Spring Security是一个安全框架,侧重于为Java应用程序提供身份验证和授权。 Pivotal 团队的背书,再加上现在主流web开发是围绕着Spring框架的其优势相较于其他安全框架优势不言而喻🙃。废话就不多说了,然我们开始吧。
首先导入相关依赖(tip:作者开发使用的框架是SpringBoot)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
接下来是代码
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private PasswordEncoder passwordEncoder;
/**
* 当前版本是需要配置编码器的
*/
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
//开启身份认证任何请求,任何请求都需要有基本ROLE_USER身份
//用户还可以进行更细粒度的控制。
http.authorizeRequests()
.anyRequest()
.authenticated();
//开启表单登录
http.formLogin();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//使用内存用户信息认证
auth.inMemoryAuthentication().withUser("baozi")
//对我们的明文密码进行加密
.password(passwordEncoder.encode("888"))
.roles("USER")
.and()
.passwordEncoder(passwordEncoder);
}
}
然后启动我们的springboot应用,由于我配置的是anyrequest(),第一次请求没有进行过身边验证所以它会跳转到springboot默认生成的登录页
然后输入刚才我们的用户名("baozi")与密码("888");
简单几步我们就实现了登录功能,那其中发生了什么呢?让我们先看一张流程图(精简版);
其实springsecurity的核心就是一组过滤器。下面列出的三个过滤器是这次demo涉及到的主要过滤器。我将结合部分源码解读。(tip:方法参数及一些不那么重要的代码我省略了)
首先用户请求我们的受保护资源,第一次用户请求时没有带任何信息的,所以用户的请求会直接到最后一个拦截器FilterSecurityInterceptor进行访问权限处理。
// FilterSecurityInterceptor dofilter()中执行invoke()
public void invoke(){
......
// 这里调用父类的方法进行鉴权
InterceptorStatusToken token = super.beforeInvocation(fi);
// 这里实际已经调用了我们的受保护资源
fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
......
}
//让我们展开父类AbstractSecurityInterceptor的beforeInvocation()方法
//tip:该方法内会根据处理结果发布不同的事件,如果你想做日志的话,可以以此为拓展。
protected InterceptorStatusToken beforeInvocation(){
......
try{
//决策是否有权限访问我们的资源,具体在上面SecurityConfig.configure(HttpSecurity http)
//中配置决策管理器决策失败抛出异常
this.accessDecisionManager.decide(authenticated, object, attributes);
}
catch (AccessDeniedException accessDeniedException) {
publishEvent(new AuthorizationFailureEvent(object, attributes, authenticated,
accessDeniedException));
throw accessDeniedException;
}
......
}
异常逐级抛出,最后由ExceptionTranslationFilter捕获。此过滤器将尝试解析异常SpringSecurityException
异常,解析失败直接抛出,最终由springmvc的异常拦截器捕获处理。解析成功将根据不同的访问失败原因返回。
public void doFilter(){
try{
chain.doFilter(request, response);
}catch(){
//io异常继续抛出
}
catch(){
......
//上面省略代码是尝试将异常转换为SpringSecurityException 成功后将进行。
if(ase != null){
......
handleSpringSecurityException(request, response, chain, ase);
}
}
//非SpringSecurityException继续抛出
......
}
//此处将根据异常类型或异常信息返回不同的页面 sendStartAuthentication()负责;
private void handleSpringSecurityException(){
//跳转到登录页。
ex instance AuthenticationException
sendStartAuthentication(......)
// 分为匿名用户 跳转到登录页
// 与 非匿名用户(被服务端主动拒绝的用户) accessDeniedHandler执行handle();
//默认的accessDeniedHandler是返回403页面,我们也可以自己实现accessDeniedHandle接口
//自定义返回
ex instance AuthenticationException
sendStartAuthentication(......) or accessDeniedHandle.handle()
}
然后我们被引导到了登录页(本次不考虑非匿名用户被拒绝的情况),这次我们登录将带上username password;
还有请求的是/login post方法(tip:可自定义),这个请求将被AbstractAuthenticationProcessingFilter::UsernamePasswordAuthenticationFilter过滤器进行处理。
public void doFilter(){
......
//抽象方法交由子类实现,本例为UsernamePasswordAuthenticationFilter
//内部进行相应的用户验证,成功则将Authentication加入安全上下文。
authResult = attemptAuthentication(request, response);
......
//处理失败跳转到失败的页面,可自定义
unsuccessfulAuthentication(request, response, failed);
......
//处理成功转发到原请求handler,可自定义
successfulAuthentication(request, response, chain, authResult);
}
}
到此登录处理流程就完成了。
总结一下
springsecurity将简单易用的api暴露给我们,但的"复杂"的流程封装在其中.可能第一次接触的开发者有点懵,但是归根结底只是一组过滤器,弄明白顺序,这也是日后我们"把玩"这个框架的最大前提。下面我列出了主要的过滤器及顺序,从上到下依次执行。
ChannelProcessingFilter
ConcurrentSessionFilter
SecurityContextPersistenceFilter
LogoutFilter
X509AuthenticationFilter
AbstractPreAuthenticatedProcessingFilter
CasAuthenticationFilter
UsernamePasswordAuthenticationFilter
ConcurrentSessionFilter
OpenIDAuthenticationFilter
org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter
org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter
ConcurrentSessionFilter
DigestAuthenticationFilter
org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationFilter
BasicAuthenticationFilter
RequestCacheAwareFilter
SecurityContextHolderAwareRequestFilter
JaasApiIntegrationFilter
RememberMeAuthenticationFilter
AnonymousAuthenticationFilter
SessionManagementFilter
ExceptionTranslationFilter
FilterSecurityInterceptor
SwitchUserFilter
根据不同的情况加载的过滤器也有所不同,具体可观察控制台,会有相应的日志报告。
下节我将主要讲解与分析被我一笔带过的AbstractAuthenticationProcessingFilter,好了今天的分享就到这里:-)

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论