@chenbinghua
2018-08-13T08:14:07.000000Z
字数 1588
阅读 1183
SpringBoot
假如一个网站有两个网页,login.html,index.html。我们希望是如果直接访问index页面,如果没有登录的话,直接跳转到login进行登录。
这其实是很多网站都有的一个登陆拦截的需求。
我们先搭建SpringBoot框架,对外能提供login.html,index.html这两个网页的访问。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
在src/main/resouces/templates编写login.html,index.html两个文件
index.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title></head><body><h3>这是index文件</h3></body></html>
login.html
<!DOCTYPE html><html xmlns:th="http://www.w3.org/1999/xhtml"><head><meta charset="UTF-8" /><title>Insert title here</title></head><body>错误信息:<h4 th:text="${msg}"></h4><form action="" method="post"><p>账号:<input type="text" name="username" value="admin"/></p><p>密码:<input type="text" name="password" value="123456"/></p><p><input type="submit" value="登录"/></p></form></body>
package com.chen.springbootshiro.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloController {@RequestMapping("/index")public String index(){return "index";}@RequestMapping("/login")public String login(){return "login";}}
运行springboot的配置类
在浏览器输入http://localhost:8080/index,http://localhost:8080/login,都能访问到对应的页面。
至此,我们第一步的代码就完成了,但是我们还没实现文章开头的那个简单的需求,我们希望是如果直接访问index页面,如果没有登录的话,直接跳转到login进行登录。
下一篇文章,我们将在当前代码集成shiro,实现上述功能。
