本文章的的主题:
登录的功能的实现和登录功能的拦截信息:
在下面的文件图片中会展示两部分登录页面:你认为那个更安全呢?
第一部分:
利用前端的基础页面操作实现页面的注册然后进行登录
又来到了登录页面我们换个浏览器试一下
浏览器1
浏览器2
浏览器三
思考以下这样的浏览页面信息安全吗?我的答案是不安全的 容易让自己的信息有遗漏:
第二部分:
运行项目
浏览器一
在浏览器二中 http://localhost:8080/index.html 加了地址
同样的地址在这这个浏览器为什么不行
当我输入对的密码后
进入主页
http://localhost:8080/main.html 复制地址重新进入观察下面的图
在没有切换浏览器的情况下
这个图标如何实现
<!--判断是否显示,使用if, ${}可以使用工具类,可以看thymeleaf的中文文档--> <!--/*@thymesVar id="msg" type="java"*/--> <h1 style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></h1>
代码在下面
package com.spring.springboot0907web.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; import javax.servlet.http.HttpSession; @Controller public class LoginController { @RequestMapping("/user/login") // @ResponseBody public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpSession session) { //具体业务 if (!StringUtils.isEmpty(username) && "123456".equals(password)) { session.setAttribute("loginUser", username); // return "index1.html"; return "redirect:/main.html"; } else { //登录失败 model.addAttribute("msg", "用户名或者密码错误错误请重新登录"); return "index"; } } }
拦截器的使用
package com.spring.springboot0907web.config; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LonginHandlerInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //登录成功后要有session Object loginUser = request.getSession().getAttribute("loginUser"); //如果为空不让进 拦截器 if(loginUser==null){ request.setAttribute("msg","请重新登录你没有访问的请求"); request.getRequestDispatcher("/index.html").forward(request, response); return false; }else { return true; } } }
@Configuration public class MyMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); registry.addViewController("/index.html").setViewName("index"); registry.addViewController("/main.html").setViewName("dashboard"); }