1.把login頁(yè)面也放入Thymeleaf模板引擎管理范圍
通過創(chuàng)建自定義配置類懊直,實(shí)現(xiàn)WebMvcConfigurer接口,可以擴(kuò)展SpringBoot默認(rèn)的MVC配置
作用:在現(xiàn)有自動(dòng)SpringBoot自動(dòng)配置的基礎(chǔ)上胳泉,疊加自定義配置信息宿亡。例如:
-1. Spring MVC的多個(gè)路徑的路由重定向来氧;
-2. Spring MVC的攔截器設(shè)置舒憾;
實(shí)現(xiàn)方式:在項(xiàng)目路徑下新建config——MyMvcConfig類,將其標(biāo)注為:@Configuration居灯,并實(shí)現(xiàn)WebMvcConfigurer接口祭务,即可重寫其現(xiàn)有配置的方法内狗。
技巧:IDEA中繼承父類,可以通過快捷鍵Ctrl+o义锥,調(diào)出所有可重寫的方法柳沙,并選擇它。
? IDEA中如果需要快速引入資源拌倍,可以在提示錯(cuò)誤的紅色字中使用快捷鍵Alt+Ins赂鲤,自動(dòng)完成import引入。
例如:重寫視圖映射(重定向):把login.html放入Thymeleaf模板引擎的templates目錄中柱恤,實(shí)現(xiàn)對(duì)靜態(tài)資源的訪問可重定向到指定路徑下数初,從而隱藏靜態(tài)資源真實(shí)路徑。
package com.zhbit.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//視圖映射:http://localhost/login重定向到模板引擎中的login.html
registry.addViewController("/login").setViewName("login");
registry.addViewController("/main.html").setViewName("homepage");
}
}
擴(kuò)展知識(shí):如果不想要SpringMVC的自動(dòng)配置梗顺,想只讀取本自定義配置類時(shí):
可以通過@EnableWebMvc
標(biāo)注自定義的配置類MyMvcConfig泡孩,全面接管MVC配置。
2.登錄失敗寺谤,當(dāng)前頁(yè)提示
當(dāng)用戶登錄失敗時(shí)仑鸥,起始我們不需要跳轉(zhuǎn)到一個(gè)單獨(dú)的錯(cuò)誤頁(yè)面,只需要在當(dāng)前登錄頁(yè)面顯示錯(cuò)誤提示即可变屁。這樣既可實(shí)現(xiàn)提示作用眼俊,也能快速進(jìn)行二次登錄操作。
因此粟关,當(dāng)用戶所輸入的登錄信息有誤時(shí)疮胖,只需刷新頁(yè)面,并顯示錯(cuò)誤信息參數(shù)msg即可誊役。
<!--當(dāng)頁(yè)面獲取到msg參數(shù)获列,且msg不為空時(shí)谷市,顯示當(dāng)前<p>標(biāo)簽的內(nèi)容-->
<p style="coler:red" th:if="${not #string.isEmpty{msg}}" th:text="${msg}">
</p>
//對(duì)應(yīng)登錄功能的控制器中修改跳轉(zhuǎn)目的地
@PostMapping(value = "/logon")
public String login(Map<String,Object> map , ……)
{
if(!StringUtils.isEmpty(username) ){
//登錄成功
return "homepage";
}else{
map.put("msg","用戶名密碼錯(cuò)誤");
return "login"; //返回登錄頁(yè)面
}
}
3.登錄成功重定向到main.html
用戶登錄成功后蛔垢,自動(dòng)跳轉(zhuǎn)到后臺(tái)主頁(yè);此時(shí)若用戶刷新迫悠,由于服務(wù)器并沒有記錄當(dāng)前已經(jīng)登錄的用戶信息鹏漆,因此頁(yè)面會(huì)提示重新提交表單。
為了防止多次提交表單创泄,用戶登錄成功后不能直接跳轉(zhuǎn)到后臺(tái)主頁(yè)homepage.html艺玲,需要經(jīng)過一個(gè)中介main.html后進(jìn)行重定向到后臺(tái)主頁(yè)homepage.html。
//對(duì)應(yīng)登錄功能的控制器中修改跳轉(zhuǎn)目的地
@PostMapping(value = "/logon")
public String login(Map<String,Object> map , ……)
{
if(!StringUtils.isEmpty(username) ){
//登錄成功鞠抑,重定向到main.html饭聚,通過MyMvcConfig中擴(kuò)展的視圖映射,可以重定向跳轉(zhuǎn)到homepage頁(yè)面中搁拙。
return "redirect:/main.html";
}else{
map.put("msg","用戶名密碼錯(cuò)誤");
return "login"; //返回登錄頁(yè)面
}
}
技巧:IDEA在運(yùn)行期間秒梳,快捷鍵Ctrl+F9法绵,重新編譯當(dāng)前html頁(yè)面。
4.登錄成功后酪碘,保存到session中
放了解決用戶每次訪問都需要重新登錄朋譬,通過服務(wù)器會(huì)話功能session,在一定時(shí)間內(nèi)記錄用戶的已登錄狀態(tài)兴垦。
@PostMapping(value = "/logon")
public String login(Map<String,Object> map , ……, HttpSession session)
{
if(!StringUtils.isEmpty(username) ){
//把登錄成功后的用戶信息徙赢,保存到session中
session.setAttribute("loginUser",username);
return "redirect:/main.html";
}else{
map.put("msg","用戶名密碼錯(cuò)誤");
return "login"; //返回登錄頁(yè)面
}
}
5.攔截沒有登錄的請(qǐng)求直接訪問main.html
如果用戶知道后臺(tái)地址,直接訪問http://localhost/main.html探越,則越過了用戶登錄驗(yàn)證狡赐。此時(shí),可以使用Spring中的攔截器钦幔,實(shí)現(xiàn)所有頁(yè)面的登錄檢查阴汇。
步驟一:在component目錄下創(chuàng)建一個(gè)LoginHandlerInterceptor攔截器
此攔截器功能:對(duì)所有頁(yè)面進(jìn)行登錄檢查。
public class LoginHandlerInterceptor implements HandlerInterceptro{
//目標(biāo)方法執(zhí)行之前被觸發(fā)
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler){
//獲取session中的用戶信息
Object user = request.getSession().getAttribute("loginUser");
//如果session中存在用戶信息节槐,表示已登錄驗(yàn)證過了搀庶,直接放行;
//如果session中不存在用戶信息铜异,則表示沒有登錄哥倔,重定向到login.html
if(user == null){
request.setAttribute("msg","沒有權(quán)限,請(qǐng)先完成登錄操作");
request.getRequestDispatcher("login.html").forward(request,response);
return false;
}else{
//return true;
}
}
//目標(biāo)方法
@Override
public boolean postHandle(HttpServletRequest request, HttpServletResponse response,Object handler,Model model){
}
//
@Override
public boolean afterComletion(HttpServletRequest request, HttpServletResponse response,Object handler){
}
}
步驟二:在自定義配置類MyMvcConfig中把攔截器配置上揍庄。
在自定義擴(kuò)展配置類中咆蒿,注冊(cè)剛自定義的攔截器對(duì)象,讓Spring容器調(diào)用它蚂子。
package com.zhbit.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//視圖映射:http://localhost/login重定向到模板引擎中的login.html
registry.addViewController("/login").setViewName("login");
registry.addViewController("/main.html").setViewName("homepage");
}
//注冊(cè)攔截器
@Override
public void addInterceptors(InterceptorRegistry registry){
//addInterceptor()方法是往容器中追加一個(gè)攔截器對(duì)象沃测;
//攔截器的addPathPatterns()表示要攔截哪些請(qǐng)求。
//攔截器的addPathPatterns(“/**”)表示要攔截所有的請(qǐng)求食茎;
//攔截器的excludePathPatterns("")表示除哪些路徑之外(白名單機(jī)制)蒂破。
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/login.html","/");
}
}