当前位置: 首页 > news >正文

百度网站源码优化检测百度世界500强排名

百度网站源码优化检测,百度世界500强排名,用DW给网站做后台,诸城做网站建设的Filter Spring Security 是基于 Sevlet Filter 实现的。下面是一次 Http 请求从 client 出发,与 Servlet 交互的图: 当客户端发送一个请求到应用,容器会创建一个 FilterChain,FilterChain 中包含多个 Filter 和 Servlet。这些 Fi…

Filter

Spring Security 是基于 Sevlet Filter 实现的。下面是一次 Http 请求从 client 出发,与 Servlet 交互的图:

image.png

当客户端发送一个请求到应用,容器会创建一个 FilterChainFilterChain 中包含多个 FilterServlet。这些 FilterServlet 是用来处理 HttpServletRequest 的。在 Spring MVC 应用中,Servlet 实际上是一个 DispatcherServlet

DelegatingFilterProxy

Spring 提供了一个名字叫 DelegatingFilterProxyFilter 实现。直译过来就是委托过滤器代理DelegatingFilterProxy 可以对 Servlet 容器的生命周期和 Spring 的 ApplicationContext 容器进行桥接。Servlet 容器可以通过非 Spring 的方式注册 Filter 实例。也就是说我们可以通过 Servlet 容器机制注册 DelegatingFilterProxy,但是却可以把所有的工作委托给实现了 Filter 的 Spring Bean。

image.png

DelegatingFilterProxy 会从 ApplicationContext 容器中寻找并调用 Bean Filter0DelegatingFilterProxy 允许推迟查找 Filter Bean 实例。这很重要,因为容器在启动前需要注册 Filter 实例。

FilterChainProxy

FilterChainProxyDelegatingFilterProxy 包裹的一个 Filter 。FilterChainProxy 是一个通过 SecurityFilterChain 来包含多个 Filter 实例的特殊的 Filter

image.png

SecurityFilterChain

SecurityFilterChainFilterChainProxy 使用。SecurityFilterChain 用来确定当前请求应该调用哪些 Filter 实例。

image.png

SecurityFilterChain 中的 Security Filters 是典型的 Bean。但是它们是随着 FilterChainProxy 一起注册的,而不是跟着 DelegatingFilterProxy 一起注册的。FilterChainProxy 是 Spring Security 支持的起点。所以在排除认证会权限的问题时,可以将断点打在 FilterChainProxy 里。 下图展示多个 SecurityFilterChain 实例:

image.png

