亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

SpringBoot+Spring Security無法實現跨域的解決方案

瀏覽:61日期:2023-02-24 14:51:17
SpringBoot+Spring Security無法實現跨域未使用Security時跨域:

import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.autoconfigure.AutoConfigureBefore;import org.springframework.context.annotation.Configuration;import org.springframework.format.FormatterRegistry;import org.springframework.web.servlet.config.annotation.*;@Configuration@AutoConfigureBefore(SecurityConfig.class)public class MyMvcConfigurer implements WebMvcConfigurer { public void addCorsMappings(CorsRegistry registry){LOGGER.info('跨域已設置');registry.addMapping('/**').allowedOrigins('*').allowedMethods('*').allowedHeaders('*').allowCredentials(true).maxAge(3600); }}

整合Security時發現只用上述方法前后端分離時仍存在跨域問題,

解決方法如下:

@Configuration@AutoConfigureBefore(Swagger2Configuration.class)@EnableWebSecurity@EnableGlobalMethodSecurity(prePostEnabled = true)@Order(-1)public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception {http.formLogin().loginProcessingUrl('/user/login').loginPage('/singIn.html').successHandler(moyuAuthenticationSuccessHandler).failureHandler(moyuAuthenticationFailureHandler).and().apply(moyuSocialSecurityConfig).and().rememberMe().tokenRepository(persistentTokenRepository()).tokenValiditySeconds(3600*24*7).userDetailsService(userDetailsService).and().authorizeRequests().antMatchers('/user/login','/login','/singIn.html','**','/**').permitAll().anyRequest().authenticated().and().cors().and().csrf().disable(); }}重點加入代碼:

.and() .cors()//新加入 .and() .csrf().disable();引用Spring Security 項目的跨域處理

最近項目采用了前后端分離的框架,前端和后臺接口沒有部署到一個站點,出現了跨域問題,什么是跨域,這里就不再贅述,直接說解決辦法。

Spring 解決跨域的方式有很多,個人采用了Crosfilter的方式

具體代碼如下:

@Bean public CorsFilter corsFilter() {final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();final CorsConfiguration corsConfiguration = new CorsConfiguration();corsConfiguration.setAllowCredentials(true);corsConfiguration.addAllowedOrigin('*');corsConfiguration.addAllowedHeader('*');corsConfiguration.addAllowedMethod('*');urlBasedCorsConfigurationSource.registerCorsConfiguration('/**', corsConfiguration);return new CorsFilter(urlBasedCorsConfigurationSource); }

配置完成后,測試調用,報錯401,依然不行。網上查資料得知,跨域請求會進行兩次。具體流程見下圖:

SpringBoot+Spring Security無法實現跨域的解決方案

每次跨域請求,真正請求到達后端之前,瀏覽器都會先發起一個preflight request,請求方式為OPTIONS 詢問服務端是否接受該跨域請求,具體參數如下圖:

SpringBoot+Spring Security無法實現跨域的解決方案

但是該請求不能攜帶cookie和自己定義的header。

由于項目中引入了Spring security ,而我使用的token傳遞方式是在header中使用authorization 字段,這樣依賴Spring Security攔截到 preflight request 發現它沒有攜帶token,就會報錯401,沒有授權。

解決這個問題很簡單,可以使用以下配置

讓Spring security 不校驗preflight request 。

@Override public void configure(HttpSecurity http) throws Exception {ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = http.authorizeRequests();registry.requestMatchers(CorsUtils::isPreFlightRequest).permitAll();//讓Spring security放行所有preflight request }

再試就搞定了,但是后端直接配置支持跨域會導致兩次請求。還使用另一種方式,使用Nginx 轉發一下請求也可以。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 日本特黄的免费大片视频 | 丁香婷婷激情网 | 国产男同志gay毛片欧美 | 午夜国产精品久久影院 | 最新的国产成人精品2022 | 精品成人毛片一区二区视 | 日本高清视频网址 | 久草综合在线观看 | 国产色视频网站免费观看 | 打床炮视频在线观看免费 | 91嫩草国产在线观看免费 | 免费看片网址 | 黄色片免费在线观看 | 午夜精品久久久久久 | 精品三级国产一区二区三区四区 | aaa毛片在线| 欧美精品国产日韩综合在线 | 久久国产精品亚洲一区二区 | 国产美女亚洲精品久久久综合91 | 国产区精品高清在线观看 | 亚洲国产精品成人综合久久久 | 欧美黄色tv | 看片久久 | 草莓榴莲向日葵十八岁全微糖 | 亚洲精品123区在线观看 | 日本欧美大码aⅴ在线播放 日本欧美不卡一区二区三区在线 | 免费看的一级片 | 欧美视屏在线观看 | 成年人激情视频 | 久久国产成人 | 手机看片在线播放 | 三级黄色一级视频 | 久久亚洲综合中文字幕 | 做受又硬又粗又免费视频 | 97青青草原国产免费观看 | 一级片毛片 | 久久夜色tv网站免费影院 | 18一19日本人xxxx | 激情婷婷在线 | 国产成人www免费人成看片 | 免费播放欧美一级特黄 |