第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶有 Spring Boot 2 的 CORS

帶有 Spring Boot 2 的 CORS

達(dá)令說 2022-07-14 10:14:01
我正在嘗試將我的 Angular 應(yīng)用程序連接到我的新 Spring Boot 2 控制器。我開始一切,我得到:Access to XMLHttpRequest at 'localhost:8093/restapi/setup' from origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.其次是:ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "localhost:8093/restapi/setup", ok: false, …}所以這是CORS,對吧?正如你所料,當(dāng)我localhost:8093/restapi/setup從郵遞員那里打來時,我得到了有效的回應(yīng)。所以我做了一些研究,特別是我發(fā)現(xiàn):No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API我終于在這里找到了這篇文章:https://chariotsolutions.com/blog/post/angular-2-spring-boot-jwt-cors_part1/這導(dǎo)致我得到以下代碼:@Configurationpublic class ManageConfiguration {    private static final Logger         LOGGER = LogManager.getLogger(ManageConfiguration.class);    @Bean    public CorsFilter corsFilter() {        LOGGER.debug("Configuring CORS");        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();        CorsConfiguration config = new CorsConfiguration();        config.setAllowCredentials(true);        config.addAllowedOrigin("*");        config.addAllowedHeader("*");        config.addAllowedMethod("OPTIONS");        config.addAllowedMethod("GET");        config.addAllowedMethod("POST");        config.addAllowedMethod("PUT");        config.addAllowedMethod("DELETE");        source.registerCorsConfiguration("/**", config);        return new CorsFilter(source);    }}所以我認(rèn)為這很簡單,現(xiàn)在再試一次,我得到:Access to XMLHttpRequest at 'localhost:8093/restapi/setup' from origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.其次是:ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "localhost:8093/restapi/setup", ok: false, …}一天來一直在為下一步要嘗試什么而撓頭,卻什么也想不出來。建議?
查看完整描述

3 回答

?
翻閱古今

TA貢獻(xiàn)1780條經(jīng)驗 獲得超5個贊

我認(rèn)為您http://在請求 URL 中發(fā)送沒有協(xié)議前綴的 ajax 請求,請嘗試http://localhost:8093/restapi/setup從 ajax 中點擊。



查看完整回答
反對 回復(fù) 2022-07-14
?
茅侃侃

TA貢獻(xiàn)1842條經(jīng)驗 獲得超22個贊

在您的代碼中添加此 WebSecurityConfigurerAdapter


import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;

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;


@EnableWebSecurity

@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)

public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {


  @Override

  protected void configure(HttpSecurity http) throws Exception {

    http.cors().and().csrf().disable();

  }


}

還要添加以下 WebMvcConfigurer


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 WebMvcConfigurerImpl implements WebMvcConfigurer {


  @Override

  public void addCorsMappings(CorsRegistry registry) {

    registry.addMapping("/**");

  }

}

最后在你的休息控制器類的頂部添加這個注釋:@CrossOrigin。


@CrossOrigin

public class RestController {

// Your methods

}

如果你有過濾器,你可以在響應(yīng)中添加以下屬性,如果你沒有,你可以使用這個。


import java.io.IOException;


import javax.servlet.FilterChain;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import org.springframework.stereotype.Service;

import org.springframework.web.filter.OncePerRequestFilter;


@Service

public class JwtAuthenticationFilter extends OncePerRequestFilter {


  @Override

  protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {


    response.setHeader("Access-Control-Allow-Origin", "*");

    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");

    response.setHeader("Access-Control-Allow-Credentials", "true");

    response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

    response.setHeader("Access-Control-Expose-Headers", "Content-Length, Authorization");

    filterChain.doFilter(request, response);

  }


}


查看完整回答
反對 回復(fù) 2022-07-14
?
慕尼黑的夜晚無繁華

TA貢獻(xiàn)1864條經(jīng)驗 獲得超6個贊

@Configuration

public class CorsConfig {


    @Bean

    public WebMvcConfigurer corsConfigurer() {

        return new WebMvcConfigurerAdapter() {

            @Override

            public void addCorsMappings(CorsRegistry registry) {

                registry.addMapping("/**").allowedMethods("GET", "POST", "PUT", "DELETE").allowedOrigins("*")

                        .allowedHeaders("*");

            }

        };

    }

}


@Configuration

@EnableWebMvc

public class WebConfig extends WebMvcConfigurerAdapter {


    @Override

    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**");

    }

}

請在此處查看教程https://spring.io/blog/2015/06/08/cors-support-in-spring-framework


查看完整回答
反對 回復(fù) 2022-07-14
  • 3 回答
  • 0 關(guān)注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號