springboot-項目實戰:登錄實現


承接:springboot-項目實戰:國際化

1 修改MyMvcConfig.java

在MyMvcConfig.java的addViewControllers方法中增加一條視圖控制,也就是增加了下面這行代碼,目的是讓 /main.html 的請求跳轉到 dashhoard.html 頁面

registry.addViewController("/main.html").setViewName("dashboard");

修改后的MyMvcConfig.java如下

MyMvcConfig.java

package com.lv.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;

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/main.html").setViewName("dashboard");
    }
    //自定義的國際化組件就生效了
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }
}

2 在controller包下創建一個登錄控制類

LoginController.java

package com.lv.controller;

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

@Controller
public class LoginController {
    @RequestMapping("/user/login")
    public String login(@RequestParam("userName")String userName, @RequestParam("password") String password,Model model){
        //具體的業務
        if(!StringUtils.isEmpty(userName) && "123456".equals(password)){
            return "redirect:/main.html";
        }else {
            //告訴用戶,你登錄失敗了
            model.addAttribute("msg","用戶名或密碼錯誤");
            return "index";
        }
    }
}

只是模擬了驗證,並沒有查詢后台數據,只要用戶名不為空並且密碼是123456就登錄成功,否則就失敗

3 修改index.html

一共修改了三個部分,第一個是為form表單設置了提交地址

<form class="form-signin" th:action="@{/user/login}">

第二是為輸入用戶名和密碼的input增加了name屬性

<input type="text" name="userName" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
<input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="">

第三個是增加了一個登錄失敗的回顯

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

修改后的index.html 如下

index.html

<!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>
      <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
      <link th:href="@{/css/signin.css}" rel="stylesheet">
   </head>
   <body class="text-center">
      <form class="form-signin" th:action="@{/user/login}">
         <img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72">
         <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">Username</label>
         <input type="text" name="userName" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
         <label class="sr-only">Password</label>
         <input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="">
         <div class="checkbox mb-3">
            <label>
          <input type="checkbox" value="remember-me">[[#{login.remember}]]
        </label>
         </div>
         <button class="btn btn-lg btn-primary btn-block" type="submit">[[#{login.btn}]]</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>

4 重啟程序,測試登錄

輸入錯誤的信息,賬號為1,密碼為1

再輸入正確的信息,賬號為1,密碼為123456

成功實現了登錄驗證的效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM