如何连接角+nginx码头集装箱到Spring,而不得到cors错误?

mrphzbgm  于 2022-11-02  发布在  Nginx
关注(0)|答案(1)|浏览(176)

我的问题是,每次我向服务器发出请求时,都会收到CORS错误。我试过从前端容器向服务器发出curl请求,它们在同一个网络上,一切正常,如果我在本地运行容器,也没有问题。
以下是我的设置:

我没有在nginx.conf中更改任何内容

替换default.conf的我的配置

server {
  listen 80;
  sendfile on;
  default_type application/octet-stream;

  gzip on;
  gzip_http_version 1.1;
  gzip_disable      "MSIE [1-6]\.";
  gzip_min_length   256;
  gzip_vary         on;
  gzip_proxied      expired no-cache no-store private auth;
  gzip_types        text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  gzip_comp_level   9;

  root /usr/share/nginx/html;

  location / {
    try_files $uri $uri/ /index.html =404;
  }
}

停靠合成

version: "3.5"
services:
  backend:
    image: spring_backend
    container_name: spring
    build:
      context: ./
    expose:
      - 8080
    restart: always
    networks:
      - reverse-proxy

  frontend:
    image: angular_frontend
    container_name: angular
    build: https://github.com/some_repo/frontend.git
    ports:
      - "80:80"
    restart: always
    networks:
      - reverse-proxy

networks:
  reverse-proxy:
    name: reverse-proxy

有Angular 的环境

export const environment = {
  url: 'http://spring:8080/',
  production: true,
};

后端配置

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final PersonDetailsService personDetailsService;
    private final JWTFilter jwtFilter;

    @Autowired
    public SecurityConfig(PersonDetailsService personDetailsService, JWTFilter jwtFilter){
        this.personDetailsService = personDetailsService;
        this.jwtFilter = jwtFilter;
    }

    @SneakyThrows(Exception.class)
    protected void configure(HttpSecurity http) {
        http
                .csrf().disable()
                .cors().configurationSource(corsConfigurationSource())
                .and()
                .authorizeRequests().antMatchers("/login","/register").permitAll()
                .anyRequest().hasAnyRole("USER")
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        http.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
    }

    @SneakyThrows(Exception.class)
    protected void configure(AuthenticationManagerBuilder auth) {
        auth.userDetailsService(personDetailsService);
    }
    @Bean
    public PasswordEncoder getPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Bean
    @SneakyThrows(Exception.class)
    public AuthenticationManager authenticationManagerBean() {
        return super.authenticationManagerBean();
    }
    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.addAllowedOrigin("*");
        configuration.addAllowedHeader("*");
        configuration.addAllowedMethod("*");
        configuration.addExposedHeader("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

}

今天我决定通过postman发出请求,注意到nginx向我返回了一个405错误,spring服务器没有接收到请求。

6yt4nkrj

6yt4nkrj1#

CORS都是关于后端问题。
基本上,它可以通过添加Access-Control-Allow-Origin *到服务器配置,让其他网站可以使用您的服务来解决。

nginx.配置文件

server {
   ...
   add_header Access-Control-Allow-Origin *;
   ...
}

相关问题