แอปแบ็กเอนด์ของฉันใน 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, …}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource ...'
- person Ibrahim Rajabli   schedule 18.03.2019Access-Control-Allow-Origin
ลองค้นหาเพิ่มเติม แนะนำให้ค้นหาวิธีใช้webpack proxy
เพื่อให้ด้านหน้าและด้านหลังดูเหมือนทำงานบนพอร์ตเดียวกัน คำอธิบายประกอบ@CrossOrigin(...)
ที่คุณใช้ไม่ใช่แนวทางปฏิบัติที่ดีที่สุด - person Amir Choubani   schedule 19.03.2019Access-Control-Allow-Origin
ได้ง่าย ด้วยวิธีแก้ปัญหาที่คุณแนะนำ คุณพูดกับ Spring Security เฮ้ เพิกเฉยต่อคำขอประเภท get และโพสต์ ect ที่มาจากภายนอกไปยัง url /api/** - person Amir Choubani   schedule 19.03.2019