优化:HttpServer跨域设置(CrossOrigin)

This commit is contained in:
pppscn 2022-06-06 23:46:50 +08:00
parent 1c43250b43
commit bdd62ff77e
7 changed files with 50 additions and 41 deletions

View File

@ -8,10 +8,7 @@ import com.idormy.sms.forwarder.entity.BatteryInfo
import com.idormy.sms.forwarder.server.model.BaseRequest
import com.idormy.sms.forwarder.server.model.EmptyData
import com.idormy.sms.forwarder.utils.BatteryUtils
import com.yanzhenjie.andserver.annotation.PostMapping
import com.yanzhenjie.andserver.annotation.RequestBody
import com.yanzhenjie.andserver.annotation.RequestMapping
import com.yanzhenjie.andserver.annotation.RestController
import com.yanzhenjie.andserver.annotation.*
@Suppress("PrivatePropertyName")
@RestController
@ -21,10 +18,10 @@ class BatteryController {
private val TAG: String = BatteryController::class.java.simpleName
//远程查电量
@CrossOrigin(methods = [RequestMethod.POST])
@PostMapping("/query")
fun query(@RequestBody bean: BaseRequest<EmptyData>): BatteryInfo {
val cloneBean = bean.data
Log.d(TAG, cloneBean.toString())
Log.d(TAG, bean.data.toString())
val intentFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
val intent: Intent? = App.context.registerReceiver(null, intentFilter)

View File

@ -1,25 +1,26 @@
package com.idormy.sms.forwarder.server.controller
import android.util.Log
import com.idormy.sms.forwarder.entity.CallInfo
import com.idormy.sms.forwarder.server.model.BaseRequest
import com.idormy.sms.forwarder.server.model.CallQueryData
import com.idormy.sms.forwarder.utils.PhoneUtils
import com.yanzhenjie.andserver.annotation.PostMapping
import com.yanzhenjie.andserver.annotation.RequestBody
import com.yanzhenjie.andserver.annotation.RequestMapping
import com.yanzhenjie.andserver.annotation.RestController
import com.yanzhenjie.andserver.annotation.*
@Suppress("PrivatePropertyName")
@RestController
@RequestMapping(path = ["/call"])
class CallController {
//private val TAG: String = CallController::class.java.simpleName
private val TAG: String = CallController::class.java.simpleName
//远程查通话
@CrossOrigin(methods = [RequestMethod.POST])
@PostMapping("/query")
fun query(@RequestBody bean: BaseRequest<CallQueryData>): List<CallInfo>? {
fun query(@RequestBody bean: BaseRequest<CallQueryData>): List<CallInfo> {
val callQueryData = bean.data
Log.d(TAG, callQueryData.toString())
val limit = callQueryData.pageSize
val offset = (callQueryData.pageNum - 1) * limit
return PhoneUtils.getCallInfoList(callQueryData.type, limit, offset, callQueryData.phoneNumber)

View File

@ -4,10 +4,7 @@ import android.util.Log
import com.idormy.sms.forwarder.entity.CloneInfo
import com.idormy.sms.forwarder.server.model.BaseRequest
import com.idormy.sms.forwarder.utils.HttpServerUtils
import com.yanzhenjie.andserver.annotation.PostMapping
import com.yanzhenjie.andserver.annotation.RequestBody
import com.yanzhenjie.andserver.annotation.RequestMapping
import com.yanzhenjie.andserver.annotation.RestController
import com.yanzhenjie.andserver.annotation.*
@Suppress("PrivatePropertyName")
@RestController
@ -17,10 +14,12 @@ class CloneController {
private val TAG: String = CloneController::class.java.simpleName
//客户端从服务端拉取克隆信息
@CrossOrigin(methods = [RequestMethod.POST])
@PostMapping("/pull")
fun pull(@RequestBody bean: BaseRequest<CloneInfo>): CloneInfo {
val cloneBean = bean.data
Log.d(TAG, cloneBean.toString())
HttpServerUtils.compareVersion(cloneBean)
val cloneInfo = HttpServerUtils.exportSettings()
@ -29,10 +28,12 @@ class CloneController {
}
//客户端向服务端推送克隆信息
@CrossOrigin(methods = [RequestMethod.POST])
@PostMapping("/push")
fun push(@RequestBody bean: BaseRequest<CloneInfo>): String {
val cloneInfo = bean.data
Log.d(TAG, cloneInfo.toString())
HttpServerUtils.compareVersion(cloneInfo)
return if (HttpServerUtils.restoreSettings(cloneInfo)) "success" else "还原失败"

View File

@ -1,19 +1,24 @@
package com.idormy.sms.forwarder.server.controller
import android.util.Log
import com.idormy.sms.forwarder.server.model.BaseRequest
import com.idormy.sms.forwarder.server.model.ConfigData
import com.idormy.sms.forwarder.utils.HttpServerUtils
import com.yanzhenjie.andserver.annotation.PostMapping
import com.yanzhenjie.andserver.annotation.RequestBody
import com.yanzhenjie.andserver.annotation.RequestMapping
import com.yanzhenjie.andserver.annotation.RestController
import com.yanzhenjie.andserver.annotation.*
@Suppress("PrivatePropertyName")
@RestController
@RequestMapping(path = ["/config"])
class ConfigController {
private val TAG: String = CloneController::class.java.simpleName
//远程查配置
@CrossOrigin(methods = [RequestMethod.POST])
@PostMapping("/query")
fun test(@RequestBody bean: BaseRequest<*>): ConfigData {
Log.d(TAG, bean.data.toString())
return ConfigData(
HttpServerUtils.enableApiClone,
HttpServerUtils.enableApiSmsSend,

View File

@ -1,26 +1,29 @@
package com.idormy.sms.forwarder.server.controller
import android.util.Log
import com.idormy.sms.forwarder.entity.ContactInfo
import com.idormy.sms.forwarder.server.model.BaseRequest
import com.idormy.sms.forwarder.server.model.ContactQueryData
import com.idormy.sms.forwarder.utils.PhoneUtils
import com.yanzhenjie.andserver.annotation.PostMapping
import com.yanzhenjie.andserver.annotation.RequestBody
import com.yanzhenjie.andserver.annotation.RequestMapping
import com.yanzhenjie.andserver.annotation.RestController
import com.yanzhenjie.andserver.annotation.*
@Suppress("PrivatePropertyName")
@RestController
@RequestMapping(path = ["/contact"])
class ContactController {
private val TAG: String = ContactController::class.java.simpleName
//远程查话簿
@CrossOrigin(methods = [RequestMethod.POST])
@PostMapping("/query")
fun query(@RequestBody bean: BaseRequest<ContactQueryData>): MutableList<ContactInfo>? {
val callQueryData = bean.data
val limit = callQueryData.pageSize
val offset = (callQueryData.pageNum - 1) * limit
return PhoneUtils.getContactInfoList(limit, offset, callQueryData.phoneNumber, callQueryData.name)
fun query(@RequestBody bean: BaseRequest<ContactQueryData>): MutableList<ContactInfo> {
val contactQueryData = bean.data
Log.d(TAG, contactQueryData.toString())
val limit = contactQueryData.pageSize
val offset = (contactQueryData.pageNum - 1) * limit
return PhoneUtils.getContactInfoList(limit, offset, contactQueryData.phoneNumber, contactQueryData.name)
}
}

View File

@ -13,10 +13,7 @@ import com.idormy.sms.forwarder.server.model.SmsSendData
import com.idormy.sms.forwarder.utils.PhoneUtils
import com.xuexiang.xui.utils.ResUtils
import com.xuexiang.xutil.XUtil
import com.yanzhenjie.andserver.annotation.PostMapping
import com.yanzhenjie.andserver.annotation.RequestBody
import com.yanzhenjie.andserver.annotation.RequestMapping
import com.yanzhenjie.andserver.annotation.RestController
import com.yanzhenjie.andserver.annotation.*
@Suppress("PrivatePropertyName")
@RestController
@ -26,6 +23,7 @@ class SmsController {
private val TAG: String = SmsController::class.java.simpleName
//发送短信
@CrossOrigin(methods = [RequestMethod.POST])
@PostMapping("/send")
fun send(@RequestBody bean: BaseRequest<SmsSendData>): String {
val smsSendData = bean.data
@ -50,9 +48,12 @@ class SmsController {
}
//查询短信
@CrossOrigin(methods = [RequestMethod.POST])
@PostMapping("/query")
fun query(@RequestBody bean: BaseRequest<SmsQueryData>): List<SmsInfo>? {
fun query(@RequestBody bean: BaseRequest<SmsQueryData>): List<SmsInfo> {
val smsQueryData = bean.data
Log.d(TAG, smsQueryData.toString())
val limit = smsQueryData.pageSize
val offset = (smsQueryData.pageNum - 1) * limit
return PhoneUtils.getSmsList(smsQueryData.type, limit, offset, smsQueryData.keyword)

View File

@ -224,19 +224,20 @@ class HttpServerUtils private constructor() {
//返回统一结构报文
fun response(output: Any?): String {
val resp: MutableMap<String, Any> = mutableMapOf()
val timestamp = System.currentTimeMillis()
resp["timestamp"] = timestamp
if (output is String && output != "success") {
resp["code"] = HTTP_FAILURE_CODE
resp["msg"] = output
} else {
resp["code"] = HTTP_SUCCESS_CODE
resp["msg"] = "success"
if (output != null) resp["data"] = output
}
val timestamp = System.currentTimeMillis()
resp["timestamp"] = timestamp
if (!TextUtils.isEmpty(serverSignKey)) {
resp["sign"] = calcSign(timestamp.toString(), serverSignKey.toString())
if (output != null) {
resp["data"] = output
}
if (!TextUtils.isEmpty(serverSignKey)) {
resp["sign"] = calcSign(timestamp.toString(), serverSignKey.toString())
}
}
return Gson().toJson(resp)