คำขอ Spring Security และ Angular 6 HTTPS

แอปแบ็กเอนด์ของฉันใน Spring Boot และรักษาความปลอดภัยด้วย SSL ฉันใช้การเข้าสู่ระบบ Facebook OAuth2 ยังเป็นแอปส่วนหน้าใน Angular 7 และรักษาความปลอดภัยด้วย ssl ปัญหาของฉันคือการส่งคำขอ Angular ไปยังแอป Spring boot ของฉัน แอพทั้งหมดเป็น https

ป.ล. ใช้งานได้ทั้งหมดถ้าฉันเพิ่ม url ให้กับ webSecurity.ignoring() และไม่รักษาความปลอดภัยแบ็กเอนด์ของฉัน ฉันคิดว่ามีปัญหาบางอย่างเกี่ยวกับความปลอดภัยและคำขอ HTTPS ขอบคุณที่ช่วยเหลือ.

แบ็กเอนด์

SecurityConfig.java

@RestController
@CrossOrigin(origins = "https://192.168.1.106:4400")
@Configuration
@Order(1000)
@EnableWebSecurity

public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserRepo userRepo;

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .cors().and()
            .csrf().disable()
            .authorizeRequests()
            .antMatchers(HttpMethod.GET, "/unauth/**").permitAll()
            .antMatchers(HttpMethod.POST, "/unauth/upload").permitAll()

            .antMatchers(HttpMethod.POST, "/api/**").authenticated()
            .antMatchers(HttpMethod.PUT, "/api/**").authenticated()
            .antMatchers(HttpMethod.DELETE, "/api/**").authenticated()
            .antMatchers(HttpMethod.GET, "/api/**").authenticated()
            .anyRequest().permitAll()
            .and().logout().logoutSuccessUrl("/").permitAll();

}

@Override
public void configure(WebSecurity webSecurity) {
    webSecurity.ignoring().antMatchers(HttpMethod.GET, "/unauth/**");
    webSecurity.ignoring().antMatchers(HttpMethod.POST, "/unauth/**");
}
  webSecurity.ignoring().antMatchers(HttpMethod.POST, "/unauth/**");
}

SomeRestController.java

 @RestController
 @CrossOrigin(origins = "https://192.168.1.106:4400")
  @RequestMapping ("/api")
 public class ProductService {



@Autowired
private ProductRepo productRepo;

@CrossOrigin(origins = "https://192.168.1.106:4400")
@GetMapping("/products")
public List<Product> getProducts(){
    return productRepo.findAll();

}

SpringBootApplication.java

@SpringBootApplication
@EnableOAuth2Sso
@CrossOrigin(origins = {"https://192.168.1.106:4400"}, allowCredentials = "false")
public class MongoTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(MongoTestApplication.class, args);
    }
}

ส่วนหน้า

SomeComponent.html ‹ ปุ่ม (click)="makeRequest()"> ทำคำขอ ‹ /button >

SomeComponent.ts

val:any = {};
  makeRequest(){
    this.http.get("https://localhost:8443/api/products").subscribe(value =>  {this.val = value; console.log(this.val.key)});
  }

ข้อผิดพลาด

error in browser

Access to XMLHttpRequest at 'https://localhost:8443/api/brands' from origin 'https://192.168.1.106:4400' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
core.js.pre-build-optimizer.js:15714 ERROR n {headers: t, status: 0, statusText: "Unknown Error", url: "https://localhost:8443/api/brands", ok: false, …}

person Ibrahim Rajabli    schedule 18.03.2019    source แหล่งที่มา
comment
คุณตั้งค่า Cross-Origin บนฝั่ง API เป็น 192.168.1.106:4400 แต่เข้าถึงได้จาก localhost:8443/api/products เป็นชื่อโดเมนที่แตกต่างอย่างสิ้นเชิง ลองเข้าถึงโดยใช้ IP (พอร์ตควรจะเหมือนกัน) มีพร็อกซีเซิร์ฟเวอร์อยู่ระหว่างไคลเอนต์และแบ็กเอนด์หรือไม่ เนื่องจากคุณเข้าถึง 8443 และคาดว่าจะกำหนดเส้นทางไปยังพอร์ต 4400   -  person printfmyname    schedule 18.03.2019
comment
ฉันลองกับแอพทั้งหมดด้วย localhost ผลลัพธ์เดียวกัน ฉันคิดว่าไม่เป็นไรหรอก   -  person Ibrahim Rajabli    schedule 18.03.2019
comment
ลองใช้คำตอบนี้ด้วยวิธีแก้ปัญหาที่ใช้งานได้กับปัญหาเดียวกันกับที่คุณประสบ stackoverflow .com/questions/40286549/spring-boot-security-cors   -  person printfmyname    schedule 18.03.2019
comment
ฉันใช้ทุกอย่างที่เป็นไปได้เกี่ยวกับการกรอง ไม่มีผลลัพธ์. เพียงแค่ให้ข้อยกเว้นเช่น org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource ...'   -  person Ibrahim Rajabli    schedule 18.03.2019
comment
เป็นข้อผิดพลาดบ่อยครั้งกับ Access-Control-Allow-Origin ลองค้นหาเพิ่มเติม แนะนำให้ค้นหาวิธีใช้ webpack proxy เพื่อให้ด้านหน้าและด้านหลังดูเหมือนทำงานบนพอร์ตเดียวกัน คำอธิบายประกอบ @CrossOrigin(...) ที่คุณใช้ไม่ใช่แนวทางปฏิบัติที่ดีที่สุด   -  person Amir Choubani    schedule 19.03.2019
comment
@AmirChoubani หนึ่งคำถาม เหตุใดจึงใช้งานได้เมื่อฉันเพิ่มลิงก์นี้ไปที่ webSecurity.ignoring().antMatchers(HttpMethod.GET, /api/**); ทั้งหมดทำงานได้อย่างสมบูรณ์แบบ ? ทำไมเราถึงต้องการพอร์ตเดียวกัน? มันเป็นแอพที่แตกต่างกัน และฉันคิดว่าไม่จำเป็นต้องมีพอร์ตคำเดียว   -  person Ibrahim Rajabli    schedule 19.03.2019
comment
ปลั๊กอิน webpack ช่วยให้จัดการกับ Access-Control-Allow-Origin ได้ง่าย ด้วยวิธีแก้ปัญหาที่คุณแนะนำ คุณพูดกับ Spring Security เฮ้ เพิกเฉยต่อคำขอประเภท get และโพสต์ ect ที่มาจากภายนอกไปยัง url /api/**   -  person Amir Choubani    schedule 19.03.2019
comment
โอเค ฉันจะค้นหามันตอนนี้เลย   -  person Ibrahim Rajabli    schedule 19.03.2019


คำตอบ (1)


แก้ไขคลาสหลักของคุณตามด้านล่าง และลบ @CrossOrigin ทั้งหมดออกจากคอนโทรลเลอร์

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
@EnableOAuth2Sso
public class MongoTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(MongoTestApplication.class, args);
    }

 @SuppressWarnings("deprecation")
    @Bean
        public WebMvcConfigurer corsConfigurer()
        {
            return new WebMvcConfigurerAdapter() {
                @Override
                public void addCorsMappings(CorsRegistry registry) {
                    registry.addMapping("/**").allowedMethods("GET", "PUT", "POST", "DELETE", "OPTIONS");
                }    
            };
        }
}
person shreyansh sharma    schedule 18.03.2019
comment
ฉันเปลี่ยนชั้นเรียนแต่ผลลัพธ์ก็เหมือนเดิม - person Ibrahim Rajabli; 18.03.2019