**使用form 表单做 登陆: **
注意:
1.使用th:action="@{XXX}" 请求路径
<form class="form-signin" th:action="@{/user/login}" methed = "post">
2.input里,一定要写name 参数, name="username",参数要和后端接收的参数名对应
<input type="text" name="username" class="form-control" placeholder="Username" required="" autofocus="">
3. 判断 th:if="${ not #strings.isEmpty(msg)}"
<!--判断 ,当msg不为空时,生成p标签-->
<p style="color:red;" th:text="${msg}" th:if="${ not #strings.isEmpty(msg)}"></p>
<form class="form-signin" th:action="@{/user/login}" methed = "post">
<img class="mb-4" src="asserts/img/bootstrap-solid.svg" th: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>
<!--判断 ,当msg不为空时,生成p标签-->
<p style="color:red;" th:text="${msg}" th:if="${ not #strings.isEmpty(msg)}"></p>
<label class="sr-only" th:text="#{login.password}">Username</label>
<input type="text" name="username" class="form-control" placeholder="Username" required="" autofocus="">
<label class="sr-only" th:text="#{login.password}">Password</label>
<input type="password" name="password" class="form-control" placeholder="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="@{/login.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/login.html(l='en_US')}">English</a>
</form>
Controller
**使用restful风格接收请求 GetMapping **
@Controller
public class LoginController
{
// @GetMapping()
// @DeleteMapping()
// @RequestMapping(value = "/user/login",method = RequestMethod.POST)
@GetMapping(value = "/user/login")
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("username",username);
//登陆成功:防止表单重复提交,使用重定向
return "redirect:/main.html";
}else{
//登陆失败
map.put("msg","用户名密码错误");
return "login";
}
}
}
定义拦截器
package com.xuexi.springboot.component;
import org.springframework.lang.Nullable;
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 {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object user = request.getSession().getAttribute("username");
if(user == null){
//未登录.返回登录页面
request.setAttribute("msg","没有权限,请先登陆");
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}else{
//已登录.放行请求
return true;
}
}
}
配置类里注册拦截器
package com.xuexi.springboot.MyMVCConfig;
import com.xuexi.springboot.component.LoginHandlerInterceptor;
import com.xuexi.springboot.component.MyLocalResolver;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
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 MyMvcConfig extends WebMvcAutoConfiguration{
@Bean
public LocaleResolver localeResolver(){
return new MyLocalResolver();
}
// 所有的WebMvcConfigurer 组件一起起作用
@Bean //将组件注册到容器中
public WebMvcConfigurer webMvcConfigurer(){
WebMvcConfigurer adapter = new WebMvcConfigurer(){
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//addViewController 相当于requestMapping setViewName 是路径名
registry.addViewController("/").setViewName("login");
registry.addViewController("/login.html").setViewName("login");
registry.addViewController("main.html").setViewName("dashboard");
}
/*注册拦截器*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
//静态资源 *.js *.css
//SpringBoot 已经 做好了静态资源映射,我们不需要处理
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/login.html","/","/user/login");
}
};
return adapter;
}
}