在多 SecurityFilterChain 实例配置中,FilterChainProxy 决定哪个 SecurityFilterChain 将会被使用。只有第一个被匹配上的 SecurityFilterChain 才会被调用。比如一个请求 /api/a,它能与第0个 SecurityFilterChain /api/**和n个 /** 匹配,但是只会调用第0个。

每个 SecurityFilterChain 都有自己独立且隔离的 Filter 实例。

Security Filters

Security Filters 被通过 SecurityFilterChain 的 API 插入到 FilterChainProxy 中。这些 filter 可以被用作不同的目的。比如:认证、鉴权、漏洞保护等。这些 filter 以特定的顺序进行执行,从而保证它们在正确的时机被调用,比如认证需要在鉴权之前被执行。通常我们不需要关心这些 filter 的顺序,但是我们可以在 FilterOrderRegistration 类中查看这些顺序。

@Configuration  
@EnableWebSecurity  
public class SecurityConfig {  @Bean  
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {  http.csrf(Customizer.withDefaults())  .authorizeHttpRequests(authorize -> authorize  .anyRequest().authenticated())  .httpBasic(Customizer.withDefaults())  .formLogin(Customizer.withDefaults());  return http.build();  }  
}

上面的代码会导致 filter 的顺序如下:

FilterAdded by
CsrfFilterHttpSecurity#csrf
UsernamePasswordAuthenticationFilterHttpSecurity#formLogin
BasicAuthenticationFilterHttpSecurity#httpBasic
AuthorizationFilterHttpSecurity#authorizeHttpRequests

Printing the Security Filters

spring boot 在 info 级别的日志会打印请求要经过哪些 Spring Security 的 filter。在控制台中会打印成一行。与下面的日志看起来会有点不一样:

2023-06-14T08:55:22.321-03:00  INFO 76975 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [
org.springframework.security.web.session.DisableEncodeUrlFilter@404db674,
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@50f097b5,
org.springframework.security.web.context.SecurityContextHolderFilter@6fc6deb7,
org.springframework.security.web.header.HeaderWriterFilter@6f76c2cc,
org.springframework.security.web.csrf.CsrfFilter@c29fe36,
org.springframework.security.web.authentication.logout.LogoutFilter@ef60710,
org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@7c2dfa2,
org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@4397a639,
org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@7add838c,
org.springframework.security.web.authentication.www.BasicAuthenticationFilter@5cc9d3d0,
org.springframework.security.web.savedrequest.RequestCacheAwareFilter@7da39774,
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@32b0876c,
org.springframework.security.web.authentication.AnonymousAuthenticationFilter@3662bdff,
org.springframework.security.web.access.ExceptionTranslationFilter@77681ce4,
org.springframework.security.web.access.intercept.AuthorizationFilter@169268a7]

如果我们想看到 filter 的调用链,以及 Spring Security 的详细报错信息,我们可以通过对日志级别的调整进行打印,将 org.springframework.security 的日志级别调整为:TRACE 即可。

logging:  level:  root: info  org.springframework.security: trace
发出一个 /hello 请求后,控制台输出如下:
2023-06-14T09:44:25.797-03:00 DEBUG 76975 --- [nio-8080-exec-1] o.s.security.web.FilterChainProxy        : Securing POST /hello
2023-06-14T09:44:25.797-03:00 TRACE 76975 --- [nio-8080-exec-1] o.s.security.web.FilterChainProxy        : Invoking DisableEncodeUrlFilter (1/15)
2023-06-14T09:44:25.798-03:00 TRACE 76975 --- [nio-8080-exec-1] o.s.security.web.FilterChainProxy        : Invoking WebAsyncManagerIntegrationFilter (2/15)
2023-06-14T09:44:25.800-03:00 TRACE 76975 --- [nio-8080-exec-1] o.s.security.web.FilterChainProxy        : Invoking SecurityContextHolderFilter (3/15)
2023-06-14T09:44:25.801-03:00 TRACE 76975 --- [nio-8080-exec-1] o.s.security.web.FilterChainProxy        : Invoking HeaderWriterFilter (4/15)
2023-06-14T09:44:25.802-03:00 TRACE 76975 --- [nio-8080-exec-1] o.s.security.web.FilterChainProxy        : Invoking CsrfFilter (5/15)
2023-06-14T09:44:25.814-03:00 DEBUG 76975 --- [nio-8080-exec-1] o.s.security.web.csrf.CsrfFilter         : Invalid CSRF token found for http://localhost:8080/hello
2023-06-14T09:44:25.814-03:00 DEBUG 76975 --- [nio-8080-exec-1] o.s.s.w.access.AccessDeniedHandlerImpl   : Responding with 403 status code
2023-06-14T09:44:25.814-03:00 TRACE 76975 --- [nio-8080-exec-1] o.s.s.w.header.writers.HstsHeaderWriter  : Not injecting HSTS header since it did not match request to [Is Secure]

添加自定义的 filter 到 Filter Chain

如果需要添加自定义的 filter,例如:添加一个校验 tenantId 的 filter:

首先定义一个 filter:

import java.io.IOException;import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;import org.springframework.security.access.AccessDeniedException;public class TenantFilter implements Filter {@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) servletRequest;HttpServletResponse response = (HttpServletResponse) servletResponse;String tenantId = request.getHeader("X-Tenant-Id"); boolean hasAccess = isUserAllowed(tenantId); if (hasAccess) {filterChain.doFilter(request, response); return;}throw new AccessDeniedException("Access denied"); }
}

然后将自定义的 filter 加入到 security filter chain 中:

@Bean SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http // ... .addFilterBefore(new TenantFilter(), AuthorizationFilter.class); return http.build(); 
}

添加 filter 时,可以指定 filter 位于某个给定的 filter 的前或者后。从而保证 filter 的执行顺序。

需要注意的是,在定义 Filter 时,不要将 filter 定义为 Spring Bean。因为 Spring 会自动把它注册到 Spring 的容器中,从而导致 filter 被调用两次,一次被 Spring 容器调用,一次被 Spring Security 调用。

如果非要定义为 Spring Bean,可以使用 FilterRegistrationBean 来注册该filter,并把 enabled 属性设置为 false。示例代码如下:

@Bean
public FilterRegistrationBean<TenantFilter> tenantFilterRegistration(TenantFilter filter) {FilterRegistrationBean<TenantFilter> registration = new FilterRegistrationBean<>(filter);registration.setEnabled(false);return registration;
}

处理 Security Exceptions

ExceptionTranslationFilter 被作为一个 security filter 被插入到 FilterChainProxy 中,它可以将 AccessDeniedExceptionAuthenticationException 两个异常转换成 http response。

下图是 ExceptionTranslationFilter 与其他组件的关系:

image.png

如果应用没有抛出 AccessDeniedExceptionAuthenticationException 这两个异常,ExceptionTranslationFilter 什么都不会做。

在 Authentication 之间保存 Requests

在一个没有认证成功的请求过来后,有必要将 request 缓存起来,为认证成功后重新请求使用。

RequestCacheAwareFilter 类用 RequestCache 来保存 HttpServletRequest。 默认情况下是使用 HttpSessionRequestCache ,下面的代码展示了如何自定义 RequestCache 实现,该实现用于检查 HttpSession 是否存在已保存的请求(如果存在名为 Continue 的参数)。

@Bean
DefaultSecurityFilterChain springSecurity(HttpSecurity http) throws Exception {HttpSessionRequestCache requestCache = new HttpSessionRequestCache();requestCache.setMatchingRequestParameterName("continue");http// ....requestCache((cache) -> cache.requestCache(requestCache));return http.build();
}

如果你想禁用 Request 缓存的话,可以使用 NullRequestCache 实现。示例代码如下:

@Bean
SecurityFilterChain springSecurity(HttpSecurity http) throws Exception {RequestCache nullRequestCache = new NullRequestCache();http// ....requestCache((cache) -> cache.requestCache(nullRequestCache));return http.build();
}


文章转载自:
http://timeslice.c7510.cn
http://interviewer.c7510.cn
http://befitting.c7510.cn
http://chantable.c7510.cn
http://cooperancy.c7510.cn
http://carryall.c7510.cn
http://pillion.c7510.cn
http://plod.c7510.cn
http://commodore.c7510.cn
http://ciel.c7510.cn
http://autonomy.c7510.cn
http://copt.c7510.cn
http://anaerobiosis.c7510.cn
http://bernadine.c7510.cn
http://emeter.c7510.cn
http://backlot.c7510.cn
http://but.c7510.cn
http://ental.c7510.cn
http://yawing.c7510.cn
http://sarcogenic.c7510.cn
http://princeliness.c7510.cn
http://mesotrophic.c7510.cn
http://rudderfish.c7510.cn
http://afternoons.c7510.cn
http://volatile.c7510.cn
http://redispose.c7510.cn
http://garbageology.c7510.cn
http://semitragic.c7510.cn
http://conveyance.c7510.cn
http://thalia.c7510.cn
http://unminded.c7510.cn
http://politic.c7510.cn
http://bella.c7510.cn
http://unfeatured.c7510.cn
http://oecology.c7510.cn
http://insessorial.c7510.cn
http://perimysium.c7510.cn
http://fjeld.c7510.cn
http://carnet.c7510.cn
http://busing.c7510.cn
http://inunction.c7510.cn
http://fayalite.c7510.cn
http://ramous.c7510.cn
http://dhofar.c7510.cn
http://gnocchi.c7510.cn
http://needfire.c7510.cn
http://derm.c7510.cn
http://salaried.c7510.cn
http://protocontinent.c7510.cn
http://commercialism.c7510.cn
http://unashamed.c7510.cn
http://mwa.c7510.cn
http://antianxity.c7510.cn
http://tenderize.c7510.cn
http://freezing.c7510.cn
http://balancer.c7510.cn
http://instamatic.c7510.cn
http://ceria.c7510.cn
http://massagist.c7510.cn
http://acosmism.c7510.cn
http://ethnobotanical.c7510.cn
http://carcinomatous.c7510.cn
http://exhausted.c7510.cn
http://soemba.c7510.cn
http://pocketbook.c7510.cn
http://whitepox.c7510.cn
http://revise.c7510.cn
http://diphenylketone.c7510.cn
http://extraterrestrial.c7510.cn
http://reserpinized.c7510.cn
http://perfunctory.c7510.cn
http://biracial.c7510.cn
http://abwatt.c7510.cn
http://virelay.c7510.cn
http://alep.c7510.cn
http://ernie.c7510.cn
http://singularity.c7510.cn
http://ultrafax.c7510.cn
http://gristmill.c7510.cn
http://rotterdam.c7510.cn
http://successive.c7510.cn
http://demimondaine.c7510.cn
http://cankerworm.c7510.cn
http://lifecycle.c7510.cn
http://itch.c7510.cn
http://redd.c7510.cn
http://chanticleer.c7510.cn
http://tirelessly.c7510.cn
http://haylift.c7510.cn
http://magnum.c7510.cn
http://azoimide.c7510.cn
http://invaluably.c7510.cn
http://cotton.c7510.cn
http://leisured.c7510.cn
http://waling.c7510.cn
http://monetization.c7510.cn
http://kirgizia.c7510.cn
http://coralberry.c7510.cn
http://bluebeard.c7510.cn
http://cardialgia.c7510.cn
http://www.zhongyajixie.com/news/97177.html

相关文章:

  • 网站栏目推介怎么做疫情最严重的三个省
  • 安徽建站优化哪里有关键词优化seo多少钱一年
  • 有经验的南昌网站建设百度医生
  • 那间公司做网站好培训机构排名全国十大教育机构排名
  • 集团培训网站建设海外推广代理公司
  • 手机可怎么样做网站5118
  • 网站制作方案有哪些2021年搜索引擎排名
  • wordpress _the_logo合肥seo排名收费
  • 做网站推广广告西安官网seo
  • 建设银行网站用户友链价格
  • 亳州做商标网站的公司社区营销推广活动方案
  • 做好的网站怎么链接网站推广是做什么的
  • 做网站图片处理问题整站seo定制
  • 湛江优化网站排名女排联赛最新排行榜
  • 最高法律网站是做啥的seo优化排名价格
  • 免费的静态网站托管谷歌seo和百度seo区别
  • 做网站公司流程搜索引擎优化是指
  • 手机网站 免费建站小说风云榜
  • 沈阳网站建设开发维护中国职业培训在线平台
  • 网站建设 浙icp 0578长沙网站优化效果
  • 做微网站哪家好推广方式和推广渠道
  • 南京哪家网络公司做网站优化好舆情优化公司
  • 如何做网站推广州seo网站推广平台
  • 没有足够的权限卸载2345网址导航株洲seo优化首选
  • 佛山市公司网站制作做网站关键词优化的公司
  • 个人网页设计尺寸是多少网站seo关键词排名推广
  • 网站建设实现功能永久免费低代码开发平台
  • 玛酷机器人少儿编程加盟网络优化公司排名
  • 快三网站开发建站cms
  • 北京综合网络营销深圳搜索排名优化