springboot+mybatis登錄注冊


接上上一篇博客的繼續往下做,上一篇已經實現了mybatis自動生成代碼,和連接數據庫測試部分

本篇我們添加一些功能,實現登錄注冊,時間原因,前端實現的很粗糙,以后有時間再改吧

 

首先看一下數據庫的構成,作為一個例子實現的簡單點

看了一下mybatis自動生成的代碼,不太好用,所以我們自己手寫sql,然后按照Dao,Service,Controller的順序修改代碼

工程結構:

 

具體的代碼放Github里了

看一下結果吧

首頁:

點擊注冊:

 

 下一步實現攔截器、shiro等功能

 

5.26更新:

實現攔截器的功能,如果不登錄的話,就自動跳轉到登錄頁面,只有登錄了才能訪問別的頁面

這次的文件結構如下:

新加攔截器:

 1 import org.springframework.web.servlet.HandlerInterceptor;
 2 import org.springframework.web.servlet.ModelAndView;
 3 
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpServletResponse;
 6 import javax.servlet.http.HttpSession;
 7 
 8 public class MyInterceptor implements HandlerInterceptor {
 9     //在請求處理之前進行調用(Controller方法調用之前
10     @Override
11     public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
12         HttpSession session = httpServletRequest.getSession();
13         Object user = session.getAttribute("session_user"); //獲取登錄的session信息
14         if(user != null){
15             System.out.println(user);
16             return true;
17         }
18         else{
19             System.out.println("沒登錄");
20             httpServletRequest.setAttribute("msg","沒有權限請先登陸");
21             httpServletResponse.sendRedirect("/");  //未登錄自動跳轉界面
22             return false;
23         }
24     }
25 
26     //請求處理之后進行調用,但是在視圖被渲染之前(Controller方法調用之后)
27     @Override
28     public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
29         System.out.println("postHandle被調用\n");
30     }
31 
32     //在整個請求結束之后被調用,也就是在DispatcherServlet 渲染了對應的視圖之后執行(主要是用於進行資源清理工作)
33     @Override
34     public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
35         System.out.println("afterCompletion被調用\n");
36     }
37 }
MyInterceptor

新加配置文件:

 1 import com.example.mybatis.interceptor.MyInterceptor;
 2 import org.springframework.context.annotation.Configuration;
 3 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 4 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 5 
 6 @Configuration
 7 public class WebMvcConfig implements WebMvcConfigurer {
 8     /**
 9      * 注冊攔截器
10      */
11     @Override
12     public void addInterceptors(InterceptorRegistry registry) {
13         //addPathPattern后跟攔截地址,excludePathPatterns后跟排除攔截地址
14         registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/userLogin").excludePathPatterns("/index").excludePathPatterns("/");
15     }
16 }
WebMvcConfig

並修改Controller中的一些東西

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM