|
@@ -1,15 +1,21 @@
|
|
|
package com.caimei.www.controller.unlimitedApi;
|
|
|
|
|
|
import com.caimei.www.service.link.ShortLinkService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.server.ServerWebExchange;
|
|
|
import org.thymeleaf.util.StringUtils;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import java.net.URI;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* Description
|
|
@@ -18,6 +24,7 @@ import java.net.URI;
|
|
|
* @date : 2021/6/24
|
|
|
*/
|
|
|
@RestController
|
|
|
+@Slf4j
|
|
|
public class ShortLinkApi {
|
|
|
private ShortLinkService shortLinkService;
|
|
|
|
|
@@ -33,8 +40,19 @@ public class ShortLinkApi {
|
|
|
* @param response
|
|
|
*/
|
|
|
@GetMapping("/t/{link}")
|
|
|
- public void linkJump(@PathVariable String link, ServerHttpResponse response) {
|
|
|
- String jumpLink = shortLinkService.linkJump(link);
|
|
|
+ public void linkJump(@PathVariable String link, ServerWebExchange serverWebExchange, ServerHttpResponse response) {
|
|
|
+ ServerHttpRequest request = serverWebExchange.getRequest();
|
|
|
+ String ip = "";
|
|
|
+ HttpHeaders headers = request.getHeaders();
|
|
|
+ ip = String.valueOf(headers.get("x-forwarded-for"));
|
|
|
+ if (StringUtils.isEmpty(ip)) {
|
|
|
+ ip = String.valueOf(headers.get("X-Real-IP"));
|
|
|
+ }
|
|
|
+ if (null != ip && "" != ip) {
|
|
|
+ ip = ip.replaceAll("[^\\d.]", "");
|
|
|
+ }
|
|
|
+ log.info("访问短链接Ip--------------------------------------》" + ip + "link------------------------------->" + link);
|
|
|
+ String jumpLink = shortLinkService.linkJump(link, ip);
|
|
|
response.setStatusCode(HttpStatus.FOUND);
|
|
|
if (StringUtils.isEmpty(jumpLink)) {
|
|
|
response.getHeaders().setLocation(URI.create("https://www.caimei365.com/404.html"));
|