欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis動(dòng)態(tài)傳入order?by問題

 更新時(shí)間:2022年12月29日 10:33:59   作者:ClearLoveQ  
這篇文章主要介紹了Mybatis動(dòng)態(tài)傳入order?by問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Mybatis動(dòng)態(tài)傳入order by

當(dāng)Mybatis的mapper文件傳入的order by 為動(dòng)態(tài)參數(shù)說的時(shí)候發(fā)現(xiàn)排序無法生效:

像下面這樣,在choose when中的order by后的參數(shù)是用預(yù)編譯的方式,用的是#號(hào),這樣是可以防止sql注入的問題,但是在傳入order by參數(shù)的時(shí)候無法解析:

	<select id="feescaleList" resultType="com.hasagei.modules.mebespoke.entity.HasageiMeBespoke" parameterType="com.hasagei.modules.mebespoke.entity.HasageiMeBespoke">
		SELECT
		A.ID AS id,
		TO_CHAR(A.BESPOKE_DATE,'yyyy-mm-dd') AS bsepokeDate,
		A.USER_ID AS	userId,
		A.BESPOKE_DATE AS	bespokeDate,
		A.BESPOKE_STATUS	bespokeStatus,
		A.PROJECT_ID AS	projectId,
		A.PERIOD_START AS 	periodStart,
		A.PERIOD_END AS	periodEnd,
		A.FEESCALE_MONEY AS feescaleMoney,
		A.FEESCALE_NAME AS feescaleName,
		A.PAYMENT_TIME AS paymentTime,
		A.PAYMENT_MONEY AS paymentMoney,
		B.IDENTITY_CARD AS identityCard,
		B.REALNAME AS realname,
		B.SJ AS sj,
		B.GZZH AS   gzzh,
		B.PHOTOELECTRIC_CARD AS photoelectricCard,
		B.SEX AS sex,
		B.BIRTH_DATE AS  birthDate,
		B.STUDENT_ID AS  studentId,
		B.EXAMINE_NUMBER AS  examineNumber,
		B.DEPARTMENT AS  department,
		B.FACULTY AS  faculty,
		C.MEC_NAME AS mecName
		FROM
		TSINGHUA_ME_BESPOKE A LEFT JOIN TSINGHUA_USERINFO B ON A.USER_ID=B.ID
		LEFT JOIN MEC_ITEM C ON A.PROJECT_ID=C.MEC_NUMBER
		WHERE 1=1
		<if test='bespokeDateGteJ != null and bespokeDateLteJ != null '>
			<if test='bespokeDateLteJ != "" and bespokeDateLteJ != ""'>
				AND A.PAYMENT_TIME <![CDATA[<=]]> to_date(#{bespokeDateLteJ,jdbcType=DATE},'yyyy-MM-dd HH24:MI:SS')
				AND A.PAYMENT_TIME <![CDATA[>=]]> to_date(#{bespokeDateGteJ,jdbcType=DATE},'yyyy-MM-dd HH24:MI:SS')
			</if>
		</if>
		<choose>
			<when test='orderByFiled!=null and orderByFiled!="" and orderBySe!=null and orderBySe!=""'>
				ORDER BY #{orderByFiled}  #{orderBySe}
			</when>
			<otherwise>
				ORDER BY A.PAYMENT_TIME DESC
			</otherwise>
		</choose>
	</select>

最簡單的解決辦法是將#換為$,但是這樣會(huì)有sql注入的問題

Mybatis order by動(dòng)態(tài)參數(shù)防注入

先提及一下Mybatis動(dòng)態(tài)參數(shù)

參數(shù)符號(hào)編譯安全
#{}預(yù)編譯安全處理后的值,字符類型都帶雙引號(hào)
${}未預(yù)編譯不安全,存在SQL注入問題傳進(jìn)來啥就是啥

order by 動(dòng)態(tài)參數(shù)

order by 后面參數(shù)值是表字段或者SQL關(guān)鍵字

所以使用#{} 是無效的,只能使用${}

那么SQL注入問題就來了

解決Order by動(dòng)態(tài)參數(shù)注入問題

1、使用正則表達(dá)式規(guī)避

特殊字符 * + - / _ 等等

使用indexOf判斷到了直接返回

有可能會(huì)存在其它情況,不能完全規(guī)避,但是可以逐步排查

2、技巧解決

  • 2.1 先從order by 動(dòng)態(tài)參數(shù)思考

組合情況只有這兩種

  • order by 字段名 (asc直接略掉得了) 
  • order by 字段名 desc

所以我們只要找到對(duì)應(yīng)集合,判斷我們動(dòng)態(tài)排序條件是否在其中就可以了

  • 2.2 獲取排序條件集合

有些勤勞的小伙伴可能就開始寫了

userOrderSet = ['id','id desc','age','age desc',.......]
scoreOrderSet = ['yuwen','yuwen desc','shuxue','shuxue desc',.......]
.............

要是有n多表n多字段可是要急死人

我們使用注解+映射來獲取

  • 2.3 動(dòng)態(tài)獲取集合
/**
首先改造實(shí)體類,有@Column注解映射,也有使用@JsonProperty,都行,沒有用@Column映射,就加個(gè)@JsonProperty,不影響,我偷懶使用@JsonProperty
**/
@Data
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class ContentEntity {
 
    
    @JsonProperty("id")
    private Long id;//主鍵ID
    @JsonProperty("code")
    private String code;//編碼
    @JsonProperty("content")
    private String content;//內(nèi)容
    @JsonProperty("is_del")
    private Integer isDel;//是否刪除,0未刪除,1已刪除
    @JsonProperty("creator")
    private String creator;//創(chuàng)建人
    @JsonProperty("creator_id")
    @JsonFormat(pattern = DatePattern.NORM_DATETIME_PATTERN, timezone = "GMT+8")
    private Date createAt;//創(chuàng)建時(shí)間
    @JsonProperty("updater")
    private String updater;//更新人
    @JsonProperty("updater_id")
    @JsonFormat(pattern = DatePattern.NORM_DATETIME_PATTERN, timezone = "GMT+8")
    private Date updateAt;//更新時(shí)間
 
}
/**工具類來了**/
 
public class MybatisDynamicOrderUtils {
 
    private static final String desc = " desc";
    /**
     * 獲取對(duì)象 JsonProperty 值列表 使用Column替換一下
     * @param object
     * @return
     */
    public static Set<String> getParamJsonPropertyValue(Class<?> object){
        try {
            //獲取filed數(shù)組
            Set<String> resultList =  new HashSet<>();
            Field[] fields = object.getDeclaredFields();
            for (Field field:fields){
                //獲取JsonProperty注解
                if(field.getAnnotation(JsonProperty.class)!=null){
                    JsonProperty annotation = field.getAnnotation(JsonProperty.class);
                    if (annotation != null) {
                        //獲取JsonProperty 的值
                        String jsonPropertyValue = annotation.value();
                        resultList.add(jsonPropertyValue);
                    }
                }
            }
            return resultList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 判斷動(dòng)態(tài)order是否是合理
     * @param order
     * @param object
     * @return
     */
    public static Boolean isDynamicOrderValue(String order,Class<?> object){
        //先獲取JsonProperty 注解中的集合
        Set<String> set = getParamJsonPropertyValue(object);
        //屬于直屬字段 直接返回
        if(set.contains(order)){
            return true;
        }
        //多了倒序,先去除倒序字段再判斷
        if(order.lastIndexOf(desc)>0){
            String temp = order.substring(0,order.lastIndexOf(desc));
            if(set.contains(temp)){
                return true;
            }
        }
        return false;
    }
}
//調(diào)用操作一下
 
//檢驗(yàn)動(dòng)態(tài)order是否合理,防止SQL注入
if(!MybatisDynamicOrderUtils.isDynamicOrderValue(sort,ContentEntity.class)){
            log.error("dynamic order is error:{}",sort);
            return null;
}
 
 
 
//mapper.class
 
@Select({"<script>select  * from content order by ${sort}</script>"})
List<ContentEntity> getList(@Param("sort") String sort);

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論