Browse Source

按域名关闭功能

master
nili 5 months ago
parent
commit
f1ea75b5f8
  1. 4
      game-service/src/main/java/awesome/group/game/service/cache/CacheKey.java
  2. 19
      game-service/src/main/java/awesome/group/game/service/cache/JedisManager.java
  3. 8
      game-web/src/main/java/awesome/group/game/web/RequestContext.java
  4. 23
      game-web/src/main/java/awesome/group/game/web/filter/AdminLoginFilter.java

4
game-service/src/main/java/awesome/group/game/service/cache/CacheKey.java

@ -15,4 +15,8 @@ public class CacheKey {
public static final String activateTryTimes(Integer uid) {
return String.format("vip_activate_times_%s", uid);
}
public static final String disableChannel() {
return "disable_channel";
}
}

19
game-service/src/main/java/awesome/group/game/service/cache/JedisManager.java

@ -145,6 +145,25 @@ public class JedisManager {
}
}
public void sadd(String key, String... value) {
try (Jedis jedis = getJedis()) {
jedis.sadd(key, value);
}
}
public void srem(String key, String... value) {
try (Jedis jedis = getJedis()) {
jedis.srem(key, value);
}
}
public boolean sIsMember(String key, String val) {
try (Jedis jedis = getJedis()) {
return jedis.sismember(key, val);
}
}
public long incr(String key) {
Assert.notNull(key, "key is null");
try (Jedis jedis = getJedis()) {

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

@ -62,16 +62,16 @@ public class RequestContext {
if (serverName.equalsIgnoreCase("localhost")) {
return "qiji";
}
if(serverName.startsWith("luigi")) {
if (serverName.startsWith("luigi")) {
return "qiji";
}
if(serverName.equalsIgnoreCase("www.maitiancom.com")) {
if (serverName.equalsIgnoreCase("www.maitiancom.com")) {
return "lihao";
}
if(serverName.startsWith("lihao")) {
if (serverName.startsWith("lihao")) {
return "lihao";
}
if(serverName.equalsIgnoreCase("admin.xianjiayaokeji.top")) {
if (serverName.equalsIgnoreCase("admin.xianjiayaokeji.top")) {
return "jiayao";
}
int port = request.getServerPort();

23
game-web/src/main/java/awesome/group/game/web/filter/AdminLoginFilter.java

@ -1,5 +1,7 @@
package awesome.group.game.web.filter;
import awesome.group.game.service.cache.CacheKey;
import awesome.group.game.service.cache.JedisManager;
import awesome.group.game.web.RequestContext;
import awesome.group.game.service.common.log.L;
import awesome.group.game.service.common.response.R;
@ -11,6 +13,7 @@ import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import java.io.IOException;
@ -22,6 +25,9 @@ import java.util.List;
@WebFilter(filterName = "AdminLoginFilter", urlPatterns = {"/api/admin/*"})
public class AdminLoginFilter implements Filter {
@Autowired
private JedisManager jedisManager;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
@ -33,7 +39,7 @@ public class AdminLoginFilter implements Filter {
try {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String token = "";
if(httpRequest.getCookies() != null) {
if (httpRequest.getCookies() != null) {
for (Cookie c : httpRequest.getCookies()) {
if (c.getName().equals("MatrixToken")) {
token = c.getValue();
@ -64,6 +70,12 @@ public class AdminLoginFilter implements Filter {
}
if (pass) {
RequestContext.initAdmin(httpRequest, (HttpServletResponse) response, adminId);
String channel = RequestContext.getRequestChannel();
String cacheKey = CacheKey.disableChannel();
if (adminId != null && adminId != 1 && jedisManager.sIsMember(cacheKey, channel)) {
channelDisable(response);
return;
}
chain.doFilter(request, response);
} else {
authFail(response);
@ -82,6 +94,15 @@ public class AdminLoginFilter implements Filter {
response.getWriter().flush();
}
private void channelDisable(ServletResponse response) throws IOException {
R<String> res = new R<>(-99999, "服务已停机", 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() {

Loading…
Cancel
Save