Spring Boot項目實踐-員工管理系統(四)·登錄功能實現

本次實戰項目主要是借鑑b站上的視頻資源【狂神說Java】SpringBoot最新教程IDEA版通俗易懂完成的,有需求的話,可以直接去b站觀看完整的視頻教程,本文若有不對之處,望不吝賜教,謝謝~
博文前提:

一、新建登錄控制器

LoginController.java

package com.example.employee_management.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @className: LoginController
 * @description: 登錄控制器
 */
@Controller
public class LoginController {

    /**
     * 登錄
     * @param username
     * @param password
     * @param model
     * @return
     */
    @RequestMapping("/user/login")
    public String login(@RequestParam("username")String username,
                        @RequestParam("password")String password, Model model){

        //用戶名不爲空且密碼正確  注意這裏的數據是默認密碼爲123456,便於調試
        if(!StringUtils.isEmpty(username) && "123456".equals(password)){
            return "redirect:/main.html";
        }else {
            //用戶名或密碼錯誤
            model.addAttribute("msg","用戶名或密碼錯誤");
            return "index";
        }
    }
}

二、修改index.html文件

(1)修改表單的action爲@{/user/login}
(2)添加提示信息
(3)對於用戶名和密碼輸入框添加name屬性

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Signin Template for Bootstrap</title>
    <!-- Bootstrap core CSS -->
    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link th:href="@{/css/signin.css}" rel="stylesheet">
</head>

<body class="text-center">
<!--修改表單的action爲@{/user/login}-->
<form class="form-signin" th:action="@{/user/login}">
    <img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72" src="">
    <!--修改-->
    <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>

    <!--添加提示信息-->
    <!--如果msg信息不爲空,則顯示消息-->
    <p style="color:red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>

    <!--修改-->
    <!--<label class="sr-only" th:text="#{login.username}">Username</label>-->
    <label>
        <!--修改-->
        <input type="text" name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
    </label>
    <!--修改-->
    <!--<label class="sr-only" th:text="#{login.password}">Password</label>-->
    <label>
        <!--修改-->
        <input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="">
    </label>
    <div class="checkbox mb-3">
        <!--修改-->
        <label>
            <input type="checkbox" value="remember-me">[[#{login.rememberMe}]]
        </label>
    </div>
    <!--修改-->
    <button class="btn btn-lg btn-primary btn-block" type="submit">[[#{login.signInBtn}]]</button>
    <p class="mt-5 mb-3 text-muted">© 2017-2018</p>
    <!--修改-->
    <a class="btn btn-sm" th:href="@{/index.html(language='zh_CN')}">中文</a>
    <a class="btn btn-sm" th:href="@{/index.html(language='en_US')}">English</a>
</form>

</body>

</html>

三、添加用戶主頁

在MyMvcConfig類中添加用戶主頁頁面控制器

package com.example.employee_management.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @className: MyMvcConfig
 * @description: MVC 控制器  藉助註解完成控制器而不用手動編寫
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    /**
     * MVC 添加首頁控制器
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //這裏 “/”和“/index.html”效果一樣,因爲web項目默認頁是index.html
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");

        //添加用戶主頁
        registry.addViewController("/main.html").setViewName("dashboard");
    }


    /**
     * 將國際化組件放入ioc容器中
     *
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }
}

啓動項目。輸入正確的用戶名和密碼後,會自動跳轉到dashboard.html頁面;當輸入錯誤時會有提示信息
在這裏插入圖片描述

四、總結

實現登錄功能需要注意的地方有:

  • 建立一個登錄控制器類,且要在MVC配置類中作相應配置
  • 利用form表單提交信息
  • 目前實現的登錄功能不完善,後續需要加上攔截器使其更安全

在這裏插入圖片描述
2020.04.11

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章