SpringBoot安全登录验证
项目结构:
验证实现——WebSecurityConfig.java
package com.dx.config;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
@Configuration
public class WebSecurityConfig extends WebMvcConfigurerAdapter{/*** 登录session key*/public final static String SESSION_KEY = "user";@Beanpublic SecurityInterceptor getSecurityInterceptor() {return new SecurityInterceptor();}public void addInterceptors(InterceptorRegistry registry) {InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor());// 排除配置addInterceptor.excludePathPatterns("/error");addInterceptor.excludePathPatterns("/login**");// 拦截配置addInterceptor.addPathPatterns("/**");}private class SecurityInterceptor extends HandlerInterceptorAdapter {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {HttpSession session = request.getSession();if (session.getAttribute(SESSION_KEY) != null)return true;// 跳转登录String url = "/login";response.sendRedirect(url);return false;}}
}
请求处理——MainController.java
package com.dx.controller;import java.util.HashMap;
import java.util.Map;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.SessionAttribute;import com.dx.config.WebSecurityConfig;@Controller
public class MainController {@GetMapping("/")public String index(@SessionAttribute(WebSecurityConfig.SESSION_KEY) String account,Model model) {model.addAttribute("name", account);return "index";}@GetMapping("/login")public String login() {return "login";}@PostMapping("/loginPost")public String loginPost(String account,String password, HttpSession session) {Map<String, Object> map = new HashMap<>();if (!"123456".equals(password)) {map.put("success", false);map.put("message", "密码错误");return "err";}// 设置sessionsession.setAttribute(WebSecurityConfig.SESSION_KEY, account);map.put("success", true);map.put("message", "登录成功");return "index";}@GetMapping("/logout")public String logout(HttpSession session) {// 移除sessionsession.removeAttribute(WebSecurityConfig.SESSION_KEY);return "redirect:/login";}
}
index.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>玩转spring boot——简单登录认证</title>
</head>
<body><h1>登陆成功!<a href="/logout"><font color="blue">注销</font></a></h1>
</body>
</html>
login.xml
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>玩转spring boot——简单登录认证</title>
</head>
<body><h1>玩转spring boot——简单登录认证</h1><form action="/loginPost" method="post">用户名:<input type="text" name="account"/> <br/>密码:<input type="password" name="password"/> <br /><input type="submit" value="登录" /></form>
</body>
</html>
err.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>玩转spring boot——简单登录认证</title>
</head>
<body><h1>登陆失败!<a href="/logout"><font color="blue">返回</font></a></h1>
</body>
</html>
SpringBoot安全登录验证相关推荐
- SpringBoot+Vue项目中实现登录验证码校验
SpringBoot+Vue项目中实现登录验证码校验 在各大项目中,为保证数据的安全性,通常在登录页面加入验证码校验,以防止爬虫带来的数据泄露危机.本文将介绍在前后端分离的项目中,怎样实现图形验证码校 ...
- Springboot实现短信登录验证
Springboot学习笔记--Java实现短信登录验证功能--Servlet/SSM/SpringBoot都可以用 小白记录一下短信验证登入的实现,方便以后可以拿来直接用. 发短信平台:互亿无线 官 ...
- SpringBoot+jwt+shiro实现登录验证及接口权限校验
SpringBoot+jwt+shiro+token实现对接口权限校验 最近在一个项目上实现登录模块,就想到了权限验证功能,了解了Spring Security和Shiro之后,决定使用Shiro来实 ...
- SpringBoot+MybatisPlus+Vue项目(一):登录验证
文章目录 创建vue-cli项目 创建SpringBoot项目 Vue-cli:登录页面 SpringBoot登录验证 Vue-cli实现登录登录验证 小结 创建vue-cli项目 安装基础工具:No ...
- java SpringBoot登录验证token拦截器
用户访问接口验证,如果用户没有登录,则不让他访问除登录外的任何接口. 实现思路: 1.前端登录,后端创建token(通过JWT这个依赖),返给前端 2.前端访问其他接口,传递token,后端判断tok ...
- 只需2步,教你在Vue中设置登录验证拦截
摘要:两步教你在Vue中设置登录验证拦截! 本文分享自华为云社区<两步教你在Vue中设置登录验证拦截!>,作者: 灰小猿 . 今天在做vue和springboot交互的一个项目的时候,想要 ...
- JAVASE基础模块三十四( 菜鸡版简单登录验证模块系统IO流文件写入)
JAVASE基础模块三十四( 菜鸡版简单登录验证模块系统IO流文件写入) 开发流程:需求文档 接口文档 效果图 开发环境统一:JDK1.8 IDEA win10 GIT SSM SSH SpringB ...
- mysql查询使用qq邮箱注册_Spring Boot实现qq邮箱验证码注册和登录验证功能
1.登录注册思路 这是一个使用spring boot做的一个qq邮箱注册和登录的项目. 没写前端页面,使用postman测试.有截图详细. 1.1.思路 注册:通过输入的邮箱发送验证码,检验前端传来的 ...
- 取消springsecurity默认的登录验证
取消springsecurity默认的登录验证 问题描述 解决方法一 方法二 问题描述 springboot 2.x,访问swagger-ui.html时,会自动跳转到springsecurity的l ...
最新文章
- golang sync.Mutex 互斥锁 使用实例
- python恶搞小程序-有趣的python小程序
- 学习率对神经网络迭代次数和准确率的影响以及近似数学表达式
- Python文件与目录操作
- 【算法】图(一)拓扑排序的实现 图的邻接表算法 判断是否图G中存在环
- python深度学习环境支撑列表 各版本对应关系,Keras,TensorFlow,pytorch ,caffe等。
- java学习(52):抽象类
- 数据库事务(Database Transaction)
- 5 video关掉字幕选项_Photoshop 2020中英文字幕批量生成的制作方法
- 到今天上了一个月班了,功能基本实现了.
- 极客大学架构师训练营 JVM虚拟机原理 JVM垃圾回收原理 Java编程优化 第17课 听课总结
- 谈产品研发项目需求及需求变更管理
- Mac彻底卸载搜狗输入法,看这一篇就够了
- Java 基础知识 【钢镚核恒】
- 计算机移动硬盘无法访问,移动硬盘无法访问参数不正确的解决方法
- 跳转第三方App或网页进行导航
- Linux arm 下载程序,在Linux下使用kermit和dnw给ARM板下载程序
- 算法图解之狄克斯特拉算法实现
- Mining Hero於ETHDenver峰會提交專案,整合IPFS應用場景
- 第十一天 乐在其中-Android与远端之JDBC
热门文章
- 学生计算机教室怎么连接网络,计算机(网络)教室学生使用守则
- Bryntum Gantt 5.2.2 New-Crack
- wps可以登录网页版_wps网页版入口_WPS个人免费版_点点游
- wps在Linux上使用命令打开文档的方法
- IKBC键盘win键失效
- 天蝎项目整机柜服务器技术规范,天蝎整机柜服务器技术规范25.doc
- 安卓设置字符串样式,字符串中部分字体样式
- 三菱fx2n64mr说明书_FX2N-64MR-001手册三菱FX2N-64MR-001使用说明书 - 广州凌控
- 客户端服务器通信demo(续) -- 使用二进制协议 (附源码)
- 电子计算机怎么按不了数字,计算器失灵按不出数字