最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringSecurity如何配置跨域訪問(wèn)

 更新時(shí)間:2024年08月22日 09:08:45   作者:chinoukin  
這篇文章主要介紹了SpringSecurity如何配置跨域訪問(wèn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

說(shuō)明

java后端web服務(wù)有很多種方法可以實(shí)現(xiàn)跨域訪問(wèn),配置很簡(jiǎn)單,今天這里我們用SpringSecurity的方式配置跨域訪問(wèn)

配置方法如下:

package com.wisea.config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }
}

其他

看網(wǎng)上的配置里會(huì)有代碼如下:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors();
        ...
    }

實(shí)際上并不起什么作用,總結(jié),當(dāng)工程中開啟了@EnableWebSecurity的時(shí)候,我們只需要讓spring容器中存在一個(gè)CorsFilter的跨域過(guò)濾器即可。

some days 幾天發(fā)現(xiàn)問(wèn)題:

當(dāng)請(qǐng)求的中有redirect時(shí),上面這段代碼就必須了。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

宝鸡市| 含山县| 平潭县| 松江区| 龙岩市| 洪江市| 岳阳县| 光泽县| 榆社县| 银川市| 明星| 寿阳县| 丰宁| 马关县| 大埔县| 嵊州市| 托克逊县| 扶风县| 邹平县| 长葛市| 星子县| 三河市| 崇左市| 行唐县| 兴安县| 武清区| 黎川县| 察雅县| 克山县| 贞丰县| 屯留县| 夏津县| 都匀市| 莎车县| 陈巴尔虎旗| 三原县| 南靖县| 奉节县| 西宁市| 延边| 遵化市|