SpringSecurity 理解

SpringSecurity 是基于Spring的安全框架。

1 主要作用

对客户端的http请求进行认证和权限管理。

2 原理(拦截器+过滤器)

如下图所示
在这里插入图片描述

在你添加SpringSecurity的依赖以后 添加如下Security配置类

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http);
        http
                .csrf().disable();    // 关闭csrf

    }
}

再启动项目,打开debug可以看到Debug日志中多了这么一条(意思是创建了一个过滤器链)
在这里插入图片描述这条日志完整如下, 意思是创建了一条过滤器链,可以看到链中的各个过滤器的名字。

2019-06-20 18:07:52.980  INFO 24096 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@29d405e6, org.springframework.security.web.context.SecurityContextPersistenceFilter@4b56b031, org.springframework.security.web.header.HeaderWriterFilter@42e4e589, org.springframework.security.web.authentication.logout.LogoutFilter@d3e3085, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@1283ca23, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@79a201cf, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@4c731956, org.springframework.security.web.session.SessionManagementFilter@6dca31eb, org.springframework.security.web.access.ExceptionTranslationFilter@5d5c41e5]

这个时候可以发送一个http请求,我们再来看一下日志。
在这里插入图片描述可以看到经过了很多默认的过滤器,可以找到这几个过滤器在里面的doFilter 上打断点,可以看到http请求会依次进入断点。

3 自定义过滤器。

自定义一个类 实现Filter 或者继承Filter实现类。在重写doFilter方法。doFilter里面写上我们的过滤逻辑。如下是一个简单的demo

@Component
public class MyFilter implements Filter {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, Idempotent");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        logger.info("自定义过滤器");
    }
}

说到这里,SpringSecuity是怎么运作的,就理解了吧。。不去探讨很底层的东西。从表面上来分析。
添加SpringSecuity依赖以后,写一个配置类继承自WebSeculityConfigAdapter 。类上加上注解@Configuation和@EnableWebSecurity 。。SpringSecuity框架就生效了。

个人理解的整个过滤流程就是上面这些了,有不对的地方还望广大博友指出,不胜感激。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值