Browse Source

matrix接口

master
nili 8 months ago
parent
commit
ef6a196b45
  1. 23
      game-dao/src/main/java/awesome/group/game/dao/bean/MatrixAdvRecord.java
  2. 11
      game-dao/src/main/java/awesome/group/game/dao/mapper/MatrixAdvRecordMapper.java
  3. 34
      game-service/src/main/java/awesome/group/game/service/MatrixService.java
  4. 12
      game-service/src/main/java/awesome/group/game/service/bo/MatrixAdvRecordBo.java
  5. 3
      game-web/src/main/java/awesome/group/RequestContext.java
  6. 25
      game-web/src/main/java/awesome/group/controller/MatrixController.java
  7. 85
      game-web/src/main/java/awesome/group/filter/LoginFilter.java
  8. 29
      game-web/src/main/java/awesome/group/interceptor/InterceptorConfig.java
  9. 45
      game-web/src/main/java/awesome/group/interceptor/JwtInterceptor.java
  10. 6
      game-web/src/main/resources/application-default.yml

23
game-dao/src/main/java/awesome/group/game/dao/bean/MatrixAdvRecord.java

@ -0,0 +1,23 @@
package awesome.group.game.dao.bean;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.sql.Timestamp;
@Data
public class MatrixAdvRecord {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String deviceId;
private Integer appId;
private Integer platform;//1穿山甲,2腾讯,3百度联盟,4 Mintegral,5 快手,6游可赢,7 Sigmob,8 Admob
private Integer advType;//1横幅,2插页,3激励视频
private Integer ecpm;//单位:分
private String deviceBrand;
private String deviceName;
private String ip;
private Timestamp createdAt;
}

11
game-dao/src/main/java/awesome/group/game/dao/mapper/MatrixAdvRecordMapper.java

@ -0,0 +1,11 @@
package awesome.group.game.dao.mapper;
import awesome.group.game.dao.bean.MatrixAdvRecord;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
public interface MatrixAdvRecordMapper extends BaseMapper<MatrixAdvRecord> {
@Select("select id from matrix_app where code = #{code}")
Integer queryAppId(String code);
}

34
game-service/src/main/java/awesome/group/game/service/MatrixService.java

@ -0,0 +1,34 @@
package awesome.group.game.service;
import awesome.group.game.dao.bean.MatrixAdvRecord;
import awesome.group.game.dao.mapper.MatrixAdvRecordMapper;
import awesome.group.game.service.bo.MatrixAdvRecordBo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@Service
public class MatrixService {
@Resource
private MatrixAdvRecordMapper mapper;
public void saveRecord(MatrixAdvRecordBo bo) {
Assert.isTrue(StringUtils.hasText(bo.appCode), "appCode不能为空");
Integer appId = mapper.queryAppId(bo.appCode);
if (appId == null) {
return;//非法请求,直接忽略
}
MatrixAdvRecord record = new MatrixAdvRecord();
record.setDeviceId(bo.deviceId);
record.setAppId(appId);
record.setPlatform(bo.platform);
record.setAdvType(bo.advType);
record.setEcpm(bo.ecpm);
record.setDeviceBrand(bo.deviceBrand);
record.setDeviceName(bo.deviceName);
record.setIp(bo.ip);
mapper.insert(record);
}
}

12
game-service/src/main/java/awesome/group/game/service/bo/MatrixAdvRecordBo.java

@ -0,0 +1,12 @@
package awesome.group.game.service.bo;
public class MatrixAdvRecordBo {
public String deviceId;
public String appCode;
public Integer platform;//1穿山甲,2腾讯,3百度联盟,4 Mintegral,5 快手,6游可赢,7 Sigmob,8 Admob
public Integer advType;//1横幅,2插页,3激励视频
public Integer ecpm;//单位:分
public String deviceBrand;
public String deviceName;
public String ip;
}

3
game-web/src/main/java/awesome/group/RequestContext.java

@ -43,5 +43,8 @@ public class RequestContext {
return (Integer) THREAD_LOCAL.get().get(KEY_UID); return (Integer) THREAD_LOCAL.get().get(KEY_UID);
} }
public static void clear() {
THREAD_LOCAL.remove();
}
} }

25
game-web/src/main/java/awesome/group/controller/MatrixController.java

@ -0,0 +1,25 @@
package awesome.group.controller;
import awesome.group.aop.RestApi;
import awesome.group.game.service.MatrixService;
import awesome.group.game.service.bo.MatrixAdvRecordBo;
import awesome.group.game.service.common.response.R;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/game/matrix")
public class MatrixController {
@Resource
private MatrixService matrixService;
@PostMapping("/saveAdvRecord")
@RestApi
public R<Void> saveAdvRecord(@RequestBody MatrixAdvRecordBo bo) {
matrixService.saveRecord(bo);
return new R<>(R.CODE_SUCCESS, "ok", null);
}
}

