Springboot中攔截GET請求獲取請求參數(shù)驗證合法性核心方法
目的
在Springboot中創(chuàng)建攔截器攔截所有GET類型請求,獲取請求參數(shù)驗證內(nèi)容合法性防止SQL注入(該方法僅適用攔截GET類型請求,POST類型請求參數(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請求校驗參數(shù)內(nèi)容 * @date 2023/8/9 10:12 * @Version 1.0 */ @Component public class SqlInterceptor implements HandlerInterceptor { /** * @Author: ljh * @Description: 在controller前攔截請求 * @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請求中的參數(shù),例如http://127.0.0.1:8088/api/checkTechCertInfoCancel?name=ljh 請求中的參數(shù)ljh Map<String, String[]> parameterMap = request.getParameterMap(); for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) { String[] value = entry.getValue(); for (String s : value) { //校驗參數(shù)值是否合法 if (verifySql(s)) { response.setContentType("application/json;charset=utf-8"); Result result = new Result(); result.setMessage("請求參數(shù)中含有非法字符!請檢查重新輸入"); result.setStatus(500); response.getWriter().write(JSON.toJSONString(result)); return false; } } } } else { //獲取EGT請求中的參數(shù),例如http://127.0.0.1:8088/api/checkTechCertInfoCancel/ljh 請求中的參數(shù)ljh Map<String, String> pathVariables = (Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); for (String key : pathVariables.keySet()) { //校驗參數(shù)值是否合法 if (verifySql(pathVariables.get(key))) { //返回錯誤提示 response.setContentType("application/json;charset=utf-8"); Result result = new Result(); result.setMessage("請求參數(shù)中含有非法字符!請檢查重新輸入"); result.setStatus(500); response.getWriter().write(JSON.toJSONString(result)); return false; } } } return true; } //處理請求完成后視圖渲染之前的處理操作 @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: 校驗非法字符 * @DateTime: 11:15 2023/8/9 * @Params: * @Return */ public boolean verifySql(String parameter) { String s = parameter.toLowerCase(); // 過濾掉的sql關(guā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|" + "'\\*|\\;|\\-|\\--|\\+|\\,|\\//|\\/|\\%|\\#"; //使用正則表達式進行匹配 boolean matches = s.matches(badStr); return matches; }
注冊攔截器
/** * @Author: ljh * @ClassName MvcInterceptorConfig * @Description 注冊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()) //需要注冊到容器中的攔截器 .addPathPatterns("/**"); //所有請求都被攔截,靜態(tài)資源也被攔截 // .excludePathPatterns("/", "/login", "/css/**", "/fonts/**", "/images/**", "/js/**"); // 放行的請求 } }
測試效果
請求內(nèi)容合法:
請求內(nèi)容不合法:
Springboot中使用過濾器校驗PSOT類型請求參數(shù)內(nèi)容
到此這篇關(guān)于Springboot中攔截GET請求獲取請求參數(shù)驗證合法性的文章就介紹到這了,更多相關(guān)Springboot攔截GET請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
RocketMQ NameServer保障數(shù)據(jù)一致性實現(xiàn)方法講解
這篇文章主要介紹了RocketMQ NameServer保障數(shù)據(jù)一致性實現(xiàn)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12如何使用會話Cookie和Java實現(xiàn)JWT身份驗證
這篇文章主要介紹了如何使用會話Cookie和Java實現(xiàn)JWT身份驗證,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2021-03-03Jersey實現(xiàn)Restful服務(wù)(實例講解)
下面小編就為大家?guī)硪黄狫ersey實現(xiàn)Restful服務(wù)(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08@Resource和@Autowired兩個注解的區(qū)別及說明
這篇文章主要介紹了@Resource和@Autowired兩個注解的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06