|
@@ -18,6 +18,7 @@ import org.apache.shiro.authz.UnauthenticatedException;
|
|
|
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
|
|
import org.apache.shiro.subject.Subject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.beans.propertyeditors.CustomDateEditor;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.ui.Model;
|
|
@@ -29,13 +30,12 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.lang.reflect.Field;
|
|
|
import java.sql.SQLException;
|
|
|
import java.text.DateFormat;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
* Description:内容管理控制器
|
|
@@ -47,6 +47,12 @@ import java.util.Map;
|
|
|
@RequestMapping("/system/cms/content")
|
|
|
public class ContentController{
|
|
|
|
|
|
+ @Value("${allowed-characters}")
|
|
|
+ private String allowedcharacters;
|
|
|
+
|
|
|
+ @Value("${allowed-characters-for-content}")
|
|
|
+ private String allowedcharactersforcontent;
|
|
|
+
|
|
|
@Autowired
|
|
|
private ContentService contentService;
|
|
|
|
|
@@ -182,6 +188,25 @@ public class ContentController{
|
|
|
}else {
|
|
|
String filedValue = request.getParameter(filed.getFiledName());
|
|
|
if (!StrUtil.isBlank(filedValue)) {
|
|
|
+ // 第一轮核验,检查是否含有不允许的字符
|
|
|
+ for (String item : allowedcharactersforcontent.split(",")) {
|
|
|
+ if (filedValue.contains(item)) {
|
|
|
+ throw new IllegalArgumentException("上传失败!禁止录入非法字符 " + item);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 第二轮核验,进行字符替换
|
|
|
+ for (String item : allowedcharactersforcontent.split(",")) {
|
|
|
+ item = item.replace(" ", "");
|
|
|
+ item = item.replace("<", "<");
|
|
|
+ item = item.replace(">", ">");
|
|
|
+ if (filedValue.contains(item)) {
|
|
|
+ throw new IllegalArgumentException("上传失败!禁止录入非法字符 " + item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果不含非法字符,则存储值
|
|
|
formParam.put(filed.getFiledName(), filedValue);
|
|
|
}
|
|
|
}
|
|
@@ -191,10 +216,61 @@ public class ContentController{
|
|
|
content.setTitle(formParam.get("name").toString());
|
|
|
}
|
|
|
if(content.getContentId()!=null) {
|
|
|
+ checkAndSave(content);
|
|
|
return contentService.update(content,cmsModel.getTableName(),cmsModelFileds,formParam,tags);
|
|
|
}
|
|
|
+ checkAndSave(content);
|
|
|
return contentService.save(content,cmsModel.getTableName(),formParam,tags);
|
|
|
}
|
|
|
+ private void checkAndSave(TCmsContent content) {
|
|
|
+ // 检查对象是否不为空
|
|
|
+ if (content != null) {
|
|
|
+ // 遍历TCmsTopic类中的所有字段
|
|
|
+ for (Field field : TCmsContent.class.getDeclaredFields()) {
|
|
|
+ // 使字段可访问以获取和设置其值
|
|
|
+ field.setAccessible(true);
|
|
|
+ try {
|
|
|
+ // 获取字段的当前值
|
|
|
+ Object value = field.get(content);
|
|
|
+ // 如果值是字符串并包含特殊字符,则直接禁止存储
|
|
|
+ if (value instanceof String) {
|
|
|
+ String originalValue = (String) value;
|
|
|
+ if (containsIllegalCharacters(originalValue)) {
|
|
|
+ String illegalCharacters = findIllegalCharacters(originalValue, allowedcharacters);
|
|
|
+ throw new IllegalArgumentException("上传失败!禁止录入非法字符 " + illegalCharacters);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ e.printStackTrace(); // 根据需要处理异常
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean containsIllegalCharacters(String value) {
|
|
|
+ for (String item : allowedcharacters.split(",")) {
|
|
|
+ if (value.contains(item)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String findIllegalCharacters(String value, String allowedcharacters) {
|
|
|
+ StringBuilder illegalCharacters = new StringBuilder();
|
|
|
+ for (String item : allowedcharacters.split(",")) {
|
|
|
+ if (value.contains(item)) {
|
|
|
+ illegalCharacters.append(item).append(", ");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 删除最后的逗号和空格
|
|
|
+ if (illegalCharacters.length() > 0) {
|
|
|
+ illegalCharacters.deleteCharAt(illegalCharacters.length() - 1);
|
|
|
+ illegalCharacters.deleteCharAt(illegalCharacters.length() - 1);
|
|
|
+ }
|
|
|
+ return illegalCharacters.toString();
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
@SysLog("内容删除")
|
|
|
@RequiresPermissions("content:delete")
|