1:构建登录javaBean
com\springboot\vo\LoginUser.java
package com.springboot.vo;
import lombok.Data;
@Data
public class LoginUser {
//登录用户名
private String username;
//登录密码
private String password;
}
2:构建登录controller
com\springboot\controller\LoginController.java
package com.springboot.controller;
import com.springboot.vo.LoginUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.http.HttpSession;
@Controller
public class LoginController {
@PostMapping("/login.html")
public String login(LoginUser user, Model model, HttpSession session){
//如果用户名不为空且密码等于123456 登录成功
if(!StringUtils.isEmpty(user.getUsername())&&"123456".equals(user.getPassword())){
//登录成功后把用户名放到session中用于登录拦截验证
session.setAttribute("loginUser",user.getUsername());
//main.html请求定义在自定义的视图解析器中
//重定向的意义在于让浏览器的URL路径更加真实
return "redirect:/main.html";
}else {
// 用于登录界面回显错误信息
model.addAttribute("msg","用户名或者密码错误");
return "index";
}
}
}
3:构建登录拦截器
package com.springboot.config;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*登录拦截器*/
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
//从session中获取用户名
String username = (String)request.getSession().getAttribute("loginUser");
//如果用户名为空则不放行
if(StringUtils.isEmpty(username)){
//登录界面回显的错误信息
request.setAttribute("msg","请先登录");
//转发到登录页
request.getRequestDispatcher("/index.html").forward(request,response);
//不放行
return false;
}
//放行
return true;
}
}
4:把登录拦截器添加到spring以及添加主页视图解析器
package com.springboot.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration //配置类注解
public class MyConfig 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();
}
//登录拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor()).
//拦截所有请求
addPathPatterns("/**").
//排除以下请求不拦截
excludePathPatterns("/","/index.html","/login.html",
"/css/**","/js/**","/img/**");
}
}
5:改造index.html
6:测试
输入错误的用户名、密码:
直接访问主页:http://localhost/main.html
输入正确的用户名、密码: