文章目录

  • 一、后台登录
  • (一)构建登录页面 - login.html
  • (二)后台管理页面 - index.htm
  • (三)创建用户接口UserService
  • (四)创建用户接口实现UserServiceImpl
  • (五)创建UserRepository接口
  • (六)创建登录控制器LoginController
  • (七)MD5加密:创建工具类MD5Utils
  • (八)将数据库密码加密
  • (九)创建登录拦截器BlogController
  • (十)创建登录过滤器LoginInterceptor
  • (十一)创建配置类WebConfig
  • (十二)运行查看效果


一、后台登录

1、构建登录页面和后台管理首页

2、UserService和UserRepository

3、LoginController实现登录

4、MD5加密

5、登录拦截器

(一)构建登录页面 - login.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head th:replace="admin/_fragments :: head(~{::title})">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客管理登录</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
    <link rel="stylesheet" href="../../static/css/me.css">
</head>
<body>

<br>
<br>
<br>
    <div class="m-container-small m-padded-tb-massive" style="max-width: 30em !important;">
        <div class="ui container">
            <div class="ui middle aligned center aligned grid">
                <div class="column">
                    <h2 class="ui teal image header">
                        <div class="content">
                            管理后台登录
                        </div>
                    </h2>
                    <form class="ui large form" method="post" action="#" th:action="@{/admin/login}">
                        <div class="ui segment">
                            <div class="field">
                                <div class="ui left icon input">
                                    <i class="user icon"></i>
                                    <input type="text" name="username" placeholder="用户名">
                                </div>
                            </div>
                            <div class="field">
                                <div class="ui left icon input">
                                    <i class="lock icon"></i>
                                    <input type="password" name="password" placeholder="密码">
                                </div>
                            </div>
                            <button class="ui fluid large teal submit button">登   录</button>
                        </div>

                        <div class="ui error mini message"></div>
                        <div class="ui mini negative message" th:unless="${#strings.isEmpty(message)}" th:text="${message}">用户名和密码错误</div>

                    </form>

                </div>
            </div>
        </div>
    </div>

<!--/*/<th:block th:replace="_fragments :: script">/*/--><!--加/*/静态页面注释有效,动态页面注释无效-->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>
<!--/*/</th:block>/*/-->

<script>
    $('.ui.form').form({
        fields : {
           username: {
                identifier : 'username',
                rules : [{
                    type :'empty',
                    prompt : '请输入用户名'
                }]
            },
            password: {
                identifier: 'password',
                rules: [{
                    type: 'empty',
                    prompt: '请输入密码'
                }]
            }
        }
    });
</script>
</body>
</html>

(二)后台管理页面 - index.htm

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head th:replace="admin/_fragments :: head(~{::title})">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客管理</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
    <link rel="stylesheet" href="../../static/css/me.css">
</head>
<body>
    <!--导航-->
    <nav th:replace="admin/_fragments ::menu(0)" class="ui inverted attached segment m-padded-tb-mini m-shadow-small">
        <div class="ui container">
            <div class="ui inverted secondary stackable menu">
                <h2 class="ui teal header item">管理后台</h2>
                <a href="#" class="active m-item item m-mobile-hide"><i class="home icon"></i>博客</a>
                <a href="#" class="m-item item m-mobile-hide"><i class="idea icon"></i>分类</a>
                <a href="#" class="m-item item m-mobile-hide"><i class="tags icon"></i>标签</a>
                <div class="right m-item m-mobile-hide menu">
                    <div class="ui dropdown item">
                        <div class="text">
                            <img class="ui avatar image" src="https://unsplash.it/100/100?image=1005">
                            袁晓晓
                        </div>
                        <i class="dropdown icon"></i>
                        <div class="menu">
                            <a href="#" th:href="@{/admin/logout}" class="item">注销</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <a href="#" class="ui menu toggle black icon button m-right-top m-mobile-show">
            <i class="sidebar icon"></i>
        </a>
    </nav>

    <!--中间内容-->
    <div class="m-container-small m-padded-tb-big">
        <div class="ui container">
            <div class="ui success large message">
                <h3>Hi,</h3>
                <p>袁晓晓,欢迎登陆!</p>
            </div>

            <img src="https://unsplash.it/1000/400?image=1005" alt="" class="ui rounded bordered fluid image">
        </div>
    </div>

    <br>
    <br>
    <!--底部footer-->
    <footer th:replace="admin/_fragments :: footer" class="ui inverted vertical segment m-padded-tb-massive">
        <div class="ui center aligned container">
            <div class="ui inverted divided stackable grid">
                <div class="three wide column">
                    <div class="ui inverted link list">
                        <div class="item">
                            <img src="../../static/images/wechat.jpg" class="ui rounded image" alt="" style="width: 110px">
                        </div>
                    </div>
                </div>
                <div class="three wide column">
                    <h4 class="ui inverted header m-text-thin m-text-spaced">最新博客</h4>
                    <div class="ui inverted link list">
                        <a href="#" class="item">用户故事(User Story)</a>
                        <a href="#" class="item">用户故事(User Story)</a>
                        <a href="#" class="item">用户故事(User Story)</a>
                    </div>
                </div>
                <div class="three wide column">
                    <h4 class="ui inverted header m-text-thin m-text-spaced">联系我</h4>
                    <div class="ui inverted link list">
                        <a href="#" class="item">Email: listen@169.com</a>
                        <a href="#" class="item">QQ: 2293432330</a>
                    </div>
                </div>
                <div class="seven wide column">
                    <h4 class="ui inverted header m-text-thin m-text-spaced">Lirenmi</h4>
                    <p class="m-text-thin m-text-spaced m-opacity-mini">这是我的个人博客,会分享关于编程、写作、思考相关的任何内容,希望可以给来到这儿的人有所帮助...</p>
                </div>
            </div>
            <div class="ui inverted section divider"></div>
            <p class="m-text-thin m-text-spaced m-opacity-tiny">Copyright@2019-2022Lirenml Designed by Lirenmi</p>
        </div>

    </footer>

<!--/*/<th:block th:replace="_fragments :: script">/*/--><!--加/*/静态页面注释有效,动态页面注释无效-->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>
<!--/*/</th:block>/*/-->

    <script>
        $('.menu.toggle').click(function () {
            $('.m-item').toggleClass('m-mobile-hide');
        });

        $('.ui.dropdown').dropdown({
            on : 'hover'
        });
    </script>
