SpringSecurity

182 阅读1分钟

Spring Security对Web资源的保护是靠Filter实现的

AOP--Filter使用,过滤器和拦截器的区别

安全配置

  • 继承WebSecurityConfigurerAdapter
  • Spring Security安全配置提供了用户验证方式,如果需要验证,需自定义实现UserDeatailService
  • @EnableWebSecurity
    • 在非Springboot的Spring Web MVC应用中,该注解@EnableWebSecurity需要开发人员自己引入以启用Web安全。而在基于Springboot的Spring Web MVC应用中,开发人员没有必要再次引用该注解,Springboot的自动配置机制WebSecurityEnablerConfiguration已经引入了该注解
    • Spring Security Config:注解EnableWebSecurity启用Web安全
  • 安全配置包含三部分主要内容:
    • 配置查询用户信息服务
    • 配置安全拦截机制
    • 配置密码编码器 用来定义用户输入密码和数据库里密码比对方式

认证流程

  • 请求/oauth/token的,如果配置支持allowFormAuthenticationForClients的,且url中有client_id和client_secret的会走ClientCredentialsTokenEndpointFilter,加载的UserDetails为ClientDetailsUserDetailsService
  • 请求/oauth/token的,如果没有支持allowFormAuthenticationForClients或者有支持但是url中没有client_id和client_secret的,走basic认证
  • client detail认证(走AuthorizationServerSecurityConfigurer的配置)成功之后,如果是password模式,要走用户账号密码认证
  • 走password的就是用AuthorizationServerEndpointsConfigurer中配置的userDetailsService来进行认证

Spring Security为了防止CSRF限制了除get请求以外的其余请求,可以通过disable解除限制