创建项目
创建一个spring boot 和Thymeleaf3 项目,参考下面两个博客。
Spring Boot (一)IDEA 创建spring boot 项目
Thymeleaf3 (二)使用spring boot 创建 thymeleaf项目
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.softtool</groupId>
<artifactId>login</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>login</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
编写登录和登录成功页面: 登录页:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form action="/index/login" method="post" th:method="post">
<input type="text" name="username" placeholder="username"><br>
<input type="password" name="password" placeholder="password"><br>
<button>提交</button><br>
<p th:if="${not #strings.isEmpty(msg)}" th:text="${msg}"></p>
</form>
</body>
</html>
成功页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<h1>success</h1>
</body>
</html>
启动类:
package com.softtool.login;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LoginApplication {
public static void main(String[] args) {
SpringApplication.run(LoginApplication.class, args);
}
}
登录控制器
package com.softtool.login.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Controller
public class LoginController {
@RequestMapping("/")
public String index(){
System.out.println("index");
return "login";
}
@RequestMapping("/success")
public String success(){
System.out.println("success");
return "success";
}
@PostMapping("/index/login")
public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpSession session){
System.out.println("@PostMapping(\"/index/login\")");
if ("1".equals(username) && "2".equals(password)){
session.setAttribute("isLogin",true);
return "redirect:/success";
}
Map<String, String> map=new HashMap<>();
map.put("msg","用户名或者密码错误");
model.addAllAttributes(map);
return "login";
}
}
登录拦截器
package com.softtool.login.component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//登录检查
public class LoginHandler implements HandlerInterceptor {
//目标方法执行之前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
Boolean isLogin = (Boolean) request.getSession().getAttribute("isLogin");
System.out.println("isLogin="+isLogin);
//未登录,返回登录界面
if (isLogin==null || isLogin==false){
//提示错误信息
request.setAttribute("msg","没有权限");
//转发到登录页
request.getRequestDispatcher("/").forward(request,response);
return false;
}
//已经登录,放行请求
return true;
}
}
使得登录拦截器生效
package com.softtool.login.component;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
@Configuration
public class MyConfig implements WebMvcConfigurer {
//注册拦截器,不注册,拦截器不生效
@Override
public void addInterceptors(InterceptorRegistry registry) {
System.out.println("----进入拦截器-----");
//新增一个登录的拦截器
registry.addInterceptor(new LoginHandler())
.addPathPatterns("/**")
.excludePathPatterns("/index","/index/login","/");
}
}
登录页面:
成功页