1. 新增小程序码和小程序二维码接口
2. 优化gateway中oauth2异常时跨域处理
This commit is contained in:
parent
4fadcb5b8e
commit
7d12de1fe6
@ -34,6 +34,12 @@
|
|||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- openfein -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-integration</artifactId>
|
<artifactId>spring-boot-starter-integration</artifactId>
|
||||||
|
|||||||
@ -5,12 +5,14 @@ import org.springframework.boot.SpringApplication;
|
|||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
|
||||||
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
|
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
|
||||||
@EnableDiscoveryClient
|
@EnableDiscoveryClient
|
||||||
@ComponentScan("com.evotech.hd.**")
|
@ComponentScan("com.evotech.hd.**")
|
||||||
@MapperScan({"com.evotech.hd.cloud.dao.**", "com.evotech.hd.common.core.dao.**"})
|
@MapperScan({"com.evotech.hd.cloud.dao.**", "com.evotech.hd.common.core.dao.**"})
|
||||||
|
@EnableFeignClients
|
||||||
public class CloudManageServerApplication {
|
public class CloudManageServerApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@ -22,6 +22,7 @@ import io.swagger.v3.oas.annotations.Hidden;
|
|||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
|
||||||
@ -78,4 +79,20 @@ public class BatteryStationController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Operation(summary = "获取小程序码")
|
||||||
|
@GetMapping("/xcx/qrcode/get")
|
||||||
|
@ApiOperationSupport(order = 6)
|
||||||
|
public void getQRCode2(String path, @RequestParam(required = false) String width, @RequestParam(required = false)String env_version, HttpServletResponse response) {
|
||||||
|
batteryStationService.getQRCode2(path, width, env_version, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Operation(summary = "获取小程序二维码")
|
||||||
|
@GetMapping("/xcx/qrcode/create")
|
||||||
|
@ApiOperationSupport(order = 7)
|
||||||
|
public void createQRCode2(String path, @RequestParam(required = false) String width, HttpServletResponse response) {
|
||||||
|
batteryStationService.createQRCode2(path, width, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,8 @@ import com.evotech.hd.common.core.entity.Result;
|
|||||||
import com.evotech.hd.common.core.entity.cloud.BatteryStation;
|
import com.evotech.hd.common.core.entity.cloud.BatteryStation;
|
||||||
import com.evotech.hd.common.core.entity.cloud.vo.BatteryStationVO;
|
import com.evotech.hd.common.core.entity.cloud.vo.BatteryStationVO;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
public interface BatteryStationService {
|
public interface BatteryStationService {
|
||||||
|
|
||||||
public Result<Integer> add(BatteryStation bs);
|
public Result<Integer> add(BatteryStation bs);
|
||||||
@ -22,4 +24,8 @@ public interface BatteryStationService {
|
|||||||
|
|
||||||
public Result<Map<String, String>> RsaSecretKey(String stationCode);
|
public Result<Map<String, String>> RsaSecretKey(String stationCode);
|
||||||
|
|
||||||
|
public void getQRCode2(String path, String width, String env_version, HttpServletResponse response);
|
||||||
|
|
||||||
|
public void createQRCode2(String path, String width, HttpServletResponse response);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
package com.evotech.hd.cloud.service.impl;
|
package com.evotech.hd.cloud.service.impl;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
import java.security.KeyPair;
|
import java.security.KeyPair;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -8,6 +11,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
@ -19,6 +23,7 @@ import com.evotech.hd.cloud.dao.BatteryStationSecretKeyDao;
|
|||||||
import com.evotech.hd.cloud.dao.VehicleInfoDao;
|
import com.evotech.hd.cloud.dao.VehicleInfoDao;
|
||||||
import com.evotech.hd.cloud.entity.BatteryStationSecretKey;
|
import com.evotech.hd.cloud.entity.BatteryStationSecretKey;
|
||||||
import com.evotech.hd.cloud.entity.request.PageListBatteryStationRequest;
|
import com.evotech.hd.cloud.entity.request.PageListBatteryStationRequest;
|
||||||
|
import com.evotech.hd.cloud.rpc.WechatService;
|
||||||
import com.evotech.hd.cloud.service.BatteryStationService;
|
import com.evotech.hd.cloud.service.BatteryStationService;
|
||||||
import com.evotech.hd.common.core.entity.Result;
|
import com.evotech.hd.common.core.entity.Result;
|
||||||
import com.evotech.hd.common.core.entity.cloud.BatteryStation;
|
import com.evotech.hd.common.core.entity.cloud.BatteryStation;
|
||||||
@ -26,9 +31,14 @@ import com.evotech.hd.common.core.entity.cloud.VehicleInfo;
|
|||||||
import com.evotech.hd.common.core.entity.cloud.vo.BatteryStationVO;
|
import com.evotech.hd.common.core.entity.cloud.vo.BatteryStationVO;
|
||||||
import com.evotech.hd.common.core.enums.CodeMsg;
|
import com.evotech.hd.common.core.enums.CodeMsg;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DatePattern;
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import cn.hutool.core.io.IoUtil;
|
||||||
import cn.hutool.crypto.KeyUtil;
|
import cn.hutool.crypto.KeyUtil;
|
||||||
import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
|
import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import jakarta.servlet.ServletOutputStream;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class BatteryStationServiceImpl implements BatteryStationService {
|
public class BatteryStationServiceImpl implements BatteryStationService {
|
||||||
@ -39,6 +49,8 @@ public class BatteryStationServiceImpl implements BatteryStationService {
|
|||||||
private BatteryStationSecretKeyDao batteryStationSecretKeyDao;
|
private BatteryStationSecretKeyDao batteryStationSecretKeyDao;
|
||||||
@Resource
|
@Resource
|
||||||
private VehicleInfoDao vehicleInfoDao;
|
private VehicleInfoDao vehicleInfoDao;
|
||||||
|
@Resource
|
||||||
|
private WechatService wechatService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Result<Integer> add(BatteryStation bs) {
|
public Result<Integer> add(BatteryStation bs) {
|
||||||
@ -152,4 +164,54 @@ public class BatteryStationServiceImpl implements BatteryStationService {
|
|||||||
return new Result<List<BatteryStationVO>>().success(res);
|
return new Result<List<BatteryStationVO>>().success(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void getQRCode2(String path, String width, String env_version, HttpServletResponse response) {
|
||||||
|
String qrCode2Str = wechatService.getQRCode2(path, width, env_version);
|
||||||
|
byte[] res = Base64.getDecoder().decode(qrCode2Str);
|
||||||
|
String fileName = "小程序码" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + ".png";
|
||||||
|
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
||||||
|
try {
|
||||||
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
ServletOutputStream out = null;
|
||||||
|
try {
|
||||||
|
out = response.getOutputStream();
|
||||||
|
out.write(res);
|
||||||
|
out.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
IoUtil.close(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void createQRCode2(String path, String width, HttpServletResponse response) {
|
||||||
|
String qrCode2Str = wechatService.createQRCode2(path, width);
|
||||||
|
byte[] res = Base64.getDecoder().decode(qrCode2Str);
|
||||||
|
String fileName = "小程序二维码" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + ".png";
|
||||||
|
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
||||||
|
try {
|
||||||
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
ServletOutputStream out = null;
|
||||||
|
try {
|
||||||
|
out = response.getOutputStream();
|
||||||
|
out.write(res);
|
||||||
|
out.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
IoUtil.close(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
package com.evotech.hd.gateway.oauth2;
|
package com.evotech.hd.gateway.oauth2;
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import org.springframework.core.io.buffer.DataBuffer;
|
import org.springframework.core.io.buffer.DataBuffer;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
@ -40,12 +39,15 @@ public class MyAuthenticationEntryPoint implements ServerAuthenticationEntryPoin
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e) {
|
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e) {
|
||||||
|
// System.out.println("0999990000");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
ServerHttpResponse response = exchange.getResponse();
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
Throwable cause = e.getCause();
|
Throwable cause = e.getCause();
|
||||||
Result<String> res = new Result<String>().exception(e.getMessage());
|
Result<String> res = new Result<String>().exception(e.getMessage());
|
||||||
|
// System.out.println("00");
|
||||||
try {
|
try {
|
||||||
if (cause instanceof JwtValidationException) {
|
if (cause instanceof JwtValidationException) {
|
||||||
|
// System.out.println("11");
|
||||||
if (cause.getMessage().contains("Jwt expired at")) {
|
if (cause.getMessage().contains("Jwt expired at")) {
|
||||||
String token = exchange.getRequest().getHeaders().getFirst("Authorization").substring(7);
|
String token = exchange.getRequest().getHeaders().getFirst("Authorization").substring(7);
|
||||||
String dateTime = DateUtil.formatDateTime(TokenUtil.getExp(token));
|
String dateTime = DateUtil.formatDateTime(TokenUtil.getExp(token));
|
||||||
@ -54,8 +56,10 @@ public class MyAuthenticationEntryPoint implements ServerAuthenticationEntryPoin
|
|||||||
res = new Result<String>().error(CodeMsg.TOKEN_INVALID.getCode(), CodeMsg.TOKEN_INVALID.getMsg(), cause.getMessage());
|
res = new Result<String>().error(CodeMsg.TOKEN_INVALID.getCode(), CodeMsg.TOKEN_INVALID.getMsg(), cause.getMessage());
|
||||||
}
|
}
|
||||||
} else if (cause instanceof BadJwtException || cause instanceof JwtEncodingException) {
|
} else if (cause instanceof BadJwtException || cause instanceof JwtEncodingException) {
|
||||||
|
// System.out.println("22");
|
||||||
res = new Result<String>().error(CodeMsg.TOKEN_INVALID.getCode(), CodeMsg.TOKEN_INVALID.getMsg(), cause.getMessage());
|
res = new Result<String>().error(CodeMsg.TOKEN_INVALID.getCode(), CodeMsg.TOKEN_INVALID.getMsg(), cause.getMessage());
|
||||||
} else if (cause instanceof InvalidBearerTokenException) {
|
} else if (cause instanceof InvalidBearerTokenException) {
|
||||||
|
// System.out.println("33");
|
||||||
String token = exchange.getRequest().getHeaders().getFirst("Authorization").substring(7);
|
String token = exchange.getRequest().getHeaders().getFirst("Authorization").substring(7);
|
||||||
String dateTime = DateUtil.formatDateTime(TokenUtil.getExp(token));
|
String dateTime = DateUtil.formatDateTime(TokenUtil.getExp(token));
|
||||||
res = new Result<String>().bussinessException(CodeMsg.TOKEN_EXPIRED.getCode(), CodeMsg.TOKEN_EXPIRED.getMsg(), "Jwt expired at " + dateTime);
|
res = new Result<String>().bussinessException(CodeMsg.TOKEN_EXPIRED.getCode(), CodeMsg.TOKEN_EXPIRED.getMsg(), "Jwt expired at " + dateTime);
|
||||||
@ -63,19 +67,22 @@ public class MyAuthenticationEntryPoint implements ServerAuthenticationEntryPoin
|
|||||||
res = new Result<String>().error(CodeMsg.AUTHENTICATION_FAILED.getCode(), CodeMsg.AUTHENTICATION_FAILED.getMsg(), e.getMessage());
|
res = new Result<String>().error(CodeMsg.AUTHENTICATION_FAILED.getCode(), CodeMsg.AUTHENTICATION_FAILED.getMsg(), e.getMessage());
|
||||||
}
|
}
|
||||||
} catch (Exception e1) {
|
} catch (Exception e1) {
|
||||||
|
// System.out.println("99");
|
||||||
log.info(e1.toString());
|
log.info(e1.toString());
|
||||||
res = new Result<String>().error(CodeMsg.AUTHENTICATION_FAILED.getCode(), CodeMsg.AUTHENTICATION_FAILED.getMsg(), e.getMessage());
|
res = new Result<String>().error(CodeMsg.AUTHENTICATION_FAILED.getCode(), CodeMsg.AUTHENTICATION_FAILED.getMsg(), e.getMessage());
|
||||||
}
|
}
|
||||||
|
// System.out.println("100");
|
||||||
response.setStatusCode(HttpStatus.OK);
|
response.setStatusCode(HttpStatus.OK);
|
||||||
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
||||||
URI uri = exchange.getRequest().getURI();
|
// URI uri = exchange.getRequest().getURI();
|
||||||
// 为了解决token过期时,前端不出现跨域错误,添加了一些header,注意Access-Control-Allow-Origin的值
|
// 为了解决token过期时,前端不出现跨域错误,添加了一些header,注意Access-Control-Allow-Origin的值: uri.getScheme() + "://" + uri.getHost()
|
||||||
response.getHeaders().add("Access-Control-Allow-Origin", uri.getScheme() + "://" + uri.getHost());
|
response.getHeaders().add("Access-Control-Allow-Origin", "*");
|
||||||
response.getHeaders().add("Access-Control-Allow-Credentials", "true");
|
response.getHeaders().add("Access-Control-Allow-Credentials", "true");
|
||||||
response.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
|
response.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
|
||||||
response.getHeaders().add("Access-Control-Allow-Headers", HttpHeaders.AUTHORIZATION);
|
response.getHeaders().add("Access-Control-Allow-Headers", HttpHeaders.AUTHORIZATION);
|
||||||
String body = JSONUtil.toJsonStr(res);
|
String body = JSONUtil.toJsonStr(res);
|
||||||
DataBuffer buffer = response.bufferFactory().wrap(body.getBytes(Charset.forName("UTF-8")));
|
DataBuffer buffer = response.bufferFactory().wrap(body.getBytes(Charset.forName("UTF-8")));
|
||||||
|
// System.out.println("200");
|
||||||
return response.writeWith(Mono.just(buffer));
|
return response.writeWith(Mono.just(buffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -51,6 +51,7 @@ public class GlobalExceptionHandler extends DefaultErrorWebExceptionHandler {
|
|||||||
protected Mono<ServerResponse> renderErrorResponse(ServerRequest request) {
|
protected Mono<ServerResponse> renderErrorResponse(ServerRequest request) {
|
||||||
// Map<String, Object> error = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
|
// Map<String, Object> error = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
|
||||||
Throwable throwable = getError(request);
|
Throwable throwable = getError(request);
|
||||||
|
System.out.println("1111666");
|
||||||
return ServerResponse
|
return ServerResponse
|
||||||
// .status(super.getHttpStatus(error))
|
// .status(super.getHttpStatus(error))
|
||||||
.status(HttpStatus.OK)
|
.status(HttpStatus.OK)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user