This commit is contained in:
jack ning
2025-01-17 16:13:53 +08:00
parent 980c71b96d
commit d3349c8d3e
6 changed files with 130 additions and 32 deletions

View File

@@ -2,7 +2,7 @@
* @Author: jackning 270580156@qq.com
* @Date: 2024-01-26 15:28:57
* @LastEditors: jackning 270580156@qq.com
* @LastEditTime: 2025-01-15 14:13:05
* @LastEditTime: 2025-01-17 15:58:29
* @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.
@@ -55,11 +55,11 @@ public class WebMvcConfig implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry) {
// 注册IP访问拦截器
registry.addInterceptor(ipAccessInterceptor)
.addPathPatterns("/**");
.addPathPatterns("/visitor/**"); // 仅拦截visitor相关接口如需拦截其他接口请直接在此数组中添加即可
// 注册Visitor访问拦截器只拦截visitor相关接口
registry.addInterceptor(visitorAccessInterceptor)
.addPathPatterns("/visitor/**");
.addPathPatterns("/visitor/**"); // 仅拦截visitor相关接口如需拦截其他接口请直接在此数组中添加即可
}
/**

View File

@@ -2,7 +2,7 @@
* @Author: jackning 270580156@qq.com
* @Date: 2024-12-24 22:23:17
* @LastEditors: jackning 270580156@qq.com
* @LastEditTime: 2025-01-17 15:35:26
* @LastEditTime: 2025-01-17 16:08:54
* @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.
@@ -13,27 +13,27 @@
*/
package com.bytedesk.core.ip.white;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
@Component
@RequiredArgsConstructor
public class IpWhitelistInitializer {
@Autowired
private IpWhitelistRepository ipWhitelistRepository;
private final IpWhitelistRestService ipWhitelistRestService;
@PostConstruct
public void init() {
if (ipWhitelistRepository.count() > 0) {
if (ipWhitelistRestService.count() > 0) {
return;
}
// 初始化白名单
IpWhitelistEntity ipWhitelist = IpWhitelistEntity.builder()
IpWhitelistRequest ipWhitelistRequest = IpWhitelistRequest.builder()
.ip("127.0.0.1")
.description("127.0.0.1")
.build();
ipWhitelistRepository.save(ipWhitelist);
ipWhitelistRestService.create(ipWhitelistRequest);
}
}

View File

@@ -1,8 +1,28 @@
/*
* @Author: jackning 270580156@qq.com
* @Date: 2024-12-24 17:49:18
* @LastEditors: jackning 270580156@qq.com
* @LastEditTime: 2025-01-17 16:09:56
* @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.ip.white;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface IpWhitelistRepository extends JpaRepository<IpWhitelistEntity, Long> {
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface IpWhitelistRepository extends JpaRepository<IpWhitelistEntity, Long>, JpaSpecificationExecutor<IpWhitelistEntity> {
boolean existsByIp(String ip);
}
Optional<IpWhitelistEntity> findByIp(String ip);
Optional<IpWhitelistEntity> findByUid(String uid);
}

View File

@@ -2,7 +2,7 @@
* @Author: jackning 270580156@qq.com
* @Date: 2025-01-17 15:26:44
* @LastEditors: jackning 270580156@qq.com
* @LastEditTime: 2025-01-17 15:28:25
* @LastEditTime: 2025-01-17 16:06:13
* @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.
@@ -15,6 +15,18 @@ package com.bytedesk.core.ip.white;
import com.bytedesk.core.base.BaseRequest;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = true)
@Builder
public class IpWhitelistRequest extends BaseRequest {
private String ip;
private String description;
}

View File

@@ -2,7 +2,7 @@
* @Author: jackning 270580156@qq.com
* @Date: 2025-01-17 15:26:54
* @LastEditors: jackning 270580156@qq.com
* @LastEditTime: 2025-01-17 15:26:57
* @LastEditTime: 2025-01-17 16:06:51
* @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.
@@ -15,6 +15,16 @@ package com.bytedesk.core.ip.white;
import com.bytedesk.core.base.BaseResponse;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = true)
@Builder
public class IpWhitelistResponse extends BaseResponse {
private String ip;
private String description;
}

View File

@@ -2,7 +2,7 @@
* @Author: jackning 270580156@qq.com
* @Date: 2024-12-24 22:18:54
* @LastEditors: jackning 270580156@qq.com
* @LastEditTime: 2025-01-17 15:36:36
* @LastEditTime: 2025-01-17 16:08:31
* @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.
@@ -16,25 +16,60 @@ package com.bytedesk.core.ip.white;
import org.springframework.stereotype.Service;
import com.bytedesk.core.base.BaseRestService;
import com.bytedesk.core.rbac.auth.AuthService;
import com.bytedesk.core.rbac.user.UserEntity;
import com.bytedesk.core.uid.UidUtils;
import java.util.Optional;
import org.modelmapper.ModelMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.orm.ObjectOptimisticLockingFailureException;
import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
public class IpWhitelistRestService extends BaseRestService<IpWhitelistEntity, IpWhitelistRequest, IpWhitelistResponse> {
private final IpWhitelistRepository ipWhitelistRepository;
private final ModelMapper modelMapper;
private final UidUtils uidUtils;
private final AuthService authService;
@Override
public Page<IpWhitelistResponse> queryByOrg(IpWhitelistRequest request) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'queryByOrg'");
Pageable pageable = PageRequest.of(request.getPageNumber(), request.getPageSize(), Sort.Direction.ASC, "id");
Specification<IpWhitelistEntity> spec = IpWhitelistSpecification.search(request);
Page<IpWhitelistEntity> ipWhitelistPage = ipWhitelistRepository.findAll(spec, pageable);
return ipWhitelistPage.map(this::convertToResponse);
}
@Cacheable(value = "ipWhitelist", key = "#ip", unless = "#result == null")
public Optional<IpWhitelistEntity> findByIp(String ip) {
return ipWhitelistRepository.findByIp(ip);
}
@Override
public Page<IpWhitelistResponse> queryByUser(IpWhitelistRequest request) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'queryByUser'");
// 获取当前用户
UserEntity currentUser = authService.getUser();
if (currentUser == null) {
throw new RuntimeException("user is null");
}
Pageable pageable = PageRequest.of(request.getPageNumber(), request.getPageSize(), Sort.Direction.ASC, "id");
Specification<IpWhitelistEntity> spec = IpWhitelistSpecification.search(request);
Page<IpWhitelistEntity> ipWhitelistPage = ipWhitelistRepository.findAll(spec, pageable);
return ipWhitelistPage.map(this::convertToResponse);
}
@Override
@@ -45,32 +80,54 @@ public class IpWhitelistRestService extends BaseRestService<IpWhitelistEntity, I
@Override
public IpWhitelistResponse create(IpWhitelistRequest request) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'create'");
Optional<IpWhitelistEntity> ipWhitelist = findByIp(request.getIp());
if (ipWhitelist.isPresent()) {
throw new RuntimeException("ipWhitelist is present");
}
IpWhitelistEntity ipWhitelistEntity = modelMapper.map(request, IpWhitelistEntity.class);
ipWhitelistEntity.setUid(uidUtils.getUid());
// 保存
IpWhitelistEntity savedIpWhitelistEntity = ipWhitelistRepository.save(ipWhitelistEntity);
if (savedIpWhitelistEntity == null) {
throw new RuntimeException("ipWhitelist is null");
}
return convertToResponse(savedIpWhitelistEntity);
}
@Override
public IpWhitelistResponse update(IpWhitelistRequest request) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'update'");
Optional<IpWhitelistEntity> ipWhitelist = findByUid(request.getUid());
if (!ipWhitelist.isPresent()) {
throw new RuntimeException("ipWhitelist is not present");
}
IpWhitelistEntity ipWhitelistEntity = modelMapper.map(request, IpWhitelistEntity.class);
return convertToResponse(ipWhitelistRepository.save(ipWhitelistEntity));
}
public long count() {
return ipWhitelistRepository.count();
}
@Override
public IpWhitelistEntity save(IpWhitelistEntity entity) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'save'");
try {
return ipWhitelistRepository.save(entity);
} catch (Exception e) {
throw new RuntimeException("ipWhitelist is null");
}
}
@Override
public void deleteByUid(String uid) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'deleteByUid'");
Optional<IpWhitelistEntity> ipWhitelist = findByUid(uid);
if (ipWhitelist.isPresent()) {
ipWhitelistRepository.delete(ipWhitelist.get());
}
}
@Override
public void delete(IpWhitelistRequest request) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'delete'");
deleteByUid(request.getUid());
}
@Override
@@ -82,8 +139,7 @@ public class IpWhitelistRestService extends BaseRestService<IpWhitelistEntity, I
@Override
public IpWhitelistResponse convertToResponse(IpWhitelistEntity entity) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'convertToResponse'");
return modelMapper.map(entity, IpWhitelistResponse.class);
}
}