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

闵行区 网站制作wordpress主题seo模板

闵行区 网站制作,wordpress主题seo模板,做装修的业务网站,下载wordpress低版本目录 什么是跨域springboot是怎么解决跨域问题在springSecurity中怎么解决跨域问题CORS源码跨域请求伪造CSRFCSRF源码总结 什么是跨域 跨域是指在网络中#xff0c;当一个网页的资源#xff08;如字体、脚本或样式表#xff09;尝试从不同的域名、端口或协议请求数据时当一个网页的资源如字体、脚本或样式表尝试从不同的域名、端口或协议请求数据时会遇到安全限制问题。这是由于浏览器的同源策略所导致的。同源策略要求网页只能从同一域名下加载资源而跨域请求则违反了这个策略。 为了解决跨域问题可以采取一些方法如使用JSONP、CORS、代理服务器等。JSONP是通过动态创建 总结起来跨域是指在网络中由于浏览器的同源策略而限制了不同域名、端口或协议之间的资源请求。通过使用适当的跨域解决方案可以允许跨域请求并获取所需的数据。一般情况下我们呢都是使用CORS跨域资源共享来解决跨域问题。 springboot是怎么解决跨域问题 在Java中可以使用CORS跨域资源共享来解决跨域问题。下面是一个Java代码示例演示如何在Spring Boot应用程序中配置CORS。 首先在Spring Boot应用程序的配置类中添加以下代码 import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;Configuration public class CorsConfig implements WebMvcConfigurer {Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping(/**).allowedOrigins(http://example.com) // 允许的跨域请求来源.allowedMethods(GET, POST) // 允许的请求方法.allowedHeaders(Origin, Content-Type, Accept) // 允许的请求头.allowCredentials(true); // 允许发送身份凭证} }在上述代码中我们通过 addMapping() 方法指定了允许跨域请求的路径使用 allowedOrigins() 方法指定了允许的跨域请求来源使用 allowedMethods() 方法指定了允许的请求方法使用 allowedHeaders() 方法指定了允许的请求头。最后通过 allowCredentials(true) 方法允许发送身份凭证如Cookie。 以上只是个代码示例实际使用时要根据具体情况做响应配置。 在springSecurity中怎么解决跨域问题 在Spring Security中你可以使用 CorsConfigurationSource 接口来配置CORS跨域资源共享策略。下面是一个Java代码示例演示如何在Spring Security中配置CORS。 首先在你的Spring Security配置类中添加以下代码 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource;Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter {Overrideprotected void configure(HttpSecurity http) throws Exception {http.cors();// ...}Beanpublic CorsConfigurationSource corsConfigurationSource() {CorsConfiguration configuration new CorsConfiguration();configuration.setAllowedOrigins(Arrays.asList(http://localhost:8080));configuration.setAllowedMethods(Arrays.asList(GET, POST));configuration.setAllowedHeaders(Arrays.asList(Authorization, Cache-Control, Content-Type));UrlBasedCorsConfigurationSource source new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration(/**, configuration);return source;} }在上述代码示例中我们通过调用 http.cors() 方法来允许跨域请求。然后我们定义了一个 CorsConfigurationSource 的Bean用于配置CORS策略。在这个Bean中我们设置了允许的跨域请求来源、允许的请求方法和请求头。 最后我们使用 UrlBasedCorsConfigurationSource 类来注册CORS配置并将其应用于所有的请求路径 /** 。 CORS源码 源码入口 public CorsConfigurerHttpSecurity cors() throws Exception {return getOrApply(new CorsConfigurer());}private C extends SecurityConfigurerAdapterDefaultSecurityFilterChain, HttpSecurity C getOrApply(C configurer)throws Exception {C existingConfig (C) getConfigurer(configurer.getClass());if (existingConfig ! null) {return existingConfig;}//执行configurerreturn apply(configurer);}可以看到最终就是获取一个CorsConfigurer类我们看CorsConfigurer类的逻辑。主要看CorsConfigurer类的configure()方法 Overridepublic void configure(H http) {ApplicationContext context http.getSharedObject(ApplicationContext.class);//获取corsFilter过滤器CorsFilter corsFilter getCorsFilter(context);Assert.state(corsFilter ! null, () - Please configure either a CORS_FILTER_BEAN_NAME bean or a CORS_CONFIGURATION_SOURCE_BEAN_NAME bean.);//把corsFilter过滤器添加到过滤器链中http.addFilter(corsFilter);}获取corsFilter过滤器并把它添加到过滤器链中。看getCorsFilter(context)方法 private CorsFilter getCorsFilter(ApplicationContext context) {if (this.configurationSource ! null) {return new CorsFilter(this.configurationSource);}//corsFilter过滤器bean是否存在boolean containsCorsFilter context.containsBeanDefinition(CORS_FILTER_BEAN_NAME);if (containsCorsFilter) {return context.getBean(CORS_FILTER_BEAN_NAME, CorsFilter.class);}//corsConfigurationSource对象是否存在boolean containsCorsSource context.containsBean(CORS_CONFIGURATION_SOURCE_BEAN_NAME);if (containsCorsSource) {//把corsConfigurationSource对象塞进CorsFilter过滤器中CorsConfigurationSource configurationSource context.getBean(CORS_CONFIGURATION_SOURCE_BEAN_NAME,CorsConfigurationSource.class);return new CorsFilter(configurationSource);}boolean mvcPresent ClassUtils.isPresent(HANDLER_MAPPING_INTROSPECTOR, context.getClassLoader());if (mvcPresent) {return MvcCorsFilter.getMvcCorsFilter(context);}return null;}可以看到对应cors处理有两种情况 没有配置corsConfigurationSource对象直接使用默认的corsFilter过滤器配置corsConfigurationSource对象把配置corsConfigurationSource对象添加到默认的过滤器。 最后是调用的springWeb的CorsFilter并把此过滤器加入到SpringSecurity过滤器链中。 跨域请求伪造CSRF 跨域请求伪造Cross-Site Request ForgeryCSRF是一种安全漏洞攻击者利用该漏洞通过伪造请求来执行未经授权的操作。在CSRF攻击中攻击者诱使受害者在已登录的状态下访问恶意网站从而触发受害者在其他网站上的操作如转账、更改密码等。 在前后端分离的架构中可以使用CSRF令牌CSRF Token来验证请求的合法性。下面是一个使用Spring Security防止CSRF攻击的Java代码示例 配置Spring Security启用CSRF保护 Configuration EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {Overrideprotected void configure(HttpSecurity http) throws Exception {http// 其他Spring Security配置.and().csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());} }在前端页面中设置CSRF令牌 html html bodyform methodpost action/submitinput typehidden name${_csrf.parameterName} value${_csrf.token} /!-- 其他表单字段 --button typesubmit提交/button/form /body /html在上述示例中首先通过 csrf() 方法启用了CSRF保护并使用 CookieCsrfTokenRepository 来存储CSRF令牌。然后在前端页面的表单中通过 ${_csrf.parameterName} 和 ${_csrf.token} 获取CSRF令牌并将其作为隐藏字段传递给服务器。 这样在每次提交表单时CSRF令牌会被包含在请求中服务器会验证请求中的CSRF令牌是否与服务器生成的令牌匹配从而防止CSRF攻击的发生。 CSRF源码 代码入口 public CsrfConfigurerHttpSecurity csrf() throws Exception {ApplicationContext context getContext();return getOrApply(new CsrfConfigurer(context));}private C extends SecurityConfigurerAdapterDefaultSecurityFilterChain, HttpSecurity C getOrApply(C configurer)throws Exception {C existingConfig (C) getConfigurer(configurer.getClass());if (existingConfig ! null) {return existingConfig;}//执行configurerreturn apply(configurer);}主要是看CsrfConfigurer这个类 public final class CsrfConfigurerH extends HttpSecurityBuilderHextends org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurerCsrfConfigurerH, H {//初始化CsrfTokenRepository默认为LazyCsrfTokenRepositoryprivate CsrfTokenRepository csrfTokenRepository new LazyCsrfTokenRepository(new HttpSessionCsrfTokenRepository()); 默认是使用LazyCsrfTokenRepository并使用HttpSessionCsrfTokenRepository初始化。接下俩看CsrfConfigurer的configure方法。 Overridepublic void configure(H http) {//初始化CsrfFilter过滤器CsrfFilter filter new CsrfFilter(this.csrfTokenRepository);RequestMatcher requireCsrfProtectionMatcher getRequireCsrfProtectionMatcher();if (requireCsrfProtectionMatcher ! null) {filter.setRequireCsrfProtectionMatcher(requireCsrfProtectionMatcher);}AccessDeniedHandler accessDeniedHandler createAccessDeniedHandler(http);if (accessDeniedHandler ! null) {filter.setAccessDeniedHandler(accessDeniedHandler);}org.springframework.security.config.annotation.web.configurers.LogoutConfigurerH logoutConfigurer http.getConfigurer(org.springframework.security.config.annotation.web.configurers.LogoutConfigurer.class);if (logoutConfigurer ! null) {logoutConfigurer.addLogoutHandler(new CsrfLogoutHandler(this.csrfTokenRepository));}org.springframework.security.config.annotation.web.configurers.SessionManagementConfigurerH sessionConfigurer http.getConfigurer(org.springframework.security.config.annotation.web.configurers.SessionManagementConfigurer.class);if (sessionConfigurer ! null) {sessionConfigurer.addSessionAuthenticationStrategy(getSessionAuthenticationStrategy());}filter postProcess(filter);//加载到过滤器链中http.addFilter(filter);}这段很简单初始化CsrfFilter并把CsrfFilter添加到过滤器链中。 在LazyCsrfTokenRepository中就是对CsrfToken的操作 public interface CsrfTokenRepository {/*** Generates a {link CsrfToken}* param request the {link HttpServletRequest} to use* return the {link CsrfToken} that was generated. Cannot be null.*/CsrfToken generateToken(HttpServletRequest request);/*** Saves the {link CsrfToken} using the {link HttpServletRequest} and* {link HttpServletResponse}. If the {link CsrfToken} is null, it is the same as* deleting it.* param token the {link CsrfToken} to save or null to delete* param request the {link HttpServletRequest} to use* param response the {link HttpServletResponse} to use*/void saveToken(CsrfToken token, HttpServletRequest request, HttpServletResponse response);/*** Loads the expected {link CsrfToken} from the {link HttpServletRequest}* param request the {link HttpServletRequest} to use* return the {link CsrfToken} or null if none exists*/CsrfToken loadToken(HttpServletRequest request);}具体的实现类就不展开说感兴趣的可自行查看代码。 总结 跨域问题当前端应用和后端API不在同一个域下时浏览器会限制跨域请求。为了解决跨域问题可以在后端配置允许跨域请求的头信息如Access-Control-Allow-Origin。CSRF保护跨站请求伪造CSRF是一种安全漏洞攻击者利用用户在其他网站上的身份信息发起恶意请求。为了防止CSRF攻击可以在Spring Security中启用CSRF保护。通过生成和验证CSRF令牌确保请求的合法性。 在后端配置中启用CSRF保护并指定CSRF令牌的存储方式如CookieCsrfTokenRepository。在前端发起请求时将CSRF令牌作为请求的参数或头信息的一部分发送给后端。后端服务器验证请求中的CSRF令牌是否与服务器生成的令牌匹配以确保请求的合法性。
http://www.yutouwan.com/news/425605/

