springboot_簡單使用者登入和攔截器
阿新 • • 發佈:2021-06-28
1、為登入頁面的form表單,新增th:action進入controller
<form class="form-signin" th:action="@{/user/login}">
2、input標籤新增“name”屬性
<input type="text" name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus=""> <input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="">
3、進入controller實現具體的業務程式碼
新建controller
@Controller public class LoginController { @RequestMapping("/user/login") public String login( @RequestParam("username")String username, @RequestParam("password")String password, Model model, HttpSession session){ //具體業務: if (!StringUtils.isEmpty(username) && "123456".equals(password)){ System.out.println(username+":"+password); session.setAttribute("loginUser",username); //重定向 return "redirect:/main.html"; }else { model.addAttribute("msg","使用者名稱或密碼錯誤"); return "index"; } } }
如果密碼錯誤頁面顯示錯誤資訊:
<!--th:if thymeleaf模板語法,進行條件判斷-->
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
img.png
- @RequestParam:獲取登入頁面的username和password引數
- Model:進行資料的回顯
- HttpSession:用於設定session,用於後續攔截器的判斷條件
4、在MyMvcConfig進行地址對映
@Configuration public class MyMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); registry.addViewController("/index.html").setViewName("index"); registry.addViewController("/main.html").setViewName("dashboard"); } }
對映前uri:
http://localhost:8080/user/login?username=123&password=123456
對映後uri:
http://localhost:8080/main.html
5、攔截器設定
建立LoginHandlerInterceptor實現HandlerInterceptor介面
重寫方法:
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {}
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//登入成功之後。應該有使用者的session
Object loginUser = request.getSession().getAttribute("loginUser");
//沒有登入
if (loginUser==null){
request.setAttribute("msg","請先登入");
//返回主頁,轉發request和response
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}else {
return true;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
6、在MyMvcConfig中配置攔截器
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard");
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
//配置攔截器
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(new LoginHandlerInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/index.html",
"/",
"/user/login",
"/css/**",
"/js/**",
"/img/**");
}
}
如果沒有登入提示使用者登入
img_1.png