1、
<form class="form-signin" th:action="@{/user/login}" method="post">
controller包中新建类
package com.cnstrong.springboot04webrestfulcrud.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;
import java.util.Map;
@Controller
public class LoginController {
// @DeleteMapping
// @PutMapping
// @GetMapping
@PostMapping("/user/login")
//@RequestMapping(value = "/user/login",method = RequestMethod.POST)
public String login(@RequestParam("username") String username,
@RequestParam("password") String password,
Map<String,Object> map){
if(!StringUtils.isEmpty(username) && "123456".equals(password)) {
//登录成功
return "dashboard";
}else{
//登录失败
map.put("msg","用户名密码错误");
return "login";
}
}
}
login.html提交字段要写name属性
开发期间模板引擎页面修改以后要实时生效的方法:
application.properties中
1)#禁用掉模板引擎的缓存
spring.thymeleaf.cache=false
配置文件修改了需要重启服务器
2)页面修改完成后 ctrl+F9 重新编译
工具表达式对象
#strings:String对象的⽅法:contains,startsWith,prepending / appending等
<!--判断-->
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
满足if才有p标签,否则不生成
2、防止表单重定向
修改MyMvcConfig类
package com.cnstrong.springboot04webrestfulcrud.config;
import com.cnstrong.springboot04webrestfulcrud.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.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Locale;
//使用WebMvcConfigurer可以扩展springmvc功能
//@EnableWebMvc
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//设置视图映射规则,把什么请求映射到什么页面
registry.addViewController("/student").setViewName("success");
}
//所有的WebMvcConfigurer组件都会一起起作用
@Bean//将组件注册在容器
public WebMvcConfigurer webMvcConfigurer(){
WebMvcConfigurer configurer = new WebMvcConfigurer() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
}
};
return configurer;
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
2、拦截器进行登录检查
controller层中新增
package com.cnstrong.springboot04webrestfulcrud.component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; 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 { Object user = request.getSession().getAttribute("loginUser"); if(user == null){ //未登录,返回登录页面 request.setAttribute("msg","没有权限,请先登录"); 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 { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
修改LoginController session.setAttribute
package com.cnstrong.springboot04webrestfulcrud.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.thymeleaf.util.StringUtils; import javax.servlet.http.HttpSession; import java.util.Map; @Controller public class LoginController { // @DeleteMapping // @PutMapping // @GetMapping @PostMapping("/user/login") //@RequestMapping(value = "/user/login",method = RequestMethod.POST) public String login(@RequestParam("username") String username, @RequestParam("password") String password, Map<String,Object> map, HttpSession session){ if(!StringUtils.isEmpty(username) && "123456".equals(password)) { //登录成功,防止表单提交,可以重定向到主页 / 代表重定向到当前项目下 session.setAttribute("loginUser",username); return "redirect:/main.html"; }else{ //登录失败 map.put("msg","用户名密码错误"); return "login"; } } } 注册拦截器
package com.cnstrong.springboot04webrestfulcrud.config; import com.cnstrong.springboot04webrestfulcrud.component.LoginHandlerInterceptor; import com.cnstrong.springboot04webrestfulcrud.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.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.Locale; //使用WebMvcConfigurer可以扩展springmvc功能 //@EnableWebMvc @Configuration public class MyMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { //设置视图映射规则,把什么请求映射到什么页面 registry.addViewController("/student").setViewName("success"); } //所有的WebMvcConfigurer组件都会一起起作用 @Bean//将组件注册在容器 public WebMvcConfigurer webMvcConfigurer(){ WebMvcConfigurer configurer = new WebMvcConfigurer() { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("login"); registry.addViewController("/index.html").setViewName("login"); registry.addViewController("/main.html").setViewName("dashboard"); } //注册拦截器,需要排除静态资源等 @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login","/asserts/**","/webjars/**"); } }; return configurer; } @Bean public LocaleResolver localeResolver(){ return new MyLocaleResolver(); } }
<a class="navbar-brand col-sm-3 col-md-2 mr-0" href="https://getbootstrap.com/docs/4.1/examples/dashboard/#">[[${session.loginUser}]]</a>