栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【SpringBoot搭建个人博客】- 后台登录,程序员Java基础案例教程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【SpringBoot搭建个人博客】- 后台登录,程序员Java基础案例教程

欢迎给star以鼓励(^_−)☆

本文将从MVC架构、MD5加密、登录拦截器来讲述个人博客系统的后台登录实现

MVC架构

后台开发采用的是MVC架构,MVC 全名是 Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写, 是一种用于设计创建 Web 应用程序表现层的模式。MVC 中每个部分各司其职:

Model(模型):

  • 通常指的是我们的数据模型,一般情况下用于封装数据

View(视图):

  • 通常指 jsp 或者 html,一般用于展示数据,通常视图是依据模型数据创建的

Controller(控制器):

  • 应用程序中处理用户交互的部分,一般用于处理程序逻辑的
1.用户实体类

之前提到过,由于是个人博客,就没有做权限管理,只是简单的区分了一下管理员(栈主)和普通用户,所以这里需要用户实体类,并需要基本的用户名和密码,管理员登录后可以对后台进行操作,而普通用户则没有权限,在com.star(Group组名)目录下创建entity包,并创建User实体类,实体类如下(这里省去了get、set、toString方法):

package com.star.entity;

import java.util.Date;

public class User {

private Long id;

private String nickname;

private String username;

private String password;

private String email;

private String avatar

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

;

private Integer type;

private Date createTime;

private Date updateTime;

}

2.MD5加密

由于后面要用到MD5加密,对登录密码进行加密,这里就先进行处理一下,在com.star包下创建util工具包,用来放工具类,创建MD5Utils工具类,如下:

package com.star.util;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

public class MD5Utils {

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”));

}

}

分析:

  • 通过该工具类,可以获取密码,在main函数中输入自己密码对应的明码,然后运行,可以在控制台获取对应的密码,这个密码是要存储在数据库中的password字段
  • eg:这里是"111111"字符串,运行main后,获得密码为:“96e79218965eb72c92a549dd5a330112”,则将该字符串存储进数据库中
3.持久层接口

在com.star目录下创建dao包,创建用户持久层接口UserDao,这里主要查询用户名和密码,通过@Param注解将参数传递给SQL,代码如下:

package com.star.dao;

import com.star.entity.User;

import org.apache.ibatis.annotations.Mapper;

import org.apache.ibatis.annotations.Param;

import org.springframework.stereotype.Repository;

@Mapper

@Repository

public interface UserDao {

User findByUsernameAndPassword(@Param(“username”) String username, @Param(“password”) String password);

}

分析:

  • @Mapper注解:让Mybatis找到对应的mapper,在编译的时候动态生成代理类,实现相应SQL功能
  • @Repository注解:用来声明dao层的bean(这个注解可有可无,可以消去依赖注入的报错信息)【@Mapper和@Repository注解可以参考这篇文章:Mybatis 中的 @Repository 与 @Mapper】
  • @Param注解:将参数传递给SQL
  • 返回一个User对象给service调用并核对用户名和密码
4.mapper

Mybatis使用XMLMMapperBuilder类的实例来解析mapper配置文件并执行SQL语句,在resources目录下创建mapper文件夹,再创建UserDao.xml文件,如下:

select * from myblog.t_user

where username = #{username} and password = #{password};

5.用户业务层

在com.star目录下创建service包,创建用户业务层接口UserService,这里主要是检验用户名和密码,传递用户名和密码两个参数,代码如下:

package com.star.service;

import com.star.entity.User;

public interface UserService {

//核对用户名和密码

User checkUser(String username, String password);

}

用户层接口实现类:

在service包下创建Impl包,用来放接口实现类,UserServiceImpl代码如下:

package com.star.service.Impl;

import com.star.dao.UserDao;

import com.star.entity.User;

import com.star.service.UserService;

import com.star.util.MD5Utils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

@Service

public class UserServiceImpl implements UserService {

@Autowired

private UserDao userDao;

@Override

public User checkUser(String username, String password) {

User user = userDao.findByUsernameAndPassword(username, MD5Utils.code(password));

return user;

}

}

分析:

  • 这里主要是获取数据库中的用户名和密码,通过控制器传递过来的密码进行解析匹配,匹配成功则登录
6.登录控制器

在controller控制器包下创建admin包,用来放用户管理的控制器,创建LoginController用户登录控制器,在这里进行登录跳转、登录校验、注销功能,代码如下:

package com.star.controller.admin;

import com.star.entity.User;

import com.star.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

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.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@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 {

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678690.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号