login.html
<body class="text-center">
<form class="form-signin" action="dashboard.html" th:action="@{user/login}" th:method="post">
<img class="mb-4" th:src="@{/asserts/img/bootstrap-solid.svg}" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
<label class="sr-only" >Username</label>
<p style="color:red;" th:text="${message}" th:if="${not #strings.isEmpty(message)}"></p>
<input type="text" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="" th:name="username">
<label class="sr-only">Password</label>
<input type="password" class="form-control" placeholder="Password" th:name="password" th:placeholder="#{login.password}" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me">[[#{login.remember}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_us')}">English</a>
</form>
</body>
注意:提交路径书写格式: th:action="@{user/login}",@{}传入的是路径
2.springmvc扩展类:
MymvcConfig.java
package com.springboot.config;
import com.springboot.component.LoginInterceptor;
import com.springboot.component.MyLocaleResolver;
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.*;
/**
-
@author jack
-
@create 2019-07-08 13:45
-
定义WebMvcConfigurerAdapter类来扩展springmvc的功能
*/
//@EnableWebMvc
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送/springboot请求就可以跳转到success,在不需要添加数据的时候就可以使用这种方式
registry.addViewController("/springboot").setViewName(“success”);}
@Bean
public WebMvcConfigurerAdapter getWebMvcConfigurerAdapter(){
WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter(){
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName(“login”);
registry.addViewController("/index.html").setViewName(“login”);
registry.addViewController("/main.html").setViewName(“dashboard”);} /** * 配置拦截器 * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()).excludePathPatterns("/","/index.html","user/login") .addPathPatterns("/**"); } }; return adapter ;
}
3.登录处理器:
LoginController.java
package com.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
-
@author jack
-
@create 2019-07-08 21:45
*/
@Controller
public class LoginController {@PostMapping("/user/login")
public String adminLogin(@RequestParam(“username”) String username, @RequestParam(“password”) String password, Map<String,Object>map, HttpSession session){
if (!StringUtils.isEmpty(username)&&“123”.equals(password)){//session.setAttribute("admin",username); //防止多次刷新 return "redirect:/main.html" ; } map.put("message","用户名或密码错误") ; return "login" ;
}
}
4.拦截器拦截用户登录
package com.springboot.component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
-
@author jack
-
@create 2019-07-08 22:21
*/
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
Object user = httpServletRequest.getSession().getAttribute(“admin”);
if (user == null) {
httpServletRequest.setAttribute(“message”,“请先登录”);
httpServletRequest.getRequestDispatcher("/index.html").forward(httpServletRequest, httpServletResponse);
return false;
} else {return true; }
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {}
}
注意拦截器在书写完成之后需要添加到WebMvcConfigurerAdapter 中,这样springboot才能识别并使用
需要注意的地方:
1.在进行登录时防止二次刷新,可以把页面重定向到自己添加的视图
2.thymeleaf模板引擎有缓存,需要在主配置文件中添加配置禁用缓存
#禁用模板引擎缓存
spring.thymeleaf.cache=false
开发期间模板引擎页面修改以后,要实时生效
# 禁用缓存
spring.thymeleaf.cache=false
2)、页面修改完成以后ctrl+f9:重新编译;
一种重要的思想:
使用配置类完成springmvc配置并且可以添加视图,如果页面本身是html页面,可以添加视图,并通过重定向或者转发的方式跳转到目标页面