-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e20a2b8
commit a8f2052
Showing
18 changed files
with
343 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
modules/core/src/main/java/com/bytedesk/core/black/access/VisitorAccessInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.bytedesk.core.black.access; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
@Component | ||
public class VisitorAccessInterceptor implements HandlerInterceptor { | ||
|
||
@Autowired | ||
private VisitorAccessService visitorAccessService; | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
// 从请求中获取visitor ID | ||
String visitorId = request.getHeader("X-Visitor-ID"); | ||
if (visitorId == null) { | ||
visitorId = request.getParameter("visitorId"); | ||
} | ||
|
||
// 检查访问权限 | ||
if (visitorId != null && !visitorAccessService.isAllowed(visitorId)) { | ||
response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access denied for visitor: " + visitorId); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
modules/core/src/main/java/com/bytedesk/core/black/access/VisitorAccessService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* @Author: jackning 270580156@qq.com | ||
* @Date: 2025-01-15 14:08:50 | ||
* @LastEditors: jackning 270580156@qq.com | ||
* @LastEditTime: 2025-01-15 14:14:26 | ||
* @Description: bytedesk.com https://github.com/Bytedesk/bytedesk | ||
* Please be aware of the BSL license restrictions before installing Bytedesk IM – | ||
* selling, reselling, or hosting Bytedesk IM as a service is a breach of the terms and automatically terminates your rights under the license. | ||
* Business Source License 1.1: https://github.com/Bytedesk/bytedesk/blob/main/LICENSE | ||
* contact: 270580156@qq.com | ||
* | ||
* Copyright (c) 2025 by bytedesk.com, All Rights Reserved. | ||
*/ | ||
package com.bytedesk.core.black.access; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class VisitorAccessService { | ||
|
||
private static final String BLOCKED_VISITOR_KEY = "bytedesk:blocked:visitor:"; | ||
|
||
@Autowired | ||
private RedisTemplate<String, String> redisTemplate; | ||
|
||
public boolean isAllowed(String visitorId) { | ||
return !Boolean.TRUE.equals(redisTemplate.hasKey(BLOCKED_VISITOR_KEY + visitorId)); | ||
} | ||
|
||
public void blockVisitor(String visitorId) { | ||
redisTemplate.opsForValue().set(BLOCKED_VISITOR_KEY + visitorId, "blocked"); | ||
} | ||
|
||
public void unblockVisitor(String visitorId) { | ||
redisTemplate.delete(BLOCKED_VISITOR_KEY + visitorId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.