Siema
Mam pytanie odnośnie architektury.
Wymyśliłem sobie że zrobię dwa projekty pierwszy będzie to REST Spring serwer(www.example1.pl/api), a drugi to serwer zwracający frontend Reactowy (www.example2.pl) który wykonuje np fetch('www.example1.pl/api/dane").
Ogólnie udało mi się wyłączyć zabezpieczenie cors dla wszystkich endpointów lecz nadal nie mogę wysyłać posta na url www.example1.pl/login w celu uzyskania cookisa sesji.
No i pytania:
-
Pozostać przy koncepcji dwa osobne serwery front/backend czy zmienić aby to www.example1.pl/ zwracało całą stronę reactową.
-
W jaki sposób wyłączyć cors dla www.example1.pl/login
-
Zmiana koncepcji: rejestracja i logowanie poprzez front zwracany z backendu, uzyskujemy tam pewien ID który potem wysyłamy z reacta(www.example2.pl) do odpowiednich endpointów
które sprawdzają w bazie czy dany ID jest przypisany do jakiegoś użytkownika, jeżeli tak to zwraca dane
fetch("http://localhost:9666/api/data",{method:"POST",body:{ID:3125165}})
.then(res => res.json())
.then(elem => console.log(elem))
// Sprawdzam na stronie frontend wysyłanie posta na /login backendu
var formData =new FormData();
formData.append('username','test');
formData.append('password','test');
fetch("http://localhost:9666/login",{method:"POST",body:formData})
.then(res => res.json())
.then(elem => console.log(elem))
//Access to fetch at 'http://localhost:9666/login' from origin 'http://localhost:8081' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
// wykonując mode:"no-cors" Cross-Origin Read Blocking (CORB) blocked cross-origin response http://krancki.ddns.net:9666/ with MIME type application/json
//SpringSecurityConfig
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers(HttpMethod.POST,"/api/user").permitAll()
.antMatchers(HttpMethod.DELETE,"/api/user/").hasRole("ADMIN")
.antMatchers("/api/user/").hasRole("ADMIN")
.antMatchers(HttpMethod.GET,"/api/book").permitAll()
.antMatchers("/api/book/").authenticated()
.antMatchers("/api/library").authenticated()
.antMatchers(HttpMethod.POST,"/api/book").hasRole("ADMIN")
.antMatchers(HttpMethod.DELETE,"/api/book/").hasRole("ADMIN")
.and()
.formLogin()
.passwordParameter("password")
.usernameParameter("username")
.and()
.logout().deleteCookies("JSESSIONID").invalidateHttpSession(true)
.permitAll()
.and()
.httpBasic()
.and()
.csrf().disable()
.cors();
}
//MvcConfig
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**/").allowedOrigins("");
}