使用Spring Security构建的Web应用程序基础① 〜登陆功能的实现〜

首先

文件结构.
├── .gradle
├── .idea
├── build
├── gradle
└── src
├── main
| ├── java
| | └── com
| | └── example
| | └── practice
| | ├── config
| | | └── SecurityConfig.java
| | ├── web
| | | ├── order
| | | | ├── OrderForm.java
| | | | └── OrderController.java
| | | └── IndexController.java
| | └── domain
| | └── order
| | ├── OrderEntity.java
| | ├── OrderService.java
| | └── OrderRepository.java
| └── resources
| ├── static
| ├── templates
| | ├── order
| | | ├── delete_confirmation.html
| | | ├── detail.html
| | | ├── form.html
| | | └── list.html
| | └── index.html
| ├── schema.sql
| ├── data.sql
| └── application.properties
└── test
.gitignore
build.gradle
gradlew.bat
HELP.md
settings.gradle

因为我们这次事先在GitHub上发布了代码,所以请您先下载代码,然后再开始。

安全性的重要性

用户隐私的保护

数据泄露风险

防止数据篡改和破坏

 

登录功能

登录功能的主要目的是什么?

确保安全性
登录功能对于系统和应用程序的安全至关重要,它可以确保只有合法用户能够访问,从而保护数据和机密性。

管理个体访问权限
通过为每个用户设置不同的访问权限,可以正确地控制信息。
可以为管理员、普通用户、游客等具有不同权限级别的用户提供适当的权限。

通过引入登录功能,用户可以访问自己的账户并保存个人设置和数据,这将改善用户体验。

一般的的登录流程

展示登录表单。
用户访问登录页面,会显示用于输入用户名和密码的登录表单。

2. 执行认证
当用户输入用户名和密码时,系统将对其进行验证,并对用户进行认证,以确定其是否为合法用户。

3. 认可确认
系统在认证成功后会确认用户的访问权限,并检查其是否能够访问适当的资源和功能。

4. 开始会话
如果认证和授权成功,系统将为用户开始会话。会话在一定时间内保持用户处于活跃状态,以提升用户体验。

5. 重定向或许可
在认证和授权成功后,系统将重定向到适当的页面或授予必要的访问权限以允许访问。

我希望通过这些步骤来实现登录功能。

实现登录功能。

让我们将下面的依赖关系添加到build.gradle中。

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.projectlombok:lombok'
	implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0'
	runtimeOnly 'com.h2database:h2'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	implementation 'org.springframework.boot:spring-boot-starter-validation'
	implementation 'org.springframework.boot:spring-boot-starter-security'
	implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
	testImplementation 'org.springframework.security:spring-security-test'
}

在添加依存关系并使用调试功能执行后,将显示以下日志的密码。

Using generated security password: #####################

当你在「http://localhost:8080」上登录后,会显示如下界面。请用”User”作为用户名,输入在日志中输出的密码。

スクリーンショット 2023-08-23 7.34.54.png

那么,将会跳转到”业务管理应用”的首页。

スクリーンショット 2023-08-23 7.37.50.png

这些一系列的步骤将成为Spring Security中默认配置的页面。

定制登录功能的实施

首先,我們需要在Spring Security中設定未登錄用戶訪問需要登錄的頁面時,會被重定向到登錄頁面。我們需要指定登錄頁面的路徑為/login,同時需要注意/login是不需要驗證的,其他頁面需要進行驗證。通過設定Spring Security的登錄頁面路徑,實現了頁面的重定向。

首先,让我们按照如下代码的方式实现“/login”页面的视图。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>トップページ | 業務用アプリケーション</title>
  <!-- Bootstrap CSS link -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-4">
  <h1 class="mb-3"><a href="../index.html" th:href="@{/}" style="color: inherit; text-decoration: none;">業務管理アプリケーション</a></h1>
  <p>これはログインページです。</p>
</div>
</body>
</html>

设置”/login”控制器。

package com.example.practice.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class indexController {
    @GetMapping
    public String index() {
        return "index";
    }

    @GetMapping("/login")
    public String LoginForm() {
        return "login";
    }
}

SecurityConfig是一个Java类,用于定制Spring Security并定义应用程序的安全相关设置,因此要实施它。

package com.example.practice.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


//WebSecurity関連の設定であるということを示すアノテーション
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .mvcMatchers("/login/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login").permitAll();
    }
}

.mvcMatchers(“/login/**”).permitAll():允许任何用户访问以/login/开头的URL,这样可以解除对登录页面的访问限制。

只要求().经过验证():对于所有其他请求,用户必须经过身份验证(登录)。
换句话说,未登录的用户无法访问其他页面。

.formLogin():设置基于表单的登录。

.loginPage(“/login”).permitAll() : 将登录页面的URL设置为 /login,并允许任何用户访问。这样一来,访问登录页面不受限制,任何人都可以访问登录页面。

换句话说,这个设定要求在访问除了登录页面以外的所有页面时,用户必须经过认证,而登录页面则允许未登录的用户访问。

结束了

广告
将在 10 秒后关闭
bannerAds