Mr丶冷文

文章 分类 评论
133 13 8619

站点介绍

冷文学习者(KEVINLU98.COM),记录一个北漂小码农的日常业余生活
友链申请地址(直接评论即可): 传送门

(二十三)登录功能及打包

MR丶冷文 2022-10-16 2699 1条评论 个人博客项目视频教程 javaspringboot个人博客

首页 / 正文
Freewind主题v1.5版本已发布,下载请移步Freewind 1.5,同时还有主题伴生插件Freewind Markdown,下载请移步 Freewind Markdown,有问题请在留言板,交换友链请直接在友链留言,我创建了一个主题交流群,有兴趣可以加下: 点此加入
报毒我说明一下,是因为我把主题的版权信息做了加密,其中用了eval,杀毒软件认为eval函数是一个危险的操作,这点介意的话请勿下载,我也没有强迫任何人去下载,也没有向大家收取一分钱的主题费用,所以也犯不着因为这些事情来喷我,喜欢就用,不喜欢就不用,就这么简单

发布于2022-10-28

(二十三)登录功能及打包

说明

上节课原计划是把登录也说了的,由于时间不够了,就把登录拿到这节课说,然后把之前遗留的小问题处理一下,最后再说说打包的功能

异步修改

我们之前做的异步实际是没有生效的,因为我们需要spring帮我们把这个类代理出来才行,我们在同一个类中就没有任作用,因为没有经过spring的代理

我们将两个发邮件的方法复制到MailHelper中或将@Async的注解加到sendMail方法就可以啦

在Application上加上@EnableAsync注解

登录功能

我们的后台是不希望被除了我们之外的其他人访问到的,所以我们需要对后台所有的请求进行登录拦截,对于在没有登录的前提下所有操作都跳转到登录页

  • 我们先定义登录页面的路由并修改页面表单
  • 我们再写登录接口,如果用户名与密码都与我们配置的用户名密码相等我们就认为登录成功将登录成功的状态存在Session中,反之则返回登录页面且给其报用户名或密码错误的信息,密码加密我们可以直接用Spring的 DigestUtils.md5DigestAsHex方法

@PostMapping("/login")
public String login(String username, String password, Model model, HttpSession session) {
  String pass = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
  if (StringUtils.equals(username, webSite.getUsername()) && StringUtils.equals(pass, webSite.getPassword())) {
    session.setAttribute(WebSite.LOGIN_SIGN, true);
    return "redirect:/admin/";
  } else {
    model.addAttribute("errorMsg", "用户名密码错误");
    return "admin/login";
  }
}

@GetMapping("/logout")
public String logout(HttpSession session) {
  session.removeAttribute(WebSite.LOGIN_SIGN);
  return "admin/login";
}

@GetMapping("/login.html")
public String login() {
  return "admin/login";
}
  • 在页面回显错误信息
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link rel="stylesheet" href="/static/plugin/font-awesome/css/font-awesome.min.css">
    <!-- 自定义css文件 -->
    <link rel="stylesheet" href="/static/admin/css/login.css">
    <style>
      .lw-error-msg {
        color: coral;
        text-align: center;
      }
    </style>
  </head>
  <body>
    <div class="lw-login-page">
      <div class="lw-login-box">
        <div class="lw-left">
          <h1>欢迎登录</h1>
          <p>冷文学习者-管理后台</p>
        </div>
        <div class="lw-right">
          <div class="lw-error-msg" th:text="${errorMsg}" th:if="${errorMsg != null}"></div>
          <form th:action="@{/admin/login}" method="post">
            <div class="lw-input">
              <input name="username" type="text" placeholder="用户名">
            </div>
            <div class="lw-input">
              <input name="password" readonly onfocus="this.removeAttribute('readonly')" type="password" placeholder="密码">
            </div>
            <div class="lw-input">
              <button type="submit">登录后台</button>
            </div>
          </form>
          <p>Copyright © www.kevinlu98.cn All Rights Reserved.
            <br>
            冷文学习者版权所有</p>
        </div>
      </div>
    </div>
  </body>