相关文章:

  • 网站制作价格怎么算服装设计图片
  • 网站制作公司浩森宇特网站建设的固定资产包括哪些
  • 网站后缀意思wordpress 调用文章分类
  • 深圳网站建设黄浦网络-技术差淘宝购物网
  • 想做个赚钱的网站不知道做那种网站首页设计风格有哪些
  • 签合网站是哪个多媒体展厅哪家公司好
  • 广州市住房和建设局网站手机在线制作图片
  • 邢台学校网站建设报价网站制作开发
  • 商务网站建设与维护论文抖音电商官网
  • 快速搭建网站视频做微商如何引流推广?怎么找客源?
  • 网站建设汇卓摄影网站建设策划书
  • a5站长网网站地图是什么样子的
  • nat123做网站 查封100m光纤做网站
  • 珠海企业集团网站建设如何修改网站后台时间
  • 园区网站建设服务公司长沙官网seo诊断
  • 公司网站制作有哪些注意事项温州通告最新
  • 淘宝客采集网站建设html电影网站模板下载
  • 溧阳人才网 网站开发宁波装修公司网站制作
  • Wordpress球队网站网站建站要求
  • WordPress多站點支付插件怎样管理一个俄语网站
  • 网站查询域名ip解析电商行业网站建设及维护
  • 苏州优化网站公司网站产品图怎么做
  • 南京 网站制作公司wordpress站点图片多大合适
  • 动漫网站策划书广告做图网站
  • 免费网站建设公司推荐文山网站建设联系电话
  • 一般网站版式有哪几种网站建设的价
  • 网站源码官网做百度网站接到多少客户电话
  • 如何建设好医院网站舟山公司做网站
  • 工装网站建设方案包装设计公司商业模式
  • 外贸网站建设 东莞wordpress分类目录 插件