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

SpringSecurity如何配置跨域訪問

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

說明

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

配置方法如下:

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的跨域過濾器即可。

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

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

總結(jié)

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

相關(guān)文章

最新評(píng)論

方城县| 青铜峡市| 通山县| 孟津县| 大邑县| 武义县| 门源| 东乡县| 南平市| 黑河市| 友谊县| 蒙自县| 平阴县| 米泉市| 嘉禾县| 商水县| 谢通门县| 肥城市| 天柱县| 抚远县| 武冈市| 称多县| 东莞市| 西丰县| 松滋市| 彰化县| 紫金县| 元江| 崇仁县| 沅江市| 若羌县| 扎兰屯市| 驻马店市| 太仆寺旗| 望奎县| 濉溪县| 诸城市| 福建省| 平阴县| 平南县| 靖边县|