營(yíng)銷(xiāo)網(wǎng)站建設(shè)是什么網(wǎng)站設(shè)計(jì)的流程
一、統(tǒng)一用戶登錄權(quán)限驗(yàn)證
1.1Spring攔截器
實(shí)現(xiàn)攔截器需要以下兩步:
1.創(chuàng)建自定義攔截器,實(shí)現(xiàn) HandlerInterceptor 接?的 preHandle(執(zhí)行具體方法之前的預(yù)處理)方法。
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)建一個(gè)登錄的攔截器*/
public class LoginInterceptor implements HandlerInterceptor {//返回true表示驗(yàn)證通過(guò),可以執(zhí)行后面的方法;// 但是返回false表示驗(yàn)證失敗,后面的代碼就不能執(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í)行到此行,表明驗(yàn)證未通過(guò),自動(dòng)跳轉(zhuǎn)到登錄頁(yè)面response.sendRedirect("login.html");return false;}
}
1.1.2將攔截器配置給當(dāng)前項(xiàng)目
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;/*** 給當(dāng)前項(xiàng)目添加攔截器* @param registry*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(loginInterceptor).addPathPatterns("/**")//攔截使用的url.excludePathPatterns("/user/reg")//不攔截/user/reg.excludePathPatterns("/**/*.html");//攔截/**/*.html}
}
1.2攔截器實(shí)現(xiàn)原理
二、統(tǒng)?異常處理
使用 @ControllerAdvice + @ExceptionHandler來(lái)實(shí)現(xiàn)
@ControllerAdvice:控制器通知類(lèi)
@ExceptionHandler:異常處理器
結(jié)合表示當(dāng)出現(xiàn)異常的時(shí)候執(zhí)行某個(gè)通知。
2.1創(chuàng)建異常類(lèi)
添加@ControllerAdvice 注解
2.2實(shí)現(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//對(duì)控制器進(jìn)行功能增強(qiáng)(當(dāng)前類(lèi)為統(tǒng)一封裝類(lèi))
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","默認(rèn)異常"+e.getMessage());result.put("data",null);return result;}
}
三、統(tǒng)?數(shù)據(jù)格式返回
以使? @ControllerAdvice + ResponseBodyAdvice實(shí)現(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 {//是否要重寫(xiě)的方法改為true,true表示在返回?cái)?shù)據(jù)之前,進(jìn)行統(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;}
}