85
game-web/src/main/java/awesome/group/filter/LoginFilter.java

@ -0,0 +1,85 @@
package awesome.group.filter;
import awesome.group.RequestContext;
import awesome.group.game.service.common.log.L;
import awesome.group.game.service.common.response.R;
import awesome.group.game.service.util.JwtUtils;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.google.gson.Gson;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.List;
/**
* @author nidaren
*/
@WebFilter(filterName = "ContextFilter", urlPatterns = {"/api/*"})
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
try {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String token = httpRequest.getHeader("Authorization");
List<String> openApi = List.of(
"/api/game/user/demo",
"/api/game/app/info",
"/api/game/auth/login",
"/api/game/auth/loginV2",
"/api/game/auth/loginV3",
"/api/game/matrix/saveAdvRecord",
"/api/game/auth/test");
boolean pass = false;
Integer userId = null;
try {
if (StringUtils.hasText(token)) {
userId = JwtUtils.parseToken(token);
pass = true;
}
} catch (TokenExpiredException e) {
//忽略
} catch (Exception e) {
L.trace("parseTokenError", "token:" + token, e);
}
String path = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length()).replaceAll("[/]+$", "");
for (String s : openApi) {
if (path.startsWith(s)) {
pass = true;
}
}
if (pass) {
RequestContext.init(httpRequest, (HttpServletResponse) response, userId);
chain.doFilter(request, response);
} else {
authFail(response);
}
} finally {
RequestContext.clear();
}
}
private void authFail(ServletResponse response) throws IOException {
R<String> res = new R<>(-88888, "not login ", null);
Gson gson = new Gson();
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(gson.toJson(res));
response.getWriter().flush();
}
@Override
public void destroy() {
}
}

29
game-web/src/main/java/awesome/group/interceptor/InterceptorConfig.java

@ -1,29 +0,0 @@
package awesome.group.interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration //配置类
public class InterceptorConfig implements WebMvcConfigurer {
@Bean //手动注入 JwtInterceptor 对象
public JwtInterceptor jwtInterceptor() {
return new JwtInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(jwtInterceptor())
//拦截的路径
.addPathPatterns("/**")
//不拦截的路径
.excludePathPatterns("/api/game/app/info")
.excludePathPatterns("/api/game/auth/login")
.excludePathPatterns("/api/game/auth/loginV2")
.excludePathPatterns("/api/game/auth/loginV3")
.excludePathPatterns("/api/game/auth/test");
}
}

45
game-web/src/main/java/awesome/group/interceptor/JwtInterceptor.java

@ -1,45 +0,0 @@
package awesome.group.interceptor;
import awesome.group.RequestContext;
import awesome.group.game.service.common.log.L;
import awesome.group.game.service.common.response.R;
import awesome.group.game.service.util.JwtUtils;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.google.gson.Gson;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import java.io.PrintWriter;
import java.util.List;
public class JwtInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String token = request.getHeader("Authorization");
List<String> openApi = List.of("/api/game/user/demo");
try {
if (StringUtils.hasText(token)) {
Integer userId = JwtUtils.parseToken(token);
RequestContext.init(request, response, userId);
return true;
}
} catch (TokenExpiredException e) {
//忽略
} catch (Exception e) {
L.trace("parseTokenError", "token:" + token, e);
}
String path = request.getRequestURI().substring(request.getContextPath().length()).replaceAll("[/]+$", "");
for (String s : openApi) {
if (path.startsWith(s)) {
return true;
}
}
PrintWriter out = response.getWriter();
Gson gson = new Gson();
out.print(gson.toJson(new R<>(-88888, "not log in", null)));
return false;
}
}

6
game-web/src/main/resources/application-default.yml

@ -12,9 +12,9 @@ spring:
datasource: datasource:
game: game:
type: com.zaxxer.hikari.HikariDataSource type: com.zaxxer.hikari.HikariDataSource
jdbcUrl: jdbc:mysql://47.108.240.181:3306/game?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&connectTimeout=300&socketTimeout=2000&serverTimezone=Hongkong&zeroDateTimeBehavior=convertToNull jdbcUrl: jdbc:mysql://127.0.0.1:3306/game?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&connectTimeout=300&socketTimeout=2000&serverTimezone=Hongkong&zeroDateTimeBehavior=convertToNull
username: game username: root
password: Vd12luSjmwAXuwDH password:
connectionTimeout: 300 connectionTimeout: 300
maximumPoolSize: 5 maximumPoolSize: 5
minimumIdle: 1 minimumIdle: 1

Loading…
Cancel
Save