文章目錄
- CAS單點登錄系統構建(初次訪問的1-5步)
- 1. 使用 thymeleaf 做模板實作
- 2. 搭建控制器 controller
- 3. 前端mvc系統代碼
- 4. 測驗是否可以正常跳轉
CAS單點登錄系統構建(初次訪問的1-5步)

1. 使用 thymeleaf 做模板實作
引入 thymeleaf 依賴

在yml檔案中配置 thymeleaf

創建統一登錄頁面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>SSO單點登錄</title>
</head>
<body>
<h1>歡迎訪問單點登錄系統</h1>
<form action="doLogin" method="post">
<input type="text" name="username" placeholder="請輸入用戶名" />
<input type="password" name="password" placeholder="請輸入密碼" />
<input type="hidden" name="returnUrl" th:value="${returnUrl}" />
<input type="submit" value="提交登錄" />
</form>
<span style="color: red" th:text="${errmsg}"></span>
</body>
</html>
2. 搭建控制器 controller
package com.beyond.controller;
import com.beyond.utils.RedisOperator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Controller
public class SSOController {
@GetMapping("/hello") // 因為上面用的是Controller注解,
@ResponseBody // 所有默認的GetMapping會認為是頁面,我們應該在加上 @ResponseBody
public Object hello(){
return "Hello World~";
}
@GetMapping("/login")
public Object login(String returnUrl,
Model model,
HttpServletRequest request,
HttpServletResponse response){
model.addAttribute("returnUrl", returnUrl);
// TODO 后續完善是否登錄
// 用戶從未登錄過, 第一次進入則跳轉到 CAS的統一登錄界面
return "login";
}
}

3. 前端mvc系統代碼

4. 測驗是否可以正常跳轉

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/281219.html
標籤:其他
上一篇:DDD實踐反思
下一篇:2021-04-28
