10.1 登录功能
登录的太好写了,对于我们 已经 写过 很多 这种 请求的代码的人来说。不要太简单了。。
- 首先为 表单 设置上 请求
- 编写 Controller
package top.muquanyu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;
@Controller
public class LoginController {
@RequestMapping("/user/login")
public String login(@RequestParam("username") String username,
@RequestParam("password") String password,
Model model
){
// 写死一个测试的假数据
if(!StringUtils.isEmpty(username) && "123456".equals(password)){
return "dashboard";
}else{
// 告诉 用户,你登录失败了
model.addAttribute("msg","用户名或者密码错误!");
return "index";
}
}
}
我们这个地方 也可以 用 工具类 string.isEmpty()
。但实际上 是不需要的。
- 隐藏请求的资源文件为其他的名字
10.2 登录拦截器
做 Filter 过滤嘛。哈哈。因为 我们现在 开发的 也是依赖于 MVC 的。所以直接 写 一个 拦截器就行。
- 先 设置一个 Session
- 写一个 MVC 的拦截器
package top.muquanyu.config;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginHandlerIntercepter implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//return HandlerInterceptor.super.preHandle(request, response, handler);
HttpSession session = request.getSession();
Object userInfo = session.getAttribute("userInfo");
if(userInfo != null){
return true;
}
request.setAttribute("msg","没有权限,请先登录");
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
- 在 MVC 拓展配置那里,我们 添加 这个拦截器
package top.muquanyu.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
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 MvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//WebMvcConfigurer.super.addInterceptors(registry);
registry.addInterceptor(new LoginHandlerIntercepter()).
addPathPatterns("/**").
excludePathPatterns("/index.html","/","/user/login","/css/**","/js/**","/img/**");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//WebMvcConfigurer.super.addViewControllers(registry);
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard.html");
}
// 自定义 的 国际化 组件 就 生效了
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
核心 代码 就是下面这条 语句:
registry.addInterceptor(拦截器对象).addPathPatterns("拦截的url请求").excludePathPatterns("不拦截的请求A","不拦截的请求B");