</html>
  • 定义一个拦截器实现HandlerInterceptor接口并覆写preHandle的方法,当Session中没有登录信息时我们直接转发到登录页面并给出请登录后再进行操作的提示
package cn.kevinlu98.intercepto;

import cn.kevinlu98.common.WebSite;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * Author: Mr丶冷文
 * Date: 2022/10/16 11:07
 * Email: kevinlu98@qq.com
 * Description:
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    HttpSession session = request.getSession();
    Boolean sign = (Boolean) session.getAttribute(WebSite.LOGIN_SIGN);
    if (Objects.nonNull(sign) && sign) {
      // 已登录
      return true;
    } else {
      request.setAttribute("errorMsg", "请登录后进行操作");
      request.getRequestDispatcher("/admin/login.html").forward(request, response);
      return false;
    }
  }
}
  • 定义一个退出登录的路由,操作为清空Session中的登录信息
  • 在WebConfig中的addInterceptors方法中启用拦截器,用addPathPatterns配置拦截的路由,用excludePathPatterns配置排除被拦截路由中的那些路由
@Override
public void addInterceptors(InterceptorRegistry registry) {
  registry.addInterceptor(loginInterceptor)
    .addPathPatterns("/admin/**")
    .excludePathPatterns("/admin/login")
    .excludePathPatterns("/admin/login.html")
    .excludePathPatterns("/admin/logout");
}

打包和运行

因为我们站点很多信息都存在于application.yml的配置文件中,我们打完包之后application.yml都会打进jar包中,如果我们想更改一下站点信息,我们还需要去改application.yml并重新打包,这样的话就难免有些不太友好,我们这里可以在启动时手动指定加载外部的yml文件

  • 我们可以打完包手动将项目的application.yml复制到外部
  • 我们可以利用打包插件自动将application.yml文件复制到外部
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <version>3.2.0</version>
  <executions>
    <execution>
      <id>copy-resources</id>
      <phase>package</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <includes>
              <include>*.properties</include>
              <include>*.yml</include>
              <include>*/*.properties</include>
              <include>*/*.yml</include>
            </includes>
          </resource>
        </resources>
        <outputDirectory>${project.build.directory}</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>
  • 启动时使用--spring.config.local=xxx.yml来启动我们的项目

评论(1)

  1. 向星辰 游客 2022-10-24 08:19 回复

    很好

热门文章

最新评论

  • snailwalk

    下载看看

  • https://bonusstats.com/en/bonus-7signs

    I drop a comment whenever I like a article on a site or I have something to valuable to contribute to the discussion. Usually it is a result of the fire displayed in the article I looked at. And on this article Freewind主题音乐播放器(老王重制版) - 冷文学习者. I was actually excited enough to post a comment :-) I actually do have a few questions for you if it's okay. Could it be just me or do some of these responses appear like left by brain dead people? :-P And, if you are posting at other places, I would like to keep up with everything new you have to post. Could you list the complete urls of your communal sites like your linkedin profile, Facebook page or twitter feed? Feel free to surf to my blog post :: https://bonusstats.com/en/bonus-7signs

  • https://Bonusstats.com/en/bonus-7signs

    Thanks for finally talking about >Freewind 1.5 更新 - 冷文学习者

  • 老王

    下载看看

  • 爱你

    我要回复一下课件

日历

2026年12月

日一二三四五六
  12345
6789101112
13141516171819
20212223242526
2728293031  

文章目录

站点公告
Freewind主题v1.5版本已发布,下载请移步Freewind 1.5,同时还有主题伴生插件Freewind Markdown,下载请移步 Freewind Markdown,有问题请在留言板,交换友链请直接在友链留言,我创建了一个主题交流群,有兴趣可以加下: 点此加入
报毒我说明一下,是因为我把主题的版权信息做了加密,其中用了eval,杀毒软件认为eval函数是一个危险的操作,这点介意的话请勿下载,我也没有强迫任何人去下载,也没有向大家收取一分钱的主题费用,所以也犯不着因为这些事情来喷我,喜欢就用,不喜欢就不用,就这么简单
点击小铃铛关闭