国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot+Spring Security無法實(shí)現(xiàn)跨域的解決方案

瀏覽:78日期:2023-02-24 14:51:17
SpringBoot+Spring Security無法實(shí)現(xiàn)跨域未使用Security時(shí)跨域:

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('跨域已設(shè)置');registry.addMapping('/**').allowedOrigins('*').allowedMethods('*').allowedHeaders('*').allowCredentials(true).maxAge(3600); }}

整合Security時(shí)發(fā)現(xiàn)只用上述方法前后端分離時(shí)仍存在跨域問題,

解決方法如下:

@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(); }}重點(diǎn)加入代碼:

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

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

Spring 解決跨域的方式有很多,個(gè)人采用了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); }

配置完成后,測試調(diào)用,報(bào)錯(cuò)401,依然不行。網(wǎng)上查資料得知,跨域請求會進(jìn)行兩次。具體流程見下圖:

SpringBoot+Spring Security無法實(shí)現(xiàn)跨域的解決方案

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

SpringBoot+Spring Security無法實(shí)現(xiàn)跨域的解決方案

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

由于項(xiàng)目中引入了Spring security ,而我使用的token傳遞方式是在header中使用authorization 字段,這樣依賴Spring Security攔截到 preflight request 發(fā)現(xiàn)它沒有攜帶token,就會報(bào)錯(cuò)401,沒有授權(quán)。

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

讓Spring security 不校驗(yàn)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 }

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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: a欧美在线 | 成年人视频网站免费 | 国产特黄特色一级特色大片 | 在线久草| 国产日韩欧美精品 | 日韩精品欧美激情国产一区 | 欧美a级毛片 | 色综合久久91 | 国产综合成人久久大片91 | 日本特黄特色视频 | 国内免费视频成人精品 | 国产日韩亚洲欧美 | 成人www| 国产福利精品在线观看 | 99久热在线精品视频观看 | 特黄a大片免费视频 | 中文字幕一区二区三区在线观看 | 美女黄网站色一级毛片 | 一区二区三区久久 | 性色aⅴ在线观看swag | 美女视频黄色免费 | 久久91精品国产91久久跳舞 | 女人十八一级毛片 | 亚洲国产中文字幕 | 337p粉嫩大胆噜噜噜鲁 | 小毛片在线观看 | 久久久99精品免费观看精品 | 黄网视频在线观看 | 日韩一区二区三区四区不卡 | 精品国产一区二区三区在线观看 | 亚洲精品国产成人 | 日本一区二区三区四区公司 | 手机在线看a | 日本欧美久久久久免费播放网 | 免费老外的毛片清高 | 日韩精品久久一区二区三区 | 亚洲国产成人久久综合碰 | 国产成年女一区二区三区 | www.久久久| 在线观看人成网站深夜免费 | 国产精品亚洲第一区二区三区 |