登录数据库(Springboot采用BCrypt密码哈希验证的极简登录(不需要数据库))

登录数据库(Springboot采用BCrypt密码哈希验证的极简登录(不需要数据库))
Springboot采用BCrypt密码哈希验证的极简登录(不需要数据库)

一、生成BCrypt 密码哈希

package com.example.demo.util;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;public class GeneratePasswordHash {    public static void main(String[] args) {        String password = "123456"; // ← 改成你想要的密码明文        String hash = new BCryptPasswordEncoder().encode(password);        System.out.println("密码: " + password);        System.out.println("BCrypt 哈希: " + hash);    }}

二、登录页面

登录数据库(Springboot采用BCrypt密码哈希验证的极简登录(不需要数据库))



            登录        

系统登录

                        用户名                                        密码                            登录    

三、Controller

1.com.example.demo.controller.LoginController登录控制器

package com.example.demo.controller;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpSession;import java.util.Map;@Controllerpublicclass LoginController {    privatestaticfinal String VALID_USERNAME = "admin";    privatestaticfinal String VALID_PASSWORD_HASH = "$2a$10$kPDU2/FfyzGUNRFFQQ2LlO2M7F6jad3wM/kC1xWu0el9Xh3wUOnRy";    // 处理表单提交    @PostMapping("/login")    public String login(@RequestParam String username,@RequestParam String password,HttpSession session,Map model) {        if (!VALID_USERNAME.equals(username)) {            model.put("error", "用户名或密码错误");            return"login"; // 返回 login        }        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();        if (!encoder.matches(password, VALID_PASSWORD_HASH)) {            model.put("error", "用户名或密码错误");            return"login";        }        // 登录成功,保存会话        session.setAttribute("username", username);        // ✅重定向到成功页面        return"redirect:/jgbf";    }    @GetMapping("/logout")    public String logout(HttpSession session) {        session.invalidate(); //立即销毁当前用户的 HTTP 会话(Session)        return"redirect:/login"; // 或首页    }    // 跳转登录页    @GetMapping("/login")    public String loginPage() {        return"login";     }}

2.com.example.demo.controller.JgbfController成功页面控制器

package com.example.demo.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpSession;@Controllerpublicclass JgbfController {    @RequestMapping("/jgbf")    public String jgbfbatup(HttpSession session) {        // 检查是否已登录        String username = (String) session.getAttribute("username");        if (username == null) {            // 未登录 → 重定向到登录页            return"redirect:/login";        }        // 已登录 → 跳转成功页面        return"jgbfbatup";    }}

四、成功页面添加“退出登录”按钮

     退出登录



💻 配套实训环境与动手练习

本文涉及的相关技术指令、开发环境与工具链已内置在边学边练在线实验室中,无需繁琐安装配置,随时在浏览器中实践体验:

文章版权声明:除非注明,否则均为边学边练网络文章,版权归原作者所有