營銷網站建設是什么網站設計的流程
一、統(tǒng)一用戶登錄權限驗證
1.1Spring攔截器
實現(xiàn)攔截器需要以下兩步:
1.創(chuàng)建自定義攔截器,實現(xiàn) HandlerInterceptor 接?的 preHandle(執(zhí)行具體方法之前的預處理)方法。
2.將?定義攔截器加? WebMvcConfigurer 的 addInterceptors 方法中
1.1.1自定義攔截器
package com.example.demo.interceptor;import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;/*** 創(chuàng)建一個登錄的攔截器*/
public class LoginInterceptor implements HandlerInterceptor {//返回true表示驗證通過,可以執(zhí)行后面的方法;// 但是返回false表示驗證失敗,后面的代碼就不能執(zhí)行了@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {HttpSession session = request.getSession(false);if (session != null && session.getAttribute("userinfo") != null){//表明用戶已登錄return true;}//執(zhí)行到此行,表明驗證未通過,自動跳轉到登錄頁面response.sendRedirect("login.html");return false;}
}
1.1.2將攔截器配置給當前項目
package com.example.demo.config;import com.example.demo.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
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 AppConfig implements WebMvcConfigurer {@Autowiredprivate LoginInterceptor loginInterceptor;/*** 給當前項目添加攔截器* @param registry*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(loginInterceptor).addPathPatterns("/**")//攔截使用的url.excludePathPatterns("/user/reg")//不攔截/user/reg.excludePathPatterns("/**/*.html");//攔截/**/*.html}
}
1.2攔截器實現(xiàn)原理
二、統(tǒng)?異常處理
使用 @ControllerAdvice + @ExceptionHandler來實現(xiàn)
@ControllerAdvice:控制器通知類
@ExceptionHandler:異常處理器
結合表示當出現(xiàn)異常的時候執(zhí)行某個通知。
2.1創(chuàng)建異常類
添加@ControllerAdvice 注解
2.2實現(xiàn)異常的封裝方法
添加@ExceptionHandler注解
package com.example.demo.config;import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.HashMap;@ControllerAdvice//對控制器進行功能增強(當前類為統(tǒng)一封裝類)
public class MyExceptionResult {@ResponseBody@ExceptionHandler(Exception.class)public HashMap<String,Object> myException(Exception e){HashMap<String,Object> result = new HashMap<String,Object>();result.put("state",-1);result.put("msg","默認異常"+e.getMessage());result.put("data",null);return result;}
}
三、統(tǒng)?數據格式返回
以使? @ControllerAdvice + ResponseBodyAdvice實現(xiàn)
package com.example.demo.config;import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;import java.util.HashMap;@ControllerAdvice
public class MyResponseBodyAdvice implements ResponseBodyAdvice {//是否要重寫的方法改為true,true表示在返回數據之前,進行統(tǒng)一的格式封裝@Overridepublic boolean supports(MethodParameter returnType, Class converterType) {return true;}@Overridepublic Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {HashMap<String,Object> result = new HashMap<>();result.put("state",1);result.put("data",body);result.put("msg","");return result;}
}