简单认证 未完全实现

This commit is contained in:
shikong 2023-08-11 17:18:02 +08:00
parent 58d1ae1124
commit ef96d0e6ac
6 changed files with 90 additions and 10 deletions

View File

@ -1,7 +1,9 @@
package cn.skcks.docking.gb28181.core.sip.listener;
import cn.skcks.docking.gb28181.core.sip.executor.DefaultSipExecutor;
import cn.skcks.docking.gb28181.core.sip.message.event.SipSubscribe;
import cn.skcks.docking.gb28181.core.sip.message.processor.MessageProcessor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@ -15,9 +17,11 @@ import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@RequiredArgsConstructor
@Component
@Slf4j
public class SipListenerImpl implements SipListener {
private final SipSubscribe sipSubscribe;
private final ConcurrentMap<String, MessageProcessor> processor = new ConcurrentHashMap<>();
public void addProcessor(String method,MessageProcessor messageProcessor){
log.debug("[SipListener] 注册 {} 处理器", method);
@ -50,6 +54,8 @@ public class SipListenerImpl implements SipListener {
// sipRequestProcessor.process(responseEvent);
// }
// if(status != Response.UNAUTHORIZED && responseEvent.getResponse() != null){}
// if (status != Response.UNAUTHORIZED && responseEvent.getResponse() != null && sipSubscribe.getOkSubscribesSize() > 0 ) {
// CallIdHeader callIdHeader = (CallIdHeader)responseEvent.getResponse().getHeader(CallIdHeader.NAME);
// if (callIdHeader != null) {

View File

@ -117,7 +117,7 @@ public class DigestServerAuthenticationHelper {
*
* @return true if authentication succeded and false otherwise.
*/
public boolean doAuthenticateHashedPassword(Request request, String hashedPassword) {
public static boolean doAuthenticateHashedPassword(Request request, String hashedPassword) {
AuthorizationHeader authHeader = (AuthorizationHeader) request.getHeader(AuthorizationHeader.NAME);
if ( authHeader == null ) {
return false;
@ -163,7 +163,7 @@ public class DigestServerAuthenticationHelper {
*
* @return true if authentication succeded and false otherwise.
*/
public boolean doAuthenticatePlainTextPassword(Request request, String pass) {
public static boolean doAuthenticatePlainTextPassword(Request request, String pass) {
AuthorizationHeader authHeader = (AuthorizationHeader) request.getHeader(AuthorizationHeader.NAME);
if ( authHeader == null || authHeader.getRealm() == null) {
return false;

View File

@ -21,15 +21,15 @@ public class SipSubscribe {
@Qualifier(DefaultSipExecutor.EXECUTOR_BEAN_NAME)
private final Executor executor;
private SubmissionPublisher<SipEventItem> submissionPublisher;
private SubmissionPublisher<SipEventItem> publisher;
@PostConstruct
private void init(){
submissionPublisher = new SubmissionPublisher<>(executor, Flow.defaultBufferSize());
publisher = new SubmissionPublisher<>(executor, Flow.defaultBufferSize());
}
@PreDestroy
private void destroy(){
submissionPublisher.close();
publisher.close();
}
}

View File

@ -7,6 +7,8 @@ import cn.skcks.docking.gb28181.core.sip.message.auth.DigestServerAuthentication
import cn.skcks.docking.gb28181.core.sip.message.processor.MessageProcessor;
import cn.skcks.docking.gb28181.core.sip.message.sender.SipMessageSender;
import cn.skcks.docking.gb28181.core.sip.utils.SipUtil;
import cn.skcks.docking.gb28181.orm.mybatis.dynamic.model.DockingDevice;
import cn.skcks.docking.gb28181.service.docking.DockingDeviceService;
import gov.nist.javax.sip.address.SipUri;
import gov.nist.javax.sip.header.Authorization;
import gov.nist.javax.sip.message.SIPRequest;
@ -14,6 +16,7 @@ import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import javax.sip.RequestEvent;
@ -29,8 +32,8 @@ public class RegisterRequestProcessor implements MessageProcessor {
private final SipListener sipListener;
private final SipMessageSender sender;
private final SipConfig sipConfig;
private final DockingDeviceService dockingDeviceService;
@PostConstruct
private void init(){
@ -48,16 +51,33 @@ public class RegisterRequestProcessor implements MessageProcessor {
SipUri uri = (SipUri)address.getURI();
String deviceId = uri.getUser();
log.debug("请求注册 设备id => {}", deviceId);
DockingDevice deviceInfo = dockingDeviceService.getDeviceInfo(deviceId);
if(deviceInfo == null){
log.info("新注册的设备 deviceId => {}", deviceId);
}
RemoteInfo remoteInfo = SipUtil.getRemoteInfoFromRequest(request, false);
log.debug("远程连接信息 => {}", remoteInfo);
String password = sipConfig.getPassword();
Authorization authorization = request.getAuthorization();
log.debug("认证信息 => {}", authorization);
if(authorization == null && StringUtils.isNotBlank(password)){
Response response = getMessageFactory().createResponse(Response.UNAUTHORIZED, request);
DigestServerAuthenticationHelper.generateChallenge(getHeaderFactory(),response,sipConfig.getDomain());
sender.send(request.getLocalAddress().getHostAddress(),response);
return;
}
Response response = getMessageFactory().createResponse(Response.UNAUTHORIZED, request);
DigestServerAuthenticationHelper.generateChallenge(getHeaderFactory(),response,sipConfig.getDomain());
sender.send(request.getLocalAddress().getHostAddress(),response);
log.debug("认证信息 => {}", authorization);
boolean authPass = StringUtils.isBlank(password) ||
DigestServerAuthenticationHelper.doAuthenticatePlainTextPassword(request,password);
if(!authPass){
Response response = getMessageFactory().createResponse(Response.FORBIDDEN, request);
response.setReasonPhrase("认证失败");
log.info("设备注册信息认证失败 deviceId => {}", deviceId);
sender.send(request.getLocalAddress().getHostAddress(),response);
return;
}
log.debug("设备 deviceId => {}, 认证通过", deviceId);
}
}

View File

@ -0,0 +1,31 @@
package cn.skcks.docking.gb28181.service.cache;
import cn.skcks.docking.gb28181.common.json.JsonUtils;
import cn.skcks.docking.gb28181.common.redis.RedisUtil;
import cn.skcks.docking.gb28181.orm.mybatis.dynamic.model.DockingDevice;
import cn.skcks.docking.gb28181.service.docking.DockingDeviceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
@Slf4j
@RequiredArgsConstructor
@Service
public class DockingDeviceCacheService {
private final DockingDeviceService dockingDeviceService;
public static final String PREFIX = "DockingDevice";
public DockingDevice getDeviceInfo(String deviceId) {
String key = StringUtils.joinWith(":", PREFIX, deviceId);
String json = RedisUtil.StringOps.get(key);
if (json == null){
DockingDevice device = dockingDeviceService.getDeviceInfo(deviceId);
if(device != null){
RedisUtil.StringOps.set(key, JsonUtils.toCompressJson(device));
}
return device;
}
return JsonUtils.parse(json, DockingDevice.class);
}
}

View File

@ -0,0 +1,23 @@
package cn.skcks.docking.gb28181.service.docking;
import cn.skcks.docking.gb28181.common.redis.RedisUtil;
import cn.skcks.docking.gb28181.orm.mybatis.dynamic.mapper.DockingDeviceDynamicSqlSupport;
import cn.skcks.docking.gb28181.orm.mybatis.dynamic.mapper.DockingDeviceMapper;
import cn.skcks.docking.gb28181.orm.mybatis.dynamic.model.DockingDevice;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.springframework.stereotype.Service;
@Slf4j
@RequiredArgsConstructor
@Service
public class DockingDeviceService {
private final DockingDeviceMapper dockingDeviceMapper;
public DockingDevice getDeviceInfo(String deviceId){
return dockingDeviceMapper.selectOne((s -> {
return s.where(DockingDeviceDynamicSqlSupport.deviceId, SqlBuilder.isEqualTo(deviceId));
})).orElse(null);
}
}