通过ValidateInput()关闭数据安全验证

本文探讨了CKEditor4在MVC框架中上传包含HTML代码的文本和图片时遇到的安全验证问题。通过禁用MVC的安全验证功能,实现了数据的成功上传。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CKEditor4,一款方便、实用的功能性插件,在Js部分进行引用后可以在页面上生成出一个文本框架,在需要上传文本内容和图片文件的一些页面或程序上非常实用,更免去了构建一个文本输入框的代码及时间。
在这里插入图片描述
但此框架在数据上传的功能方面却有个始料未及的问题,用户在文本框内输入的文本数据以及图片数据会被它直接转换成Html的代码进行上传。起初,在使用MVC对上传的数据进行处理时,我发现数据无法上传成功,一直报错。我原以为是我控制器代码的处理上发生了错误,赋值赋错了,还是路径写漏的缘故。但经过几次的反复的断点调试,我发现似乎并没有我想象的那么简单。于是乎我点开了浏览器的Network,查看浏览器输出的到底是那部分的错误。如下图:
在这里插入图片描述
潜在的危险值?起初我并不知道这错误是什么意思,接着去问了一下老师才知道,原来在MVC的安全验证中,直接返回代码数据属于[危险,且具有攻击性]的操作,进而拦截文本框数据的上传(报错),导致无法执行定义好的数据上传方法。这可是害我好找,但也让我感到新奇,原来MVC中还有数据安全性验证的这种功能。找到了出错的原因后,接着是不是应该放弃这款实用方便的插件了,然后自己重新去构建一个新的文本框架,或者再找另一个插件代替?其实并不用从头开始,凡事都有解决的办法。既然该文本框架上传的文本信息类型与MVC的安全验证有冲突,那就选择其一屏蔽掉就好,当然了,文本框架我是不会从头开始的,毕竟太麻烦。那自然就是解决MVC安全验证的这块问题。通过下面的这句看似简单但却又深藏不露的代码,便可以关闭验证: [ValidateInput(false)],
(在定义好的方法开头写上这句代码,便可以在控制器调用方法时关闭MVC安全验证)
在这里插入图片描述
就是这么简单的一句代码,却是解决了对我而言的大问题,数据理所当然的可以正常上传了,并不会像之前那样报一个危险信息错误了。
于此同时,老师也告诉我这句代码切不可每个方法都调用,这样一定会出大问题,即使是某些方法的确是需要关闭验证才能调用的也要谨慎使用,使用的次数也不能超过两次及以上。

