srpingboot攔截器
- 2022 年 5 月 8 日
- 筆記
- springboot
1、創建一個普通的web工程。
2、先創建需要的基礎文件,比如一個用戶類。
package com.example.mode; public class User { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
再創建一個監聽類,這個類繼承自HandlerInterceptor,然後,在這個類里就可以處理一些監聽到的資訊,比如有的登陸,有的沒有登陸。
package com.example.interceptor; import com.example.mode.User; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //這裡編寫攔截規則,可以從session中獲取用戶資訊,這個名為user的attribute對象如果為空,就返回error,因為沒有登陸 // 如果這個對象存在,就返回true,不做任何處理。 User user= (User) request.getSession().getAttribute("user"); if(user==null) { response.sendRedirect(request.getContextPath()+"/user/error"); return false; } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
這裡還要建一個監聽配置類,這個配置類來決定監聽的對象集合,監聽的類型,這裡主要是監聽web網頁,所以,還要實現一個
WebMvcConfigurer介面,然後重寫這個介面里的addInterceptor程式,把上面的MyInterceptor監聽類,添加到web網頁監聽中去。
package com.example.config; import com.example.interceptor.MyInterceptor; 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 MyInterceptorConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { //要攔截的路徑集合 String[] addPathPatterns={"/user/**"}; //要排除的路徑集合 String[] excludePathPatterns={"/user/out","/user/error","/user/login"}; //將規則集合添加到註冊中 registry.addInterceptor(new MyInterceptor()) .addPathPatterns(addPathPatterns) .excludePathPatterns(excludePathPatterns); } }
這些工作都做完了,現在可以去編寫控制類了,
package com.example.control; import com.example.mode.User; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; //這個注釋是表明這個類是一個Controller,還有就是以下所有方法都帶ResponseBody @RestController //這裡是為了方便,以後所有的指向都會默認指向/user/後面的路徑了 @RequestMapping("/user") public class MyController { // 以下為登陸方法,在裡面可以檢查登陸資訊,然後填充request對象,再定向到需要的頁面。 @RequestMapping("/login") public Object login(HttpServletRequest request){ User user=new User(); user.setId(1001); user.setName("翠花"); request.getSession().setAttribute("user",user); return "登陸成功"; } // 登陸成功以後,定位到用戶中心頁面 @RequestMapping("/center") public Object center(HttpServletRequest request){ User user=(User) request.getSession().getAttribute("user"); return "歡迎編號為:"+user.getId()+" 用戶:"+user.getName(); } // 沒有登陸的用戶訪問的內容 @RequestMapping("/out") public Object out(){ return "普通用戶訪問頁面"; } // 當非登陸用戶訪問到某些頁面時會定向到出錯頁面。 @RequestMapping("/error") public Object error(){ return "請登陸後再訪問,謝謝!"; } }
然後就可以直接運行了。