Merge branch '15-enable-cors-for-frontend-interaction' into 'dev'

Resolve "Enable CORS for frontend interaction"

Closes #15

See merge request sa4-2020/the-sanmarinoes/backend!18
This commit is contained in:
Claudio Maggioni 2020-03-03 11:41:54 +01:00
commit e43a913ff3
4 changed files with 38 additions and 14 deletions

View file

@ -0,0 +1,32 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.config;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
/**
* Add CORS headers to each response in order to please the frontend requests, coming from a
* different host for now (thanks to the difference in ports). Andrea would you please stop
* complaining now
*/
@Component
public class CORSFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "HEAD, PUT, POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(req, res);
}
@Override
public void init(FilterConfig filterConfig) {}
@Override
public void destroy() {}
}

View file

@ -68,8 +68,9 @@ public class SpringFoxConfig {
*/ */
private Predicate<String> paths() { private Predicate<String> paths() {
return regexPredicate("/auth.*") return regexPredicate("/auth.*")
.or(regexPredicate("/room.*")) .or(regexPredicate("/room.*"))
.or(regexPredicate("/register.*")); .or(regexPredicate("/register.*"))
.or(regexPredicate("/"));
} }
/** /**

View file

@ -18,7 +18,6 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@RestController @RestController
@CrossOrigin
@RequestMapping("/auth") @RequestMapping("/auth")
public class AuthenticationController { public class AuthenticationController {

View file

@ -1,23 +1,15 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*;
import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@RestController @RestController
@EnableAutoConfiguration @EnableAutoConfiguration
// @Mapping("/light")
public class WelcomeController { public class WelcomeController {
@GetMapping @GetMapping
List<Device> testDevices() { ResponseEntity<Void> testConnection() {
return Arrays.asList( return ResponseEntity.ok(null);
new KnobDimmer(),
new RegularLight(),
new MotionSensor(),
new Sensor(),
new SmartPlug(),
new Switch());
} }
} }