Spring Security 入门(二)

Spring Security 入门(一)中说到,Spring Security执行流程第一步是容器启动时加载系统资源与权限列表,第二步是WEB容器启动时加载拦截器链,并介绍了自定义拦截器的方法。接下来第三步步就是用户登录。介绍下用户登录的流程:

  1. 获取用户名和密码,并放入一个 UsernamePasswordAuthenticationToken 实例中(Authentication接口的一个实例);
  2. 这个token被传递到一个 AuthenticationManager 实例中进行验证;
  3. 若验证成功, AuthenticationManager返回一个所有字段都被赋值的 Authentication 对象实例,若失败则被AppFilterSecurityInterceptor
    拦截返回登录页面;
  4. 通过调用 SecurityContextHolder.getContext().setAuthentication(…)创建安全上下文,通过返回的验证对象进行传递。
  5. 登陆后,每次访问资源都会被AppFilterSecurityInterceptor这个自定义拦截器拦截,执行doFilter方法,首先调用SecurityMetadataSource实例的getAttributes方法获取被拦截url所需的权限,再调用AccessDecisionManager实例decide方法判断用户是否有权限。若有权限则返回,代表用户可以访问,执行下一个拦截器;若没有权限,则抛出异常,自动跳转权限不足页面(配置文件上配的)。

  下面是网上找的一张Spring Security的执行流程图:

猜你喜欢

转载自www.cnblogs.com/linyukun/p/9862726.html