一、认证的两种方式的介绍

1. 基于Session的认证方式

在之前的单体架构时代,我们认证成功之后都会将信息存入到Session中,然后响应给客户端的是对应的Session中数据的key,客户端会将这个key存储在cookie中,之后都请求都会携带这个cookie中的信息,结构图如下

但是随着技术的更新迭代,我们在项目架构的时候更多的情况下会选择前后端分离或者分布式架构,那么在这种情况下基于session的认证方式就显露了很多的不足,列举几个明显的特点:

  1. cookie存储的内容有限制4k
  2. cookie的有效范围是当前域名下,所以在分布式环境下或者前后端分离的项目中都不适用,及时要用也会很麻烦
  3. 服务端存储了所有认证过的用户信息

2.基于Token的认证方式

相较于Session对需求的兼容,基于Token的方式便是我们在当下项目中处理认证和授权的实现方式的首先了,Token的方式其实就是在用户认证成功后便把用户信息通过加密封装到了Token中,在响应客户端的时候会将Token信息传回给客户端,当下一次请求到来的时候在请求的Http请求的head的Authentication中会携带token

二、SSO和OAuth2.0流程浅析

前面介绍了下认证信息的实现方式,接下来看下我们在分布式环境下会经常碰到的两种解决方案SSO和OAuth2.0

1.SSO

SSO也就是我们经常听到的单点登录,是我们在分布式环境下认证实现的解决方案,具体流程如下

2.OAuth2.0

单点登录解决的分布式系统中统一认证的问题,还有一种情况是一个新的系统用户就需要去注册一个账号,用户管理的账号越多越麻烦,为了解决这个问题,当前系统就期望使用你的其他系统的资料来作为认证的信息,比如 微信,QQ,微博等,这时候就该OAuth2.0,实现流程如下

三、SpringSecurity介绍

1.基础环境准备

    我们要深入的研究SpringSecurity的原理,那么我们首先需要搭建这样的一个环境,在现在的实际开发中使用SpringBoot来构建项目的占比越来越高,所以我们应该要通过SpringBoot来研究SpringSecurity的原理,但是SpringSecurity的封装比较厉害,所以对于源码的分析会不是很友好,所以我们会先基于XML构架的方式来介绍,然后再在SpringBoot项目中来研究。 

