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

View File

@ -1,25 +1,26 @@
package com.idormy.sms.forwarder.server.controller package com.idormy.sms.forwarder.server.controller
import android.util.Log
import com.idormy.sms.forwarder.entity.CallInfo import com.idormy.sms.forwarder.entity.CallInfo
import com.idormy.sms.forwarder.server.model.BaseRequest import com.idormy.sms.forwarder.server.model.BaseRequest
import com.idormy.sms.forwarder.server.model.CallQueryData import com.idormy.sms.forwarder.server.model.CallQueryData
import com.idormy.sms.forwarder.utils.PhoneUtils import com.idormy.sms.forwarder.utils.PhoneUtils
import com.yanzhenjie.andserver.annotation.PostMapping import com.yanzhenjie.andserver.annotation.*
import com.yanzhenjie.andserver.annotation.RequestBody
import com.yanzhenjie.andserver.annotation.RequestMapping
import com.yanzhenjie.andserver.annotation.RestController
@Suppress("PrivatePropertyName") @Suppress("PrivatePropertyName")
@RestController @RestController
@RequestMapping(path = ["/call"]) @RequestMapping(path = ["/call"])
class CallController { class CallController {
//private val TAG: String = CallController::class.java.simpleName private val TAG: String = CallController::class.java.simpleName
//远程查通话 //远程查通话
@CrossOrigin(methods = [RequestMethod.POST])
@PostMapping("/query") @PostMapping("/query")
fun query(@RequestBody bean: BaseRequest<CallQueryData>): List<CallInfo>? { fun query(@RequestBody bean: BaseRequest<CallQueryData>): List<CallInfo> {
val callQueryData = bean.data val callQueryData = bean.data
Log.d(TAG, callQueryData.toString())
val limit = callQueryData.pageSize val limit = callQueryData.pageSize
val offset = (callQueryData.pageNum - 1) * limit val offset = (callQueryData.pageNum - 1) * limit
return PhoneUtils.getCallInfoList(callQueryData.type, limit, offset, callQueryData.phoneNumber) 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.entity.CloneInfo
import com.idormy.sms.forwarder.server.model.BaseRequest import com.idormy.sms.forwarder.server.model.BaseRequest
import com.idormy.sms.forwarder.utils.HttpServerUtils import com.idormy.sms.forwarder.utils.HttpServerUtils
import com.yanzhenjie.andserver.annotation.PostMapping import com.yanzhenjie.andserver.annotation.*
import com.yanzhenjie.andserver.annotation.RequestBody
import com.yanzhenjie.andserver.annotation.RequestMapping
import com.yanzhenjie.andserver.annotation.RestController
@Suppress("PrivatePropertyName") @Suppress("PrivatePropertyName")
@RestController @RestController
@ -17,10 +14,12 @@ class CloneController {
private val TAG: String = CloneController::class.java.simpleName private val TAG: String = CloneController::class.java.simpleName
//客户端从服务端拉取克隆信息 //客户端从服务端拉取克隆信息
@CrossOrigin(methods = [RequestMethod.POST])
@PostMapping("/pull") @PostMapping("/pull")
fun pull(@RequestBody bean: BaseRequest<CloneInfo>): CloneInfo { fun pull(@RequestBody bean: BaseRequest<CloneInfo>): CloneInfo {
val cloneBean = bean.data val cloneBean = bean.data
Log.d(TAG, cloneBean.toString()) Log.d(TAG, cloneBean.toString())
HttpServerUtils.compareVersion(cloneBean) HttpServerUtils.compareVersion(cloneBean)
val cloneInfo = HttpServerUtils.exportSettings() val cloneInfo = HttpServerUtils.exportSettings()
@ -29,10 +28,12 @@ class CloneController {
} }
//客户端向服务端推送克隆信息 //客户端向服务端推送克隆信息
@CrossOrigin(methods = [RequestMethod.POST])
@PostMapping("/push") @PostMapping("/push")
fun push(@RequestBody bean: BaseRequest<CloneInfo>): String { fun push(@RequestBody bean: BaseRequest<CloneInfo>): String {
val cloneInfo = bean.data val cloneInfo = bean.data
Log.d(TAG, cloneInfo.toString()) Log.d(TAG, cloneInfo.toString())
HttpServerUtils.compareVersion(cloneInfo) HttpServerUtils.compareVersion(cloneInfo)
return if (HttpServerUtils.restoreSettings(cloneInfo)) "success" else "还原失败" return if (HttpServerUtils.restoreSettings(cloneInfo)) "success" else "还原失败"

View File

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

View File

@ -1,26 +1,29 @@
package com.idormy.sms.forwarder.server.controller package com.idormy.sms.forwarder.server.controller
import android.util.Log
import com.idormy.sms.forwarder.entity.ContactInfo import com.idormy.sms.forwarder.entity.ContactInfo
import com.idormy.sms.forwarder.server.model.BaseRequest import com.idormy.sms.forwarder.server.model.BaseRequest
import com.idormy.sms.forwarder.server.model.ContactQueryData import com.idormy.sms.forwarder.server.model.ContactQueryData
import com.idormy.sms.forwarder.utils.PhoneUtils import com.idormy.sms.forwarder.utils.PhoneUtils
import com.yanzhenjie.andserver.annotation.PostMapping import com.yanzhenjie.andserver.annotation.*
import com.yanzhenjie.andserver.annotation.RequestBody
import com.yanzhenjie.andserver.annotation.RequestMapping
import com.yanzhenjie.andserver.annotation.RestController
@Suppress("PrivatePropertyName") @Suppress("PrivatePropertyName")
@RestController @RestController
@RequestMapping(path = ["/contact"]) @RequestMapping(path = ["/contact"])
class ContactController { class ContactController {
private val TAG: String = ContactController::class.java.simpleName
//远程查话簿 //远程查话簿
@CrossOrigin(methods = [RequestMethod.POST])
@PostMapping("/query") @PostMapping("/query")
fun query(@RequestBody bean: BaseRequest<ContactQueryData>): MutableList<ContactInfo>? { fun query(@RequestBody bean: BaseRequest<ContactQueryData>): MutableList<ContactInfo> {
val callQueryData = bean.data val contactQueryData = bean.data
val limit = callQueryData.pageSize Log.d(TAG, contactQueryData.toString())
val offset = (callQueryData.pageNum - 1) * limit
return PhoneUtils.getContactInfoList(limit, offset, callQueryData.phoneNumber, callQueryData.name) 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.idormy.sms.forwarder.utils.PhoneUtils
import com.xuexiang.xui.utils.ResUtils import com.xuexiang.xui.utils.ResUtils
import com.xuexiang.xutil.XUtil import com.xuexiang.xutil.XUtil
import com.yanzhenjie.andserver.annotation.PostMapping import com.yanzhenjie.andserver.annotation.*
import com.yanzhenjie.andserver.annotation.RequestBody
import com.yanzhenjie.andserver.annotation.RequestMapping
import com.yanzhenjie.andserver.annotation.RestController
@Suppress("PrivatePropertyName") @Suppress("PrivatePropertyName")
@RestController @RestController
@ -26,6 +23,7 @@ class SmsController {
private val TAG: String = SmsController::class.java.simpleName private val TAG: String = SmsController::class.java.simpleName
//发送短信 //发送短信
@CrossOrigin(methods = [RequestMethod.POST])
@PostMapping("/send") @PostMapping("/send")
fun send(@RequestBody bean: BaseRequest<SmsSendData>): String { fun send(@RequestBody bean: BaseRequest<SmsSendData>): String {
val smsSendData = bean.data val smsSendData = bean.data
@ -50,9 +48,12 @@ class SmsController {
} }
//查询短信 //查询短信
@CrossOrigin(methods = [RequestMethod.POST])
@PostMapping("/query") @PostMapping("/query")
fun query(@RequestBody bean: BaseRequest<SmsQueryData>): List<SmsInfo>? { fun query(@RequestBody bean: BaseRequest<SmsQueryData>): List<SmsInfo> {
val smsQueryData = bean.data val smsQueryData = bean.data
Log.d(TAG, smsQueryData.toString())
val limit = smsQueryData.pageSize val limit = smsQueryData.pageSize
val offset = (smsQueryData.pageNum - 1) * limit val offset = (smsQueryData.pageNum - 1) * limit
return PhoneUtils.getSmsList(smsQueryData.type, limit, offset, smsQueryData.keyword) return PhoneUtils.getSmsList(smsQueryData.type, limit, offset, smsQueryData.keyword)

View File

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