</body>
</html>

(三)创建用户接口UserService

apifox模拟springsecurity登录 springboot 模拟登录_html

(四)创建用户接口实现UserServiceImpl

apifox模拟springsecurity登录 springboot 模拟登录_html_02

(五)创建UserRepository接口

apifox模拟springsecurity登录 springboot 模拟登录_ico_03

(六)创建登录控制器LoginController

src/main/java/net.lbj/web/admin/LoginController

package net.lbj.web.admin;

import net.lbj.po.User;
import net.lbj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * 20210623
 */
@Controller
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private UserService userService;

    @GetMapping
    public String loginPage() {
        return "admin/login";
    }

    @PostMapping("/login")
    public String login(@RequestParam String username,
                        @RequestParam String password,
                        HttpSession session,
                        RedirectAttributes attributes) {
        User user = userService.checkUser(username, password);
        if (user != null) {
            user.setPassword(null);
            session.setAttribute("user", user);
            return "admin/index";
        } else {
            attributes.addFlashAttribute("message","用户名和密码错误");
            return "redirect:/admin";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/admin";
    }

}

(七)MD5加密:创建工具类MD5Utils

输入:不定长度信息(要加密的信息)

package net.lbj.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 20210623
 */
public class MD5Utils {

    /**
     * MD5加密类
     * @param str 要加密的字符串
     * @return 加密后的字符串
     */
    public static String code(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte[]byteDigest = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < byteDigest.length; offset++) {
                i = byteDigest[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            //32位加密
            return buf.toString();
            //16位加密
            //return buf.toString().substring(8, 24);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        System.out.println(code("111111"));
    }
}

运行效果:

111111加密:96e79218965eb72c92a549dd5a330112

apifox模拟springsecurity登录 springboot 模拟登录_ide_04

(八)将数据库密码加密

apifox模拟springsecurity登录 springboot 模拟登录_html_05

(九)创建登录拦截器BlogController

apifox模拟springsecurity登录 springboot 模拟登录_html_06

(十)创建登录过滤器LoginInterceptor

apifox模拟springsecurity登录 springboot 模拟登录_ico_07

(十一)创建配置类WebConfig

apifox模拟springsecurity登录 springboot 模拟登录_ico_08

(十二)运行查看效果

apifox模拟springsecurity登录 springboot 模拟登录_html_09


apifox模拟springsecurity登录 springboot 模拟登录_ide_10