Springboot中攔截GET請(qǐng)求獲取請(qǐng)求參數(shù)驗(yàn)證合法性核心方法
目的
在Springboot中創(chuàng)建攔截器攔截所有GET類型請(qǐng)求,獲取請(qǐng)求參數(shù)驗(yàn)證內(nèi)容合法性防止SQL注入(該方法僅適用攔截GET類型請(qǐng)求,POST類型請(qǐng)求參數(shù)是在body中,所以下面方法不適用)。
核心方法
1、攔截 http://127.0.0.1:8088/api/checkTechCertInfoCancel?name=ljh 類型:
Map<String, String[]> parameterMap = request.getParameterMap();
2、攔截http://127.0.0.1:8088/api/checkTechCertInfoCancel/ljh 類型:
Map<String, String> pathVariables = (Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
完整代碼
創(chuàng)建攔截器
import com.alibaba.fastjson.JSON; import com.boc.ljh.utils.Result; import com.boc.ljh.utils.status.AppErrorCode; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Map; /** * @Author: ljh * @ClassName SqlInterceptor * @Description 攔截器 用于攔截GET請(qǐng)求校驗(yàn)參數(shù)內(nèi)容 * @date 2023/8/9 10:12 * @Version 1.0 */ @Component public class SqlInterceptor implements HandlerInterceptor { /** * @Author: ljh * @Description: 在controller前攔截請(qǐng)求 * @DateTime: 10:38 2023/8/9 * @Params: * @Return */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.err.println(request.getMethod()); if (request.getMethod().equals("GET") && request.getRequestURI().contains("?")) { //獲取EGT請(qǐng)求中的參數(shù),例如http://127.0.0.1:8088/api/checkTechCertInfoCancel?name=ljh 請(qǐng)求中的參數(shù)ljh Map<String, String[]> parameterMap = request.getParameterMap(); for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) { String[] value = entry.getValue(); for (String s : value) { //校驗(yàn)參數(shù)值是否合法 if (verifySql(s)) { response.setContentType("application/json;charset=utf-8"); Result result = new Result(); result.setMessage("請(qǐng)求參數(shù)中含有非法字符!請(qǐng)檢查重新輸入"); result.setStatus(500); response.getWriter().write(JSON.toJSONString(result)); return false; } } } } else { //獲取EGT請(qǐng)求中的參數(shù),例如http://127.0.0.1:8088/api/checkTechCertInfoCancel/ljh 請(qǐng)求中的參數(shù)ljh Map<String, String> pathVariables = (Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); for (String key : pathVariables.keySet()) { //校驗(yàn)參數(shù)值是否合法 if (verifySql(pathVariables.get(key))) { //返回錯(cuò)誤提示 response.setContentType("application/json;charset=utf-8"); Result result = new Result(); result.setMessage("請(qǐng)求參數(shù)中含有非法字符!請(qǐng)檢查重新輸入"); result.setStatus(500); response.getWriter().write(JSON.toJSONString(result)); return false; } } } return true; } //處理請(qǐng)求完成后視圖渲染之前的處理操作 @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub } //視圖渲染之后的操作 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub } /** * @Author: ljh * @Description: 校驗(yàn)非法字符 * @DateTime: 11:15 2023/8/9 * @Params: * @Return */ public boolean verifySql(String parameter) { String s = parameter.toLowerCase(); // 過濾掉的sql關(guān)鍵字,特殊字符前面需要加\\進(jìn)行轉(zhuǎn)義 String badStr = "select|update|and|or|delete|insert|truncate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute|table|" + "char|declare|sitename|xp_cmdshell|like|from|grant|use|group_concat|column_name|" + "information_schema.columns|table_schema|union|where|order|by|" + "'\\*|\\;|\\-|\\--|\\+|\\,|\\//|\\/|\\%|\\#"; //使用正則表達(dá)式進(jìn)行匹配 boolean matches = s.matches(badStr); return matches; }
注冊(cè)攔截器
/** * @Author: ljh * @ClassName MvcInterceptorConfig * @Description 注冊(cè)SqlInterceptor攔截器到容器中 * @date 2023/8/9 10:21 * @Version 1.0 */ import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class MvcInterceptorConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new SqlInterceptor()) //需要注冊(cè)到容器中的攔截器 .addPathPatterns("/**"); //所有請(qǐng)求都被攔截,靜態(tài)資源也被攔截 // .excludePathPatterns("/", "/login", "/css/**", "/fonts/**", "/images/**", "/js/**"); // 放行的請(qǐng)求 } }
測(cè)試效果
請(qǐng)求內(nèi)容合法:
請(qǐng)求內(nèi)容不合法:
Springboot中使用過濾器校驗(yàn)PSOT類型請(qǐng)求參數(shù)內(nèi)容
到此這篇關(guān)于Springboot中攔截GET請(qǐng)求獲取請(qǐng)求參數(shù)驗(yàn)證合法性的文章就介紹到這了,更多相關(guān)Springboot攔截GET請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot GET和POST請(qǐng)求參數(shù)獲取方式小結(jié)
- springboot如何接收get和post請(qǐng)求參數(shù)
- SpringBoot如何獲取Get請(qǐng)求參數(shù)詳解
- springboot中Getmapping獲取參數(shù)的實(shí)現(xiàn)方式
- SpringBoot用實(shí)體接收Get請(qǐng)求傳遞過來的多個(gè)參數(shù)的兩種方式
- SpringBoot常見get/post請(qǐng)求參數(shù)處理、參數(shù)注解校驗(yàn)及參數(shù)自定義注解校驗(yàn)詳解
- 解決Springboot get請(qǐng)求是參數(shù)過長的情況
- Springboot接收Get參數(shù)實(shí)踐過程
相關(guān)文章
Mybatis 自動(dòng)映射(使用需謹(jǐn)慎)
這篇文章主要介紹了Mybatis 自動(dòng)映射(使用需謹(jǐn)慎),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10RocketMQ NameServer保障數(shù)據(jù)一致性實(shí)現(xiàn)方法講解
這篇文章主要介紹了RocketMQ NameServer保障數(shù)據(jù)一致性實(shí)現(xiàn)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12如何使用會(huì)話Cookie和Java實(shí)現(xiàn)JWT身份驗(yàn)證
這篇文章主要介紹了如何使用會(huì)話Cookie和Java實(shí)現(xiàn)JWT身份驗(yàn)證,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2021-03-03Jersey實(shí)現(xiàn)Restful服務(wù)(實(shí)例講解)
下面小編就為大家?guī)硪黄狫ersey實(shí)現(xiàn)Restful服務(wù)(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08@Resource和@Autowired兩個(gè)注解的區(qū)別及說明
這篇文章主要介紹了@Resource和@Autowired兩個(gè)注解的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06