function fruitOptimizationSystem() % 创建主图形窗口 mainFig = figure('Name', '水果店进货优化系统', ... 'NumberTitle', 'off', ... 'Position', [100, 100, 1000, 700], ... 'MenuBar', 'none', ... 'ToolBar', 'none', ... 'CloseRequestFcn', @safeCloseCallback, ... 'Color', [0.95 0.95 0.95], ... 'Tag', 'MainFigure'); % 初始化系统数据 initSystemData(mainFig); % 创建登录界面 createLoginUI(mainFig); % 等待窗口关闭 uiwait(mainFig); end %% 初始化系统数据 function initSystemData(figHandle) % 水果数据 fruits = {'苹果', '香蕉', '橙子', '草莓', '葡萄'}; costs = [3.5, 2.8, 4.2, 6.5, 5.0]; % 成本价 prices = [5.0, 4.0, 6.0, 8.5, 7.0]; % 销售价 stock = [100, 80, 120, 60, 90]; % 当前库存 % 销售预测数据 salesPrediction = struct(... 'Monday', [50, 40, 60, 30, 45], ... 'Tuesday', [45, 35, 55, 25, 40], ... 'Wednesday', [55, 45, 65, 35, 50], ... 'Thursday', [60, 50, 70, 40, 55], ... 'Friday', [70, 60, 80, 50, 65], ... 'Saturday', [80, 70, 90, 60, 75], ... 'Sunday', [65, 55, 75, 45, 60]); % 存储到图形对象 setappdata(figHandle, 'Fruits', fruits); setappdata(figHandle, 'Costs', costs); setappdata(figHandle, 'Prices', prices); setappdata(figHandle, 'Stock', stock); setappdata(figHandle, 'SalesPrediction', salesPrediction); setappdata(figHandle, 'UserLoggedIn', false); end %% 创建登录界面 function createLoginUI(figHandle) % 登录面板 loginPanel = uipanel(figHandle, ... 'Position', [0.25, 0.3, 0.5, 0.4], ... 'Title', '系统登录', ... 'FontSize', 14, ... 'BackgroundColor', [0.9 0.95 1], ... 'Tag', 'LoginPanel'); % 用户名标签和输入框 uicontrol(loginPanel, 'Style', 'text', ... 'String', '用户名:', ... 'Position', [150, 200, 80, 30], ... 'FontSize', 12, ... 'BackgroundColor', [0.9 0.95 1]); usernameEdit = uicontrol(loginPanel, 'Style', 'edit', ... 'Position', [240, 200, 200, 30], ... 'FontSize', 12, ... 'Tag', 'UsernameEdit'); % 密码标签和输入框 uicontrol(loginPanel, 'Style', 'text', ... 'String', '密码:', ... 'Position', [150, 140, 80, 30], ... 'FontSize', 12, ... 'BackgroundColor', [0.9 0.95 1]); passwordEdit = uicontrol(loginPanel, 'Style', 'edit', ... 'Position', [240, 140, 200, 30], ... 'FontSize', 12, ... 'Tag', 'PasswordEdit', ... 'KeyPressFcn', @(src,event)handleKeyPress(event, figHandle)); % 登录按钮 uicontrol(loginPanel, 'Style', 'pushbutton', ... 'String', '登录', ... 'Position', [280, 70, 120, 40], ... 'FontSize', 12, ... 'BackgroundColor', [0.3 0.7 0.5], ... 'Callback', @(src,event)loginCallback(figHandle, usernameEdit, passwordEdit)); end %% 键盘事件处理 function handleKeyPress(event, figHandle) if strcmp(event.Key, 'return') % 回车键 usernameEdit = findobj(figHandle, 'Tag', 'UsernameEdit'); passwordEdit = findobj(figHandle, 'Tag', 'PasswordEdit'); loginCallback(figHandle, usernameEdit, passwordEdit); end end %% 登录回调函数 function loginCallback(figHandle, usernameEdit, passwordEdit) username = get(usernameEdit, 'String'); password = get(passwordEdit, 'String'); % 简单验证(实际应用中应使用安全验证) if strcmp(username, 'admin') && strcmp(password, '123456') setappdata(figHandle, 'UserLoggedIn', true); delete(findobj(figHandle, 'Tag', 'LoginPanel')); createMainUI(figHandle); else errordlg('用户名或密码错误!', '登录失败'); end end %% 创建主界面 function createMainUI(figHandle) % 获取数据 fruits = getappdata(figHandle, 'Fruits'); costs = getappdata(figHandle, 'Costs'); prices = getappdata(figHandle, 'Prices'); stock = getappdata(figHandle, 'Stock'); % 创建选项卡 tabGroup = uitabgroup(figHandle, 'Position', [0.05, 0.05, 0.9, 0.85]); % 库存管理选项卡 inventoryTab = uitab(tabGroup, 'Title', '库存管理'); createInventoryUI(inventoryTab, fruits, stock); % 采购优化选项卡 optimizationTab = uitab(tabGroup, 'Title', '采购优化'); createOptimizationUI(optimizationTab, fruits, costs, prices); % 销售分析选项卡 analysisTab = uitab(tabGroup, 'Title', '销售分析'); createAnalysisUI(analysisTab, figHandle); % 系统设置选项卡 settingsTab = uitab(tabGroup, 'Title', '系统设置'); createSettingsUI(settingsTab, figHandle); end %% 创建库存管理界面 function createInventoryUI(parent, fruits, stock) % 创建表格显示库存数据 columnNames = {'水果名称', '当前库存 (kg)', '建议采购量 (kg)'}; data = [fruits; num2cell(stock); num2cell(zeros(1, numel(stock)))]'; uitable(parent, 'Data', data, ... 'ColumnName', columnNames, ... 'Position', [50, 300, 500, 250], ... 'ColumnEditable', [false, true, true], ... 'Tag', 'InventoryTable'); % 添加保存按钮 uicontrol(parent, 'Style', 'pushbutton', ... 'String', '保存库存数据', ... 'Position', [200, 200, 200, 40], ... 'FontSize', 12, ... 'Callback', @saveInventoryCallback); end %% 创建采购优化界面 function createOptimizationUI(parent, fruits, costs, prices) % 计算利润 profits = prices - costs; % 创建利润分析图 ax = axes(parent, 'Position', [0.1, 0.5, 0.8, 0.4]); bar(ax, profits, 'FaceColor', [0.2 0.6 0.8]); set(ax, 'XTickLabel', fruits, 'FontSize', 10); title(ax, '水果单位利润分析', 'FontSize', 14); ylabel(ax, '利润 (元/kg)', 'FontSize', 12); grid(ax, 'on'); % 添加优化建议 [~, idx] = max(profits); recommendation = sprintf('推荐优先采购: %s (利润: %.2f元/kg)', fruits{idx}, profits(idx)); uicontrol(parent, 'Style', 'text', ... 'String', recommendation, ... 'Position', [150, 100, 600, 30], ... 'FontSize', 12, ... 'FontWeight', 'bold', ... 'ForegroundColor', [0.8 0.2 0.2]); end %% 创建销售分析界面 function createAnalysisUI(parent, figHandle) % 获取销售预测数据 salesPrediction = getappdata(figHandle, 'SalesPrediction'); days = fieldnames(salesPrediction); fruits = getappdata(figHandle, 'Fruits'); % 创建下拉菜单选择水果 uicontrol(parent, 'Style', 'text', ... 'String', '选择水果:', ... 'Position', [100, 450, 80, 25], ... 'FontSize', 11); fruitPopup = uicontrol(parent, 'Style', 'popupmenu', ... 'String', fruits, ... 'Position', [190, 450, 150, 25], ... 'Tag', 'FruitSelector', ... 'Callback', @updateSalesPlot); % 创建分析图表区域 ax = axes(parent, 'Position', [0.15, 0.15, 0.75, 0.6], ... 'Tag', 'SalesAxes'); % 初始显示第一种水果的数据 updateSalesPlot(fruitPopup, []); % 嵌套函数更新图表 function updateSalesPlot(~, ~) selectedIdx = get(fruitPopup, 'Value'); salesData = zeros(1, numel(days)); for i = 1:numel(days) daySales = salesPrediction.(days{i}); salesData(i) = daySales(selectedIdx); end % 绘制销售预测 cla(ax); bar(ax, salesData, 'FaceColor', [0.9 0.5 0.1]); set(ax, 'XTickLabel', days, 'FontSize', 10); title(ax, ['每日销售预测: ' fruits{selectedIdx}], 'FontSize', 14); ylabel(ax, '预测销量 (kg)', 'FontSize', 12); grid(ax, 'on'); end end %% 创建系统设置界面 function createSettingsUI(parent, figHandle) % 用户管理 uicontrol(parent, 'Style', 'text', ... 'String', '用户管理', ... 'Position', [50, 450, 100, 30], ... 'FontSize', 12, ... 'FontWeight', 'bold'); % 添加新用户 uicontrol(parent, 'Style', 'pushbutton', ... 'String', '添加新用户', ... 'Position', [50, 400, 120, 30], ... 'Callback', @addUserCallback); % 数据备份 uicontrol(parent, 'Style', 'text', ... 'String', '数据备份', ... 'Position', [250, 450, 100, 30], ... 'FontSize', 12, ... 'FontWeight', 'bold'); % 备份按钮 uicontrol(parent, 'Style', 'pushbutton', ... 'String', '备份当前数据', ... 'Position', [250, 400, 120, 30], ... 'Callback', @backupDataCallback); % 系统信息 uicontrol(parent, 'Style', 'text', ... 'String', '系统信息', ... 'Position', [450, 450, 100, 30], ... 'FontSize', 12, ... 'FontWeight', 'bold'); % 版本信息 uicontrol(parent, 'Style', 'text', ... 'String', '版本: 1.0.0', ... 'Position', [450, 400, 150, 30], ... 'FontSize', 11); % 退出系统按钮 uicontrol(parent, 'Style', 'pushbutton', ... 'String', '退出系统', ... 'Position', [350, 100, 150, 40], ... 'FontSize', 12, ... 'BackgroundColor', [0.9 0.3 0.3], ... 'Callback', @(src,event)close(figHandle)); end %% 安全关闭回调 function safeCloseCallback(src, ~) % 检查用户是否登录 isLoggedIn = getappdata(src, 'UserLoggedIn'); if isLoggedIn % 确认关闭对话框 selection = questdlg('确定要退出系统吗?', ... '确认退出', ... '是', '否', '是'); if ~strcmp(selection, '是') return; end end % 执行清理操作 cleanupSystem(src); % 删除图形 delete(src); end %% 系统清理函数 function cleanupSystem(figHandle) % 获取所有数据 fruits = getappdata(figHandle, 'Fruits'); stock = getappdata(figHandle, 'Stock'); % 模拟保存数据到文件 dataTable = table(fruits', stock', 'VariableNames', {'Fruit', 'Stock'}); try writetable(dataTable, 'fruit_inventory.csv'); disp('库存数据已保存到 fruit_inventory.csv'); catch warning('数据保存失败'); end % 显示关闭消息 disp('系统资源已清理'); disp(['最后操作时间: ' datestr(now)]); end %% 其他回调函数(示例) function saveInventoryCallback(src, ~) % 在实际应用中实现保存逻辑 disp('库存数据已更新'); msgbox('库存数据保存成功!', '操作成功'); end function addUserCallback(~, ~) % 在实际应用中实现用户添加逻辑 disp('添加新用户操作'); end function backupDataCallback(~, ~) % 在实际应用中实现数据备份逻辑 disp('数据备份操作'); msgbox('数据备份完成!', '备份成功'); end 该代码能否输入数据
最新发布
07-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值