侧边栏壁纸
博主头像
Devlive 开源社区博主等级

行动起来,活在当下

  • 累计撰写 117 篇文章
  • 累计创建 32 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Spring Boot 整合 Security 权限控制-2:添加登录/首页页面

我是管理员哦
2024-02-01 / 0 评论 / 0 点赞 / 6 阅读 / 5267 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2024-02-01,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

在 pom 文件中增加thymeleaf页面支持

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

创建 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>

在 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 <br/>
 * 描述 : HomeController <br/>
 * 作者 : qianmoQ <br/>
 * 版本 : 1.0 <br/>
 * 创建时间 : 2018-03-20 下午2:24 <br/>
 * 联系作者 : <a href="mailTo:shichengoooo@163.com">qianmoQ</a>
 */
@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 <br/>
 * 描述 : UserController <br/>
 * 作者 : qianmoQ <br/>
 * 版本 : 1.0 <br/>
 * 创建时间 : 2018-03-20 下午2:24 <br/>
 * 联系作者 : <a href="mailTo:shichengoooo@163.com">qianmoQ</a>
 */
@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";
    }
 
}
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区