1.1基于XML文件的方式构建项目

     我们通过Spring+SpringMVC+SpringSecurity来构建基于XML的项目

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6.  
  7. <groupId>org.example</groupId>
  8. <artifactId>SpringSecurityDemo</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. <packaging>war</packaging>
  11.  
  12. <name>GpSpringSecurityDemo Maven Webapp</name>
  13. <!-- FIXME change it to the project's website -->
  14. <url>http://www.example.com</url>
  15.  
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <maven.compiler.source>1.7</maven.compiler.source>
  19. <maven.compiler.target>1.7</maven.compiler.target>
  20. </properties>
  21.  
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-webmvc</artifactId>
  26. <version>5.2.1.RELEASE</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>javax.servlet</groupId>
  30. <artifactId>servlet-api</artifactId>
  31. <version>2.5</version>
  32. <scope>provided</scope>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.slf4j</groupId>
  36. <artifactId>slf4j-log4j12</artifactId>
  37. <version>1.7.25</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>junit</groupId>
  41. <artifactId>junit</artifactId>
  42. <version>4.11</version>
  43. <scope>test</scope>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.security</groupId>
  47. <artifactId>spring-security-config</artifactId>
  48. <version>5.1.5.RELEASE</version>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.springframework.security</groupId>
  52. <artifactId>spring-security-taglibs</artifactId>
  53. <version>5.1.5.RELEASE</version>
  54. </dependency>
  55. </dependencies>
  56.  
  57. <build>
  58. <finalName>GpSpringSecurityDemo</finalName>
  59. <plugins>
  60. <plugin>
  61. <groupId>org.apache.tomcat.maven</groupId>
  62. <artifactId>tomcat7-maven-plugin</artifactId>
  63. <version>2.2</version>
  64. <configuration>
  65. <port>8080</port> <!-- 访问端口-->
  66. <path>/</path> <!-- 访问路径-->
  67. </configuration>
  68. </plugin>
  69. </plugins>
  70. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  71. <plugins>
  72. <plugin>
  73. <artifactId>maven-clean-plugin</artifactId>
  74. <version>3.1.0</version>
  75. </plugin>
  76. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  77. <plugin>
  78. <artifactId>maven-resources-plugin</artifactId>
  79. <version>3.0.2</version>
  80. </plugin>
  81. <plugin>
  82. <artifactId>maven-compiler-plugin</artifactId>
  83. <version>3.8.0</version>
  84. </plugin>
  85. <plugin>
  86. <artifactId>maven-surefire-plugin</artifactId>
  87. <version>2.22.1</version>
  88. </plugin>
  89. <plugin>
  90. <artifactId>maven-war-plugin</artifactId>
  91. <version>3.2.2</version>
  92. </plugin>
  93. <plugin>
  94. <artifactId>maven-install-plugin</artifactId>
  95. <version>2.5.2</version>
  96. </plugin>
  97. <plugin>
  98. <artifactId>maven-deploy-plugin</artifactId>
  99. <version>2.8.2</version>
  100. </plugin>
  101. </plugins>
  102. </pluginManagement>
  103. </build>
  104. </project>

 WEB.XML

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4.  
  5. <web-app version="2.5" id="WebApp_ID" xmlns="http://java.sun.com/xml/ns/javaee"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  8. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  9. <display-name>Archetype Created Web Application</display-name>
  10.  
  11. <!-- 初始化spring容器 -->
  12. <context-param>
  13. <param-name>contextConfigLocation</param-name>
  14. <param-value>classpath:applicationContext.xml</param-value>
  15. </context-param>
  16. <listener>
  17. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  18. </listener>
  19.  
  20. <!-- post乱码过滤器 -->
  21. <filter>
  22. <filter-name>CharacterEncodingFilter</filter-name>
  23. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  24. <init-param>
  25. <param-name>encoding</param-name>
  26. <param-value>utf-8</param-value>
  27. </init-param>
  28. </filter>
  29. <filter-mapping>
  30. <filter-name>CharacterEncodingFilter</filter-name>
  31. <url-pattern>/*</url-pattern>
  32. </filter-mapping>
  33. <!-- 前端控制器 -->
  34. <servlet>
  35. <servlet-name>dispatcherServletb</servlet-name>
  36. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  37. <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation,
  38. springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
  39. <init-param>
  40. <param-name>contextConfigLocation</param-name>
  41. <param-value>classpath:springmvc.xml</param-value>
  42. </init-param>
  43. <load-on-startup>1</load-on-startup>
  44. </servlet>
  45. <servlet-mapping>
  46. <servlet-name>dispatcherServletb</servlet-name>
  47. <!-- 拦截所有请求jsp除外 -->
  48. <url-pattern>/</url-pattern>
  49. </servlet-mapping>
  50.  
  51. <!-- 配置过滤器链 springSecurityFilterChain名称固定-->
  52. <filter>
  53. <filter-name>springSecurityFilterChain</filter-name>
  54. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  55. </filter>
  56. <filter-mapping>
  57. <filter-name>springSecurityFilterChain</filter-name>
  58. <url-pattern>/*</url-pattern>
  59. </filter-mapping>
  60.  
  61. </web-app>

login.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
  2. <html>
  3. <head>
  4. <title>Title</title>
  5. </head>
  6. <body>
  7. <h1>登录管理</h1>
  8.  
  9. <form action="/login" method="post">
  10. 账号:<input type="text" name="username"><br>
  11. 密码:<input type="password" name="password"><br>
  12. <input type="submit" value="登录"><br>
  13. </form>
  14. </body>
  15. </html>

springsecurity.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:security="http://www.springframework.org/schema/security"
  7. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  12. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
  13. http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd">
  14.  
  15. <!--
  16. auto-config="true" 表示自动加载SpringSecurity的配置文件
  17. use-expressions="true" 使用Spring的EL表达式
  18. -->
  19. <security:http auto-config="true" use-expressions="true">
  20.  
  21. <security:intercept-url pattern="/login.jsp" access="permitAll()"></security:intercept-url>
  22. <!--<security:intercept-url pattern="/login.do" access="permitAll()"></security:intercept-url>-->
  23.  
  24. <!--
  25. 拦截资源
  26. pattern="/**" 拦截所有的资源
  27. access="hasAnyRole('role1')" 表示只有role1这个角色可以访问资源
  28. -->
  29. <security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER')"></security:intercept-url>
  30.  
  31. <!--
  32. 配置认证信息
  33. login-page="/login.jsp" 自定义的登录页面
  34. login-processing-url="/login" security中处理登录的请求
  35. default-target-url="/home.jsp" 默认的跳转地址
  36. authentication-failure-url="/failure.jsp" 登录失败的跳转地址
  37.  
  38. <security:form-login
  39. login-page="/login.jsp"
  40. login-processing-url="/login"
  41. default-target-url="/home.jsp"
  42. authentication-failure-url="/failure.jsp"
  43. />-->
  44. <!-- 配置退出的登录信息
  45. <security:logout logout-url="/logout"
  46. logout-success-url="/login.jsp" />
  47. <security:csrf disabled="true"/>-->
  48. </security:http>
  49.  
  50. <!-- 设置认证用户来源 noop:SpringSecurity中默认 密码验证是要加密的 noop表示不加密 -->
  51. <security:authentication-manager>
  52. <security:authentication-provider>
  53. <security:user-service>
  54. <security:user name="zhang" password="{noop}123" authorities="ROLE_USER"></security:user>
  55. <security:user name="lisi" password="{noop}123" authorities="ROLE_ADMIN"></security:user>
  56. </security:user-service>
  57. </security:authentication-provider>
  58. </security:authentication-manager>
  59.  
  60. </beans>

springmvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
  10.  
  11. <!-- 配置扫描路径-->
  12. <context:component-scan base-package="com.ghy.security.controller"
  13. use-default-filters="false">
  14. <context:include-filter type="annotation"
  15. expression="org.springframework.stereotype.Controller" />
  16. </context:component-scan>
  17. </beans>
  1. log4j.properties
  1. log4j.rootCategory=debug, stdout
  2.  
  3. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  4. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  5. log4j.appender.stdout.layout.ConversionPattern=[GP] %p [%t] %C.%M(%L) | %m%n

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  11. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
  12.  
  13. <import resource="classpath:springsecurity.xml"/>
  14.  
  15. </beans>

  

 2.原理分析

      首先启动我们构建的项目然后访问 http://localhost:8080/index.html 因为访问的是非匿名访问的页面,所以会直接调整到系统默认的登陆界面,那么如果我们要使用自定义的登陆界面应该怎么去实现?还有我们要探究下进入默认登陆界面前整个SpringSecurity到底做了什么事情?所以我们可以从下面这三方面来深入的研究
  1. 系统启动springSecurity做了啥
  2. 页面是怎么出来的
  3. 认证流程是怎么实现的
想要搞清楚上面问题,首先我们要请求一个web请求的基本流程如下

 

请求从客户端发起(例如浏览器),然后穿过层层 Filter,最终来到 Servlet 上,被 Servlet 所处理。那么权限管理框架在设计的时候肯定也是基于Filter来实现的。那么SpringSecurity的处理结构应该是这样的

有了这个基本的认知后我们就可以来具体的分析SpringSecurity的核心原理了,为了便于大家的理解,我们先从客户端发送请求开始到显示登陆界面这条流程开始来分析。

  2.1.第一次请求的流程梳理

       基于XML的方式分析DelegatingFilterProxy

 当我们在浏览器地址栏发送http://localhost:8080/index.html的时候在服务端会被 web.xml 中配置的DelegatingFilterProxy这个过滤器拦截

 

  1. <!-- 配置过滤器链 springSecurityFilterChain名称固定-->
  2. <filter>
  3. <filter-name>springSecurityFilterChain</filter-name>
  4. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>springSecurityFilterChain</filter-name>
  8. <url-pattern>/*</url-pattern>
  9. </filter-mapping>

 所以请求的流程图应该是这样的

     但是要注意 DelegatingFilterProxy 并不是SpringSecurity提供给我们的哦,而是Spring框架之前就提供的有的,DelegatingFilterProxy就是一个对于servlet filter的代理,用这个类的好处主要是通过Spring容器来管理servlet filter的生命周期,还有就是如果filter中需要一些Spring容器的实例,可以通过spring直接注入,另外读取一些配置文件这些便利的操作都可以通过Spring来配置实现。DelegatingFilterProxy继承自GenericFilterBean,在Servlet容器启动的时候回执行GenericFilterBean的init方法,

 

  1. public final void init(FilterConfig filterConfig) throws ServletException {
  2. Assert.notNull(filterConfig, "FilterConfig must not be null");
  3. this.filterConfig = filterConfig;
  4. PropertyValues pvs = new GenericFilterBean.FilterConfigPropertyValues(filterConfig, this.requiredProperties);
  5. if (!pvs.isEmpty()) {
  6. try {
  7. BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
  8. ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
  9. Environment env = this.environment;
  10. if (env == null) {
  11. env = new StandardServletEnvironment();
  12. }
  13.  
  14. bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, (PropertyResolver)env));
  15. this.initBeanWrapper(bw);
  16. bw.setPropertyValues(pvs, true);
  17. } catch (BeansException var6) {
  18. String msg = "Failed to set bean properties on filter '" + filterConfig.getFilterName() + "': " + var6.getMessage();
  19. this.logger.error(msg, var6);
  20. throw new NestedServletException(msg, var6);
  21. }
  22. }
                     // 另外在init方法中调用了initFilterBean()方法,该方法是GenericFilterBean类是特地留给子类扩展用的 
  1. this.initFilterBean();
  2. if (this.logger.isDebugEnabled()) {
  3. this.logger.debug("Filter '" + filterConfig.getFilterName() + "' configured for use");
  4. }
  5.  
  6. }

  

DelegatingFilterProxy的initFilterBean方法
  1. protected void initFilterBean() throws ServletException {
  2. synchronized(this.delegateMonitor) {
  3. if (this.delegate == null) {
  4. if (this.targetBeanName == null) {
                                         // 获取的是在web.xml文件中设置的DelegatingFilterProxy过滤器对应的
                                          // filterName【springSecurityFilterChain】 
  1. this.targetBeanName = this.getFilterName();
  2. }
                                     // 获取IoC容器对象 
  1. WebApplicationContext wac = this.findWebApplicationContext();
  2. if (wac != null) {
                                            // 获取委托处理请求的过滤器,在此处获取的是 FilterChainProxy 
  1. this.delegate = this.initDelegate(wac);
  2. }
  3. }
  4.  
  5. }
  6. }

上面的方法首先获取在web.xml中配置的FilterName的值也就是 springSecurityFilterChain,然后再获取Spring的IoC容器对象,如果容器对象不为空,然后执行this.initDelegate(wac);方法

  1. protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
                    // 获取的值为 springSecurityFilterChain 
  1. String targetBeanName = this.getTargetBeanName();
  2. Assert.state(targetBeanName != null, "No target bean name set");
                    // 从SpringIoC容器中根据 springSecurityFilterChain 这个名称获取bean对象
                   // 所以在web.xml中我们配置的FilterName必须得是springSecurityFilterChain否则的话是获取不到的
  1. Filter delegate = (Filter)wac.getBean(targetBeanName, Filter.class);
  2. if (this.isTargetFilterLifecycle()) {
                   // 如果有设置targetFilterLifecycle为true就会执行委托过滤器的init方法 
  1. delegate.init(this.getFilterConfig());
  2. }
  3.  
  4. return delegate;
  5. }

  

通过上面这个流程init()初始化过程就结束了,通过上面源码的解析我们能够发现DelegatingFilterProxy这个过滤器在初始的时候从Spring容器中获取了 FilterChainProxy 这个过滤器链的代理对象,并且把这个对象保存在了DelegatingFilterProxy的delegate属性中。那么当请求到来的时候会执行DelegatingFilterProxy的doFilter方法,那么我们就可以来看下这个方法里面又执行了什么

  1. public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
  2. Filter delegateToUse = this.delegate;
                   // if中的代码已经在初始化阶段完成了 
  1. if (delegateToUse == null) {
  2. synchronized(this.delegateMonitor) {
  3. delegateToUse = this.delegate;
  4. if (delegateToUse == null) {
  5. WebApplicationContext wac = this.findWebApplicationContext();
  6. if (wac == null) {
  7. throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?");
  8. }
  9.  
  10. delegateToUse = this.initDelegate(wac);
  11. }
  12.  
  13. this.delegate = delegateToUse;
  14. }
  15. }
                 // 核心流程就是该代码 
  1. this.invokeDelegate(delegateToUse, request, response, filterChain);
  2. }

  invokeDelegate是doFilter中的核心代码,字面含义就是调用委托对象。从具体源码来看也确实如此

  1. protected void invokeDelegate(Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
             // 调用FilterChainProxy的doFilter方法
  1. delegate.doFilter(request, response, filterChain);
  2. }

  通过上面的代码分析我们发现,当一个请求到来的时候先通过DelegatingFilterProxy来拦截,但是DelegatingFilterProxy不处理具体的逻辑,而是将具体的处理操作交给了delegate过滤器来处理也就是FilterChainProxy来处理。

     FilterChainProxy分析 
       通过上面的分析我们也能发现FilterChainProxy在整个请求过程中的位置了,如下
     

至于FilterChainProxy怎么来的会在介绍系统初始化的时候会介绍到这块儿的内容的。对于FilterChainProxy怎么处理请求的,根据上面的内容我们知道我们可以直接看doFilter方法即可

  1. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  2. boolean clearContext = request.getAttribute(FILTER_APPLIED) == null;
  3. if (clearContext) {
  4. try {
  5. request.setAttribute(FILTER_APPLIED, Boolean.TRUE);
  6. this.doFilterInternal(request, response, chain);
  7. } finally {
  8. SecurityContextHolder.clearContext();
  9. request.removeAttribute(FILTER_APPLIED);
  10. }
  11. } else {
    //跟进
  12. this.doFilterInternal(request, response, chain);
  13. }
  14.  
  15. }

 

  1. private void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
                      // 对request对象做了一次防火墙检查,校验提交的方式是否合法【post,get等】
                     // 校验请求的地址是否在黑名单中 
  1. FirewalledRequest fwRequest = this.firewall.getFirewalledRequest((HttpServletRequest)request);
  2. HttpServletResponse fwResponse = this.firewall.getFirewalledResponse((HttpServletResponse)response);
                   // 获取处理这个请求的过滤器链中的所有的过滤器
  1. List<Filter> filters = this.getFilters((HttpServletRequest)fwRequest);
  2. if (filters != null && filters.size() != 0) {
                             // 如果有过滤器则构建一个虚拟的过滤器链路,然后执行 
  1. FilterChainProxy.VirtualFilterChain vfc = new FilterChainProxy.VirtualFilterChain(fwRequest, chain, filters);
  2. vfc.doFilter(fwRequest, fwResponse);
  3. } else {
  4. if (logger.isDebugEnabled()) {
  5. logger.debug(UrlUtils.buildRequestUrl(fwRequest) + (filters == null ? " has no matching filters" : " has an empty filter list"));
  6. }
  7.  
  8. fwRequest.reset();
                           // 如果没有要执行的过滤器 则通过外部的过滤器链执行下一个servlet 过滤器来处理 
  1. chain.doFilter(fwRequest, fwResponse);
  2. }
  3. }

 上面方法中的核心代码第一个是this.getFilters((HttpServletRequest)fwRequest);在这个方法要注意一个概念就是在SpringSecurity中可以存在多个过滤器链,而每个过滤器链又可以包含多个过滤器

 

 

那么在第一次访问到来的时候会选择哪个过滤器链然后又包含哪些具体的过滤器呢?

可以看到此处有15个过滤器。 通过vfc.doFilter(fwRequest, fwResponse);我们可以发现是被这15过滤器按照固定先后顺序来执行的。
  1. public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
  2. if (this.currentPosition == this.size) {
  3. if (FilterChainProxy.logger.isDebugEnabled()) {
  4. FilterChainProxy.logger.debug(UrlUtils.buildRequestUrl(this.firewalledRequest) + " reached end of additional filter chain; proceeding with original chain");
  5. }
  6.  
  7. this.firewalledRequest.reset();
  8. this.originalChain.doFilter(request, response);
  9. } else {
                                    // size=15 currentPosition会从0~15 一个个的取出过滤器 
  1. ++this.currentPosition;
  2. Filter nextFilter = (Filter)this.additionalFilters.get(this.currentPosition - 1);
  3. if (FilterChainProxy.logger.isDebugEnabled()) {
  4. FilterChainProxy.logger.debug(UrlUtils.buildRequestUrl(this.firewalledRequest) + " at position " + this.currentPosition + " of " + this.size + " in additional filter chain; firing Filter: '" + nextFilter.getClass().getSimpleName() + "'");
  5. }
                            // 调用取出来的过滤器的doFilter方法,此处有用到设计模式中的责任链模式 
  1. nextFilter.doFilter(request, response, this);
  2. }
  3.  
  4. }
  5. }

 

ExceptionTranslationFilter 

 在整个过滤器链中,ExceptionTranslationFilter是倒数第二个执行的过滤器,它的作用是通过catch处理下一个Filter【也就是FilterSecurityInterceptor】或应用逻辑产生的异常

  1. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
  2. HttpServletRequest request = (HttpServletRequest)req;
  3. HttpServletResponse response = (HttpServletResponse)res;
  4.  
  5. try {
                             // 进入下一个过滤器 FilterSecurityInterceptor中 
  1. chain.doFilter(request, response);
  2. this.logger.debug("Chain processed normally");
  3. } catch (IOException var9) {
  4. throw var9;
  5. } catch (Exception var10) {
                             // FilterSecurityInterceptor有显示的抛出异常 就处理 
  1. Throwable[] causeChain = this.throwableAnalyzer.determineCauseChain(var10);
  2. RuntimeException ase = (AuthenticationException)this.throwableAnalyzer.getFirstThrowableOfType(AuthenticationException.class, causeChain);
  3. if (ase == null) {
  4. ase = (AccessDeniedException)this.throwableAnalyzer.getFirstThrowableOfType(AccessDeniedException.class, causeChain);
  5. }
  6.  
  7. if (ase == null) {
  8. if (var10 instanceof ServletException) {
  9. throw (ServletException)var10;
  10. }
  11.  
  12. if (var10 instanceof RuntimeException) {
  13. throw (RuntimeException)var10;
  14. }
  15.  
  16. throw new RuntimeException(var10);
  17. }
  18.  
  19. if (response.isCommitted()) {
  20. throw new ServletException("Unable to handle the Spring Security Exception because the response is already committed.", var10);
  21. }
  22.  
  23. this.handleSpringSecurityException(request, response, chain, (RuntimeException)ase);
  24. }
  25.  
  26. }

  

handleSpringSecurityException返回就是处理下一个Filter抛出的异常
  1. private void handleSpringSecurityException(HttpServletRequest request, HttpServletResponse response, FilterChain chain, RuntimeException exception) throws IOException, ServletException {
  2. if (exception instanceof AuthenticationException) {
  3. this.logger.debug("Authentication exception occurred; redirecting to authentication entry point", exception);
  4. this.sendStartAuthentication(request, response, chain, (AuthenticationException)exception);
  5. } else if (exception instanceof AccessDeniedException) {
  6. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  7. if (!this.authenticationTrustResolver.isAnonymous(authentication) && !this.authenticationTrustResolver.isRememberMe(authentication)) {
  8. this.logger.debug("Access is denied (user is not anonymous); delegating to AccessDeniedHandler", exception);
  9. this.accessDeniedHandler.handle(request, response, (AccessDeniedException)exception);
  10. } else {
  11. this.logger.debug("Access is denied (user is " + (this.authenticationTrustResolver.isAnonymous(authentication) ? "anonymous" : "not fully authenticated") + "); redirecting to authentication entry point", exception);
  12. this.sendStartAuthentication(request, response, chain, new InsufficientAuthenticationException(this.messages.getMessage("ExceptionTranslationFilter.insufficientAuthentication", "Full authentication is required to access this resource")));
  13. }
  14. }
  15.  
  16. }
关键方法:sendStartAuthentication 

  

  1. protected void sendStartAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, AuthenticationException reason) throws ServletException, IOException {
  2. SecurityContextHolder.getContext().setAuthentication((Authentication)null);
                    // 保存本次请求的request信息,当认证成功会跳转到这个请求的地址 
  1. this.requestCache.saveRequest(request, response);
  2. this.logger.debug("Calling Authentication entry point.");
  3. this.authenticationEntryPoint.commence(request, response, reason);
  4. }
AuthenticationEntryPoint【认证入口点】中的commence方法 

  

  1. public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
  2. String redirectUrl = null;
  3. if (this.useForward) {
  4. if (this.forceHttps && "http".equals(request.getScheme())) {
  5. redirectUrl = this.buildHttpsRedirectUrlForRequest(request);
  6. }
  7.  
  8. if (redirectUrl == null) {
  9. String loginForm = this.determineUrlToUseForThisRequest(request, response, authException);
  10. if (logger.isDebugEnabled()) {
  11. logger.debug("Server side forward to: " + loginForm);
  12. }
  13.  
  14. RequestDispatcher dispatcher = request.getRequestDispatcher(loginForm);
  15. dispatcher.forward(request, response);
  16. return;
  17. }
  18. } else {
                           // 获取重定向的地址 默认是 http://localhost:8080/login 
  1. redirectUrl = this.buildRedirectUrlToLoginPage(request, response, authException);
  2. }
                        // 获取重定向的策略 然后发送重定向的地址
  1. this.redirectStrategy.sendRedirect(request, response, redirectUrl);
  2. }

 sendRedirect

  1. public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
  2. String redirectUrl = this.calculateRedirectUrl(request.getContextPath(), url);
  3. redirectUrl = response.encodeRedirectURL(redirectUrl);
  4. if (this.logger.isDebugEnabled()) {
  5. this.logger.debug("Redirecting to '" + redirectUrl + "'");
  6. }
                     // 重定向跳转 如果是自定义的登陆界面 那么此处就直接跳转到自定义的认证界面了。
  1. response.sendRedirect(redirectUrl);
  2. }

  此处重定向的地址是 .../login 该请求会被DefaultLoginPageGeneratingFilter过滤器拦截,具体看下面对DefaultLoginPageGeneratingFilter的介绍

FilterSecurityInterceptor
   FilterSecurityInterceptor是SpringSecurity过滤器链中的最后一个过滤器,作用是先判断是否身份验证,然后在做权限的验证。第一次访问的时候处理的在doFilter中的方法的关键代码如下:
  1. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  2. FilterInvocation fi = new FilterInvocation(request, response, chain);
  3. this.invoke(fi);
  4. }

  decide方法在做投票选举,第一次的时候回抛出AccessDeniedException异常,而抛出的异常会被ExceptionTranslationFilter中的catch语句块捕获,进而执行handleSpringSecurityException方法。

DefaultLoginPageGeneratingFilter 
     DefaultLoginPageGeneratingFilter该过滤器的作用从字面含义也能够看出来就是生成默认登陆界面的一个过滤器

  1. private void init(UsernamePasswordAuthenticationFilter authFilter, AbstractAuthenticationProcessingFilter openIDFilter) {
                  // 初始化方法中定义的有要拦截的 登陆页面的Url 
  1. this.loginPageUrl = "/login";
  2. this.logoutSuccessUrl = "/login?logout";
  3. this.failureUrl = "/login?error";
  4. if (authFilter != null) {
  5. this.formLoginEnabled = true;
  6. this.usernameParameter = authFilter.getUsernameParameter();
  7. this.passwordParameter = authFilter.getPasswordParameter();
  8. if (authFilter.getRememberMeServices() instanceof AbstractRememberMeServices) {
  9. this.rememberMeParameter = ((AbstractRememberMeServices)authFilter.getRememberMeServices()).getParameter();
  10. }
  11. }
  12.  
  13. if (openIDFilter != null) {
  14. this.openIdEnabled = true;
  15. this.openIDusernameParameter = "openid_identifier";
  16. if (openIDFilter.getRememberMeServices() instanceof AbstractRememberMeServices) {
  17. this.openIDrememberMeParameter = ((AbstractRememberMeServices)openIDFilter.getRememberMeServices()).getParameter();
  18. }
  19. }
  20.  
  21. }

  然后在doFilter方法中有判断请求的地址是否是'/login',如果是的话就拦截,否则就放过

  1. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
  2. HttpServletRequest request = (HttpServletRequest)req;
  3. HttpServletResponse response = (HttpServletResponse)res;
  4. boolean loginError = this.isErrorPage(request);
  5. boolean logoutSuccess = this.isLogoutSuccess(request);
  6. if (!this.isLoginUrlRequest(request) && !loginError && !logoutSuccess) {
                           // 不是默认进入登陆界面的请求放过 
  1. chain.doFilter(request, response);
  2. } else {
                              // 生成默认页面的html页面 
  1. String loginPageHtml = this.generateLoginPageHtml(request, loginError, logoutSuccess);
  2. response.setContentType("text/html;charset=UTF-8");
                              // 响应请求,中断了过滤器链的执行
  1. response.setContentLength(loginPageHtml.getBytes(StandardCharsets.UTF_8).length);
  2. response.getWriter().write(loginPageHtml);
  3. }
  4. }

  

3.基于SpringBoot方式的分析

基于配置文件的方式我们清楚了DelegatingFilterProxy是如何处理的,下面分析下在SpringBoot中也是如何处理的

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-web</artifactId>
  8. </dependency>

 

基于SpringBoot的自动装配我们知道整合第三方框架要加载的信息是在spring.factories中,如下

其中有三个我们需要关注的会自动装配的配置类

  1. org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguratio n,\
    org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoCo nfiguration,\
    org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfig uration,\

  这三个中和DelegatingFilterProxy有关系的是第三个 SecurityFilterAutoConfiguration 所以我们就直接先来看这个

  1. @Bean
  2. @ConditionalOnBean(name = DEFAULT_FILTER_NAME)
  3. public DelegatingFilterProxyRegistrationBean securityFilterChainRegistration(
  4. SecurityProperties securityProperties) {
    //这段代码和前面讲的拦截器比较类似,我们可以点进去看下做了啥
  5. DelegatingFilterProxyRegistrationBean registration = new DelegatingFilterProxyRegistrationBean(
  6. DEFAULT_FILTER_NAME);
  7. registration.setOrder(securityProperties.getFilter().getOrder());
  8. registration.setDispatcherTypes(getDispatcherTypes(securityProperties));
  9. return registration;
  10. }

  进去之后看下结构,我们发现有个ServletContextInitializer,这个是上下文初始化的接口,我们进入看下

进入发现只有一个入口onStartup,先他的RegistraitonBean实现

  1. @FunctionalInterface
  2. public interface ServletContextInitializer {
  3. void onStartup(ServletContext servletContext) throws ServletException;
  4. }

 

  1. public final void onStartup(ServletContext servletContext) throws ServletException {
  2. String description = this.getDescription();
  3. if (!this.isEnabled()) {
  4. logger.info(StringUtils.capitalize(description) + " was not registered (disabled)");
  5. } else {
                        // 进入register方法中查看 
  1. this.register(description, servletContext);
  2. }
  3. }
进入DynamicRegistrationBean中查看(不会找实现的可以看我上面的类关系图)
  1. protected final void register(String description, ServletContext servletContext) {
                    // 此处会添加对应的过滤器,也就是DelegatingFilterProxy  
  1. D registration = this.addRegistration(description, servletContext);
  2. if (registration == null) {
  3. logger.info(StringUtils.capitalize(description) + " was not registered (possibly already registered?)");
  4. } else {
  5. this.configure(registration);
  6. }
  7. }
先查看下addRegistration方法 进入的是AbstractFilterRegistrationBean它中的方法 
  1. protected Dynamic addRegistration(String description, ServletContext servletContext) {
  2. Filter filter = this.getFilter();
                     // 改过滤器会被添加到servletContext容器中,那么当有满足添加的请求到来的时候就会触发该过滤器拦截
  1. return servletContext.addFilter(this.getOrDeduceName(filter), filter);
  2. }

 然后再看getFilter方法,可以看到真实的创建了一个DelegatingFilterProxy对象并且指定的名声是springSecurityFilterChain也就是DelegatingFilterProxy代理的Spring容器的Filter的名称

  1. public DelegatingFilterProxy getFilter() {
  1. //这个过滤器名称就是外面写死的那个
    //这new的玩意其实就和前面XML文件配置的springSecurityFilterChain过滤器一样
  1. return new DelegatingFilterProxy(this.targetBeanName, this.getWebApplicationContext()) {

  2. protected void initFilterBean() throws ServletException {
  3. }
  4. };
  5. }

 找到过滤器后我们向上返回一级,servletContext.addFilter(this.getOrDeduceName(filter), filter);把过滤器加载到上下文中去,然后再向上返一级看下this.configure(registration)方法,注意这个方法我们要进入AbstractFilterRegistrationBean中查看

 

  1. protected void configure(Dynamic registration) {
  2. super.configure(registration);
  3. EnumSet<DispatcherType> dispatcherTypes = this.dispatcherTypes;
  4. if (dispatcherTypes == null) {
  5. T filter = this.getFilter();
  6. if (ClassUtils.isPresent("org.springframework.web.filter.OncePerRequestFilter", filter.getClass().getClassLoader()) && filter instanceof OncePerRequestFilter) {
  7. dispatcherTypes = EnumSet.allOf(DispatcherType.class);
  8. } else {
  9. dispatcherTypes = EnumSet.of(DispatcherType.REQUEST);
  10. }
  11. }
  12.  
  13. Set<String> servletNames = new LinkedHashSet();
  14. Iterator var4 = this.servletRegistrationBeans.iterator();
  15.  
  16. while(var4.hasNext()) {
  17. ServletRegistrationBean<?> servletRegistrationBean = (ServletRegistrationBean)var4.next();
  18. servletNames.add(servletRegistrationBean.getServletName());
  19. }
  20.  
  21. servletNames.addAll(this.servletNames);
  22. if (servletNames.isEmpty() && this.urlPatterns.isEmpty()) {
                           // 如果没有指定对应的servletNames和urlPartterns的话就使用默认的名称和拦截地址 /* 
  1. registration.addMappingForUrlPatterns(dispatcherTypes, this.matchAfter, DEFAULT_URL_MAPPINGS);
  2. } else {
  3. if (!servletNames.isEmpty()) {
  4. registration.addMappingForServletNames(dispatcherTypes, this.matchAfter, StringUtils.toStringArray(servletNames));
  5. }
  6.  
  7. if (!this.urlPatterns.isEmpty()) {
  8. registration.addMappingForUrlPatterns(dispatcherTypes, this.matchAfter, StringUtils.toStringArray(this.urlPatterns));
  9. }
  10. }
  11.  
  12. }

 

DEFAULT_URL_MAPPINGS默认的是 /*

 

至此我们看到了在SpringBoot中是通过DelegatingFilterProxyRegistrationBean 帮我们创建了一个DelegatingFilterProxy过滤器并且指定了拦截的地址,默认是 /* ,之后的逻辑就和前面介绍的XML中的就是一样的了,请求会进入FilterChainProxy中开始处理

4.SpringSecurity初始化到底经历了什么

 通过对 第一次请求的流程梳理 会看到一个FilterChainProxy 至于他是啥时候创建的及默认的过滤器链和过滤器是怎么来的,下面就看下SpringSecurity初始化的时候到底做了哪些事情,基于XML的初始化阶段其实就是各种解析器对标签的解析,过程比较繁琐这里我们就不去分析了,我们直接在SpringBoot项目中来分析,在SpringBoot项目中分析SpringSecurity的初始化过程显然我们需要从 spring.factories 中的SecurityAutoConfiguration开始  

  1. @Configuration(proxyBeanMethods = false)
    @ConditionalOnClass(DefaultAuthenticationEventPublisher.class)
    @EnableConfigurationProperties(SecurityProperties.class)
    @Import({ SpringBootWebSecurityConfiguration.class, WebSecurityEnablerConfiguration.class,
    SecurityDataConfiguration.class })
    public class SecurityAutoConfiguration {
        // 定义了一个默认的事件发布器 

  1. @Bean
    @ConditionalOnMissingBean(AuthenticationEventPublisher.class)
    public DefaultAuthenticationEventPublisher authenticationEventPublisher(ApplicationEventPublisher publisher) {
    return new DefaultAuthenticationEventPublisher(publisher);
    }

    }
该类引入了 SpringBootWebSecurityConfiguration WebSecurityEnablerConfiguration  SecurityDataConfiguration 这三个类
  1. @Configuration(
  2. proxyBeanMethods = false
  3. )
  4. @ConditionalOnClass({WebSecurityConfigurerAdapter.class})
  5. @ConditionalOnMissingBean({WebSecurityConfigurerAdapter.class})
  6. @ConditionalOnWebApplication(
  7. type = Type.SERVLET
  8. )
  9. public class SpringBootWebSecurityConfiguration {
  10. public SpringBootWebSecurityConfiguration() {
  11. }
  12.  
  13. @Configuration(
  14. proxyBeanMethods = false
  15. )
  16. @Order(2147483642)
  17. static class DefaultConfigurerAdapter extends WebSecurityConfigurerAdapter {
  18. DefaultConfigurerAdapter() {
  19. }
  20. }
  21. }
这个配置的作用是在如果开发者没有自定义 WebSecurityConfigurerAdapter 的话,这里提供一个默认的实现。@ConditionalOnMissingBean({WebSecurityConfigurerAdapter.class}) 如果有自定义的WebSecurityConfigurerAdapter那么这个配置也就不会起作用了 
  1. @Configuration(
  2. proxyBeanMethods = false
  3. )
  4. @ConditionalOnBean({WebSecurityConfigurerAdapter.class})
  5. @ConditionalOnMissingBean(
  6. name = {"springSecurityFilterChain"}
  7. )
  8. @ConditionalOnWebApplication(
  9. type = Type.SERVLET
  10. )
  11. @EnableWebSecurity
  12. public class WebSecurityEnablerConfiguration {
  13. public WebSecurityEnablerConfiguration() {
  14. }
  15. }
最关键的就是@EnableWebSecurity注解了,我们点进去玩玩
  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target({ElementType.TYPE})
  3. @Documented
  4. @Import({WebSecurityConfiguration.class, SpringWebMvcImportSelector.class, OAuth2ImportSelector.class})
  5. @EnableGlobalAuthentication
  6. @Configuration
  7. public @interface EnableWebSecurity {
  8. boolean debug() default false;
  9. }
在这个接口中我们看到做的事情还是蛮多的,导入了三个类型和一个@EnableGlobalAuthentication注解,我们需要重点来看下WebSecurityConfiguration配置类和@EnableGlobalAuthentication注解
WebSecurityConfiguration
  1. public Filter springSecurityFilterChain() throws Exception {
  2. boolean hasConfigurers = this.webSecurityConfigurers != null && !this.webSecurityConfigurers.isEmpty();
  3. if (!hasConfigurers) {
  4. WebSecurityConfigurerAdapter adapter = (WebSecurityConfigurerAdapter)this.objectObjectPostProcessor.postProcess(new WebSecurityConfigurerAdapter() {
  5. });
  6. this.webSecurity.apply(adapter);
  7. }
                 // 获取到的就是FilterChainProxy 该对象会被保存到IoC容器中 且name为springSecurityFilterChain 
  1. return (Filter)this.webSecurity.build();
  2. }

  

 

  

SpringSecurity原理的更多相关文章

  1. SpringSecurity原理解析以及CSRF跨站请求伪造攻击

    SpringSecurity SpringSecurity是一个基于Spring开发的非常强大的权限验证框架,其核心功能包括: 认证 (用户登录) 授权 (此用户能够做哪些事情) 攻击防护 (防止伪造 ...

  2. SpringSecurity原理剖析与权限系统设计

    Spring Secutity和Apache Shiro是Java领域的两大主流开源安全框架,也是权限系统设计的主要技术选型.本文主要介绍Spring Secutity的实现原理,并基于Spring ...

  3. spring-security原理学习

    spring security使用分类: 如何使用spring security,相信百度过的都知道,总共有四种用法,从简到深为:1.不用数据库,全部数据写在配置文件,这个也是官方文档里面的demo: ...

  4. SpringSecurity过滤器原理

    SpringSecurity原理 主要过滤器链 SpringSecurity的功能主要是由一系列的过滤器链相互配合完成的.验证一个过滤器之后放行到下一个过滤器链,然后到最后. 认证流程 过滤器作用 S ...

  5. Spring Security OAuth2 完全解析 (流程/原理/实战定制) —— Client / ResourceServer 篇

    一.前言 本文假设读者对 Spring Security 本身原理有一定程度的了解,假设对 OAuth2 规范流程.Jwt 有基础了解,以此来对 SpringSecurity 整合 OAuth2 有个 ...

  6. DelegatingFilterProxy干了什么?

    org.springframework.web.filter.DelegatingFilterProxy 一般情况,创建一个Filter是交给自己来实现的.基于servlet规范,在web.xml中配 ...

  7. spring security整体流程

    spring-security原理 图片中各个类的作用: 1JwtUser类:实现Springsecurity的UserDetails类,此类必须有三个属性 private String userna ...

  8. JavaSSM-总结

    Spring框架技术 SSM(Spring+SpringMVC+Mybatis)阶段的学习,也算是成功出了Java新手村. 前面我们已经学习过Mybatis了. 从这里开始,很多的概念理解起来就稍微有 ...

  9. springSecurity安全框架的学习和原理解读

    最近在公司的项目中使用了spring security框架,所以有机会来学习一下,公司的项目是使用springboot搭建 springBoot版本1.59 spring security 版本4.2 ...

随机推荐

  1. Unity中的枚举和标志

    译林军 宿学龙|2014-04-10 08:56|9007次浏览|Unity(377)0 枚举和标志 今天的主题是枚举,它是C#语言中的一个很有帮助的工具,可以增强代码的清晰度以及准确性. 枚举一系列 ...

  2. Eclipse的安装和配置

    1. 下载Eclipse 前往Eclipse官网(https://www.eclipse.org/downloads/packages/)下载Eclipse: 这里下载的版本为: 这里给出该版本的百度 ...

  3. 11_IO多路复用

    1.IO概述 input 和 output: 是在内存中存在的数据交换操作 内存和磁盘交换: 文件读写, 打印 内存和网络交换: recv send recvfrom, sendto IO密集型程序: ...

  4. 跟着兄弟连系统学习Linux-【day02】

    day02-20200528 p6.vmvare安装与使用         官网下载安装包,个人学习的时候要求不高,所以不用安装最新版本,用不到那么多的功能,保证稳定版本就好了,然后傻瓜式安装.注意安 ...

  5. [业界方案] 用SOFATracer学习分布式追踪系统Opentracing

    [业界方案] 用SOFATracer学习分布式追踪系统Opentracing 目录 [业界方案] 用SOFATracer学习分布式追踪系统Opentracing 0x00 摘要 0x01 缘由 &am ...

  6. vue-devtools-4.1.4_0.crx及Vue.js not detected的问题

    谷歌-更多工具-扩展程序 Vue.js not detected的问题

  7. Ajax跨域解决方案大全

    题纲 关于跨域,有N种类型,本文只专注于ajax请求跨域(,ajax跨域只是属于浏览器"同源策略"中的一部分,其它的还有Cookie跨域iframe跨域,LocalStorage跨 ...

  8. python应用 曲线拟合03

    问题 有许多待拟合的曲线,需批量拟合. 解决 写一个类 # -*- coding: utf-8 -*- """ @author: kurrrr ""& ...

  9. NOIP2017 Day1 T1 小凯的疑惑

    题目描述 小凯手中有两种面值的金币,两种面值均为正整数且彼此互素.每种金币小凯都有 无数个.在不找零的情况下,仅凭这两种金币,有些物品他是无法准确支付的.现在小凯想知道在无法准确支付的物品中,最贵的价 ...

  10. pycharm2020.2破解版教程激活码支持Windows Linux Mac系统-中关村老大爷

    听说很多朋友想要PyCharm专业版2020.2的破解教程.现在来了,亲测破解成功.支持mac linux windows系统.本教程提供官方安装包.激活码和注册补丁. 本教程仅供学习和讨论,禁止商业 ...