首頁 > 軟體

SpringBoot整合Security許可權控制登入首頁

2022-11-13 14:01:17

在 pom 檔案中增加thymeleaf頁面支援

<!-- 引入頁面模板 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

application.yml 組態檔

建立 resources 目錄資料夾目錄為: src/main/resources 並將其設定為 resource 資源目錄, 在resources目錄下建立 application.yml 組態檔

spring:
  thymeleaf:
    cache: false
    check-template: true
    check-template-location: true
    content-type: text/html
    enabled: true
    encoding: UTF-8
    mode: HTML5
    prefix: classpath:/templates/
    suffix: .html

在resources目錄下建立 templates 檔案目錄, 並在該目錄下建立 index.html 和 login.html 頁面檔案

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>SpringBoot Security Integration</title>
</head>
<body>
</body>
</html>

login 頁面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>登入頁面</title>
</head>
<body>
<form action="" method="post">
    <table>
        <tr>
            <td>使用者名稱:</td>
            <td><input name="username" id="username" value=""/></td>
        </tr>
        <tr>
            <td>密碼:</td>
            <td><input name="password" id="password" value=""/></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="submit" /></td>
        </tr>
    </table>
</form>
</body>
</html>

controller目錄下跳轉設定

在 java 原始碼目錄下建立controller目錄, 並在該目錄下建立 HomeController/UserController 進行頁面跳轉設定

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edurt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
 * HomeController 
 * 描述 : HomeController 
 * 作者 : qianmoQ 
 * 版本 : 1.0 
 * 建立時間 : 2018-03-20 下午2:24 
 */
@Controller
public class HomeController {
    /**
     * 首頁
     *
     * @return 首頁頁面跳轉
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    String home() {
        return "index";
    }
}

UserController

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edurt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
 * UserController 
 * 描述 : UserController 
 * 作者 : qianmoQ 
 * 版本 : 1.0 
 * 建立時間 : 2018-03-20 下午2:24 
 */
@Controller
@RequestMapping(value = "user")
public class UserController {
    /**
     * 使用者登入
     *
     * @return 使用者登入頁面跳轉
     */
    @RequestMapping(value = "login", method = RequestMethod.GET)
    String login() {
        return "login";
    }
    /**
     * 使用者登出退出
     *
     * @return 使用者登出退出頁面跳轉
     */
    @RequestMapping(value = "logout", method = RequestMethod.GET)
    String logout() {
        return "login";
    }
}

以上就是SpringBoot整合Security許可權控制登入首頁的詳細內容,更多關於SpringBoot整合Security登入的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com