Commit ca00070b authored by zengtianlai3's avatar zengtianlai3

接口调整,跨域问题真正解决

parent 1115ec57
...@@ -12,16 +12,12 @@ public class CorsConfig implements WebMvcConfigurer { ...@@ -12,16 +12,12 @@ public class CorsConfig implements WebMvcConfigurer {
@Override @Override
public void addCorsMappings(CorsRegistry registry) { public void addCorsMappings(CorsRegistry registry) {
// 设置允许跨域的路径
registry.addMapping("/**") registry.addMapping("/**")
// 设置允许跨域请求的域名
.allowedOriginPatterns("*") .allowedOriginPatterns("*")
// 是否允许证书 不再默认开启
.allowCredentials(true)
// 设置允许的方法
.allowedMethods("*") .allowedMethods("*")
// 跨域允许时间 .allowCredentials(true)
.maxAge(3600); .maxAge(3600)
.allowedHeaders("*");
} }
@Override @Override
......
package iot.sixiang.license.controller;
import iot.sixiang.license.entity.Monitor;
import iot.sixiang.license.model.ResResult;
import iot.sixiang.license.service.MonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
/**
* <p>
* 前端控制器
* </p>
*
* @author m33
* @since 2022-06-10
*/
@RestController
@RequestMapping("/iot_license/monitor")
public class MonitorController {
@Autowired
private MonitorService monitorService;
@PostMapping("add")
public boolean addMonitor(){
return monitorService.addMonitor(20);
}
@GetMapping("list")
public ResResult getMonitorList(@RequestParam("type") int type) {
HashMap<String,List<Integer>> monitorList = monitorService.getMonitorList(type);
return ResResult.success().record(monitorList);
}
}
package iot.sixiang.license.controller; package iot.sixiang.license.controller;
import com.alibaba.fastjson.JSONObject;
import iot.sixiang.license.model.ResResult; import iot.sixiang.license.model.ResResult;
import iot.sixiang.license.model.SamMonitor; import iot.sixiang.license.model.SamMonitor;
import iot.sixiang.license.operate.OperateManager; import iot.sixiang.license.operate.OperateManager;
import iot.sixiang.license.service.AlarmService; import iot.sixiang.license.service.MonitorService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList; import java.util.HashMap;
import java.util.List; import java.util.List;
@Slf4j @Slf4j
...@@ -22,49 +18,27 @@ public class OperateController { ...@@ -22,49 +18,27 @@ public class OperateController {
@Autowired @Autowired
private OperateManager operateManager; private OperateManager operateManager;
@Autowired @Autowired
private AlarmService alarmService; private MonitorService monitorService;
@GetMapping("monitor/server") @GetMapping("monitor/server")
public ResResult getDeviceTypes() { public ResResult getDeviceTypes() {
// List<JSONObject> list = new ArrayList<>();
// JSONObject obj = new JSONObject();
// obj.put("serverIp", "192.168.1.11");
// obj.put("samCount", 100);
// obj.put("onlineCount",55);
// list.add(obj);
// JSONObject obj2 = new JSONObject();
// obj2.put("serverIp", "192.168.1.12");
// obj2.put("samCount", 100);
// obj2.put("onlineCount",55);
// list.add(obj2);
List<SamMonitor> records = operateManager.getSamMonitorList(); List<SamMonitor> records = operateManager.getSamMonitorList();
return ResResult.success().record(records); return ResResult.success().record(records);
} }
@PostMapping("monitor/add")
public boolean addMonitor() {
return monitorService.addMonitor(20);
}
@GetMapping("monitor/qps") @GetMapping("monitor/qps")
public ResResult getQps(@RequestParam("type") int type) { public ResResult getQps(@RequestParam("type") int type) {
List<JSONObject> list = new ArrayList<>(); HashMap<String, List<Integer>> monitorList = monitorService.getMonitorList(type);
JSONObject obj = new JSONObject(); return ResResult.success().record(monitorList);
obj.put("x", 1);
obj.put("y", 2);
list.add(obj);
JSONObject obj2 = new JSONObject();
obj2.put("x", 2);
obj2.put("y", 2);
list.add(obj2);
JSONObject obj3 = new JSONObject();
obj2.put("x", 3);
obj2.put("y", 3);
list.add(obj3);
return ResResult.success().record(list);
} }
} }
...@@ -5,6 +5,7 @@ import com.auth0.jwt.interfaces.Claim; ...@@ -5,6 +5,7 @@ import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT; import com.auth0.jwt.interfaces.DecodedJWT;
import iot.sixiang.license.model.ResResult; import iot.sixiang.license.model.ResResult;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import javax.servlet.*; import javax.servlet.*;
import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebFilter;
...@@ -30,12 +31,10 @@ public class JwtFilter implements Filter { ...@@ -30,12 +31,10 @@ public class JwtFilter implements Filter {
final HttpServletRequest request = (HttpServletRequest) servletRequest; final HttpServletRequest request = (HttpServletRequest) servletRequest;
final HttpServletResponse response = (HttpServletResponse) servletResponse; final HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setContentType("text/html; charset=utf-8"); response.setContentType("text/html; charset=utf-8");
response.setHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Credentials", "true");
response.addHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH");
if ("OPTIONS".equals(request.getMethod())) { if ("OPTIONS".equals(request.getMethod())) {
response.setStatus(HttpServletResponse.SC_OK); response.setStatus(HttpServletResponse.SC_OK);
filterChain.doFilter(request, response);
return;
} }
String token = request.getHeader("authorization"); String token = request.getHeader("authorization");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment