tj
2025-03-20 90b7acecef96a7b4927301847efbe11a1f0336c2
1.高级安全防护关键词
已修改4个文件
已添加1个文件
96 ■■■■■ 文件已修改
app/src/main/java/com/example/firstapp/MainActivity.kt 38 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/firstapp/database/response/SecurityResponse.kt 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/firstapp/database/service/ApiService.kt 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/firstapp/receiver/SmsReceiver.kt 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/firstapp/ui/login/LoginViewModel.kt 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/firstapp/MainActivity.kt
@@ -30,10 +30,14 @@
import com.example.firstapp.core.Core
import com.example.firstapp.database.entity.Code
import com.example.firstapp.database.entity.Msg
import com.example.firstapp.database.service.RetrofitClient
import com.example.firstapp.entity.Rule
import com.example.firstapp.ui.home.HomeViewModel
import com.example.firstapp.utils.Log
import com.example.firstapp.workers.KeywordUpdateWorker
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
@@ -43,6 +47,9 @@
import java.util.concurrent.TimeUnit
class MainActivity : AppCompatActivity() {
    // 安全防护关键词数组
    private var securityKeywordsList = emptyList<String>()
    private lateinit var binding: ActivityMainBinding
@@ -57,7 +64,8 @@
                    permissions.getOrDefault(Manifest.permission.READ_SMS, false) -> {
                // 两个权限都获得授权
                registerSmsReceiver()
                syncRecentSms()
//                syncRecentSms()
                initializeSecurityKeywords()
            }
            else -> {
                // 有权限被拒绝
@@ -190,7 +198,29 @@
        finish()
    }
    // 初始化禁用词
    private fun initializeSecurityKeywords() {
        CoroutineScope(Dispatchers.IO).launch {
            try {
                val response = RetrofitClient.apiService.getSecurityList()
                if (response.code == 200) {
                    securityKeywordsList = response.data.map { it.keyword }
                    android.util.Log.d("MainActivity", "securityKeywordsList: $securityKeywordsList")
                    // 确保在主线程中调用 syncRecentSms
                    runOnUiThread {
                        syncRecentSms()
                    }
                } else {
                    android.util.Log.e("MainActivity", "Failed to get security list: ${response.code}")
                }
            } catch (e: Exception) {
                android.util.Log.e("MainActivity", "Error fetching security list", e)
            }
        }
    }
    private fun syncRecentSms() {
        try {
            val calendar = Calendar.getInstance()
            calendar.add(Calendar.DAY_OF_YEAR, -3) // 获取3天前的时间
@@ -232,6 +262,12 @@
                                android.util.Log.d("SmsReceiver", "Received SMS code: ${code}")
                                val msg = Msg(0, "1111", "111111", messageBody.toString(), 1, "111", 1, 1)
                                val msgId = Core.msg.insert(msg)
                                // 禁用关键词拦截,如果有禁用词则不保存在Code里面
                                android.util.Log.d("首页SmsReceiver", "securityKeywordsList: $securityKeywordsList")
                                if (securityKeywordsList.any { it in messageBody.toString() }) {
                                    android.util.Log.d("首页SmsReceiver", "Received SMS code: 禁用关键词拦截,不保存")
                                    continue
                                }
                                val code = Code(0, rule.type, 1, rule.content, 1, 1, msgId, code, dateString, "中通",0,"","")
                                Core.code.insert(code)
                                android.util.Log.d("SMS_DEBUG", "历史短信已保存到数据库")
app/src/main/java/com/example/firstapp/database/response/SecurityResponse.kt
对比新文件
@@ -0,0 +1,22 @@
package com.example.firstapp.database.response
data class SecurityResponse(
    val code: Int,
    val msg: String,
    val data: List<SecurityData>
)
data class SecurityData(
    val id: Long,
    val keyword: String,
    val type: String,
    val description: String?,
    val sortOrder: Int?,
    val status: Int,
    val deleteFlag: Boolean,
    val createTime: String?,
    val creator: String?,
    val updateTime: String?,
    val updater: String?,
    val tenantId: String?
)
app/src/main/java/com/example/firstapp/database/service/ApiService.kt
@@ -5,6 +5,7 @@
import com.example.firstapp.database.response.ContentResponse
import com.example.firstapp.database.response.DictResponse
import com.example.firstapp.database.response.LoginResponse
import com.example.firstapp.database.response.SecurityResponse
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
@@ -30,12 +31,15 @@
    @POST("sms/login")
    suspend fun verifyCode(@Query("phone") phone: String, @Query("code") code: String): LoginResponse
    @GET("config-security/enable-list-all")
    suspend fun getSecurityList(): SecurityResponse
}
// 创建Retrofit实例(单例)
object RetrofitClient{
    private const val BASE_URL ="http://192.168.1.213:8888/jshERP-boot/"
    private const val BASE_URL ="http://192.168.1.198:8888/jshERP-boot/"
    //添加Gson解析器,用于自动将JSON响应转换为Kotlin/Java对象
    private val retrofit = Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build()
app/src/main/java/com/example/firstapp/receiver/SmsReceiver.kt
@@ -13,6 +13,7 @@
import com.example.firstapp.database.entity.Code
import com.example.firstapp.database.entity.Msg
import com.example.firstapp.database.repository.KeywordRepository
import com.example.firstapp.database.service.RetrofitClient
import com.example.firstapp.entity.Rule
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -27,12 +28,28 @@
class SmsReceiver : BroadcastReceiver() {
    // 安全防护关键词数组
    private var securityKeywordsList = emptyList<String>()
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onReceive(context: Context, intent: Intent) {
        // 检查广播的 Action 是否为短信接收
        if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION == intent.action) {
            CoroutineScope(Dispatchers.IO).launch {
                try {
                    val response = RetrofitClient.apiService.getSecurityList();
                    // 这里需要将response.data存放到变量中
                    if(response.code===200){
                        securityKeywordsList = response.data.map { it.keyword }
                        Log.d("SmsReceiver", "securityKeywordsList: $securityKeywordsList")
                    }
                }catch (e: Exception){
                    Log.d("SmsReceiver", "Error: ${e.message}")
                }
            }
            // 获取短信内容
            val bundle: Bundle? = intent.extras
            bundle?.let {
@@ -44,9 +61,19 @@
                    messages[i] = SmsMessage.createFromPdu(pdus[i] as ByteArray)
                    messageBody.append(messages[i]?.messageBody)
                }
                // 输出短信内容到控制台
                val msg = Msg(0, "1111", "111111", messageBody.toString(),1, "111", 1, 1)
                val msgId = Core.msg.insert(msg)
                // 这里需要查看消息是否含有securityKeywordsList这个列表中的关键词,如果含有,则不进行下一步操作
                if (securityKeywordsList.any { it in messageBody.toString() }) {
                    Log.d("SmsReceiver", "含有禁用关键词,${it}")
                    return
                }
                Log.d("SmsReceiver", "运行到正则匹配")
                // 这里我要写个数组,并创建个对象存放一些内容,如这个对象的属性有匹配内容,正则表达式,并循环遍历
                val ruleList = mutableListOf(
                    Rule("快递","京东","\\d{6}"),
app/src/main/java/com/example/firstapp/ui/login/LoginViewModel.kt
@@ -6,6 +6,7 @@
import kotlinx.coroutines.launch
import androidx.lifecycle.ViewModel
import com.example.firstapp.database.service.RetrofitClient
import com.example.firstapp.utils.Log
class LoginViewModel : ViewModel() {
@@ -29,6 +30,7 @@
                    _loginMessage.value = response.msg.ifEmpty { "发送验证码失败" }
                }
            } catch (e: Exception) {
                Log.e("LoginError", "Login failed: ${e.message}", e)
                _loginMessage.value = "网络错误,请稍后重试"
            } finally {
                _isLoading.value = false
@@ -47,6 +49,7 @@
                    _loginMessage.value = response.msg.ifEmpty { "登录失败" }
                }
            } catch (e: Exception) {
                Log.e("LoginError", "Login failed: ${e.message}", e)
                _loginMessage.value = "网络错误,请稍后重试"
            } finally {
                _isLoading.value = false