尝试在SpringSecurity中设置基本认证

基本认证是指一种认证协议

这是一个在网站上实施简易访问限制的身份验证系统之一。
当访问设置了基本身份验证的页面时,您将被要求输入用户名和密码。
当验证成功后,您可以浏览网页内容。
如果验证失败,您将被要求重新输入,或者显示”401 Authorization Required”,无法访问该页面。

另外,访问限制可以在整个网站内实施,也可以针对网站中的某个页面进行限制。

需要注意的是,从安全的角度来看,它是容易受到攻击的。
虽然它可以保护我们在浏览网站(页面)时的入口,但并不能保护通信内容(尽管通过SSL通信除外)。
另一个需要注意的是,如果设置了基本认证,爬虫也无法访问和检测,因此无法显示在搜索引擎的搜索结果中。

虽然如此,我认为有时还是会有机会使用自己制作的投资组合或网站制作项目的发布前网站,所以记住这一点并不会有任何损失。

大致上点个回顾

・基本身份验证是一种简易的认证系统
・成功认证后可以浏览网页内内容
・可以在整个网站或指定页面上进行访问限制
・从安全性角度来看,基本身份验证是脆弱的
・设置基本身份验证后,网络爬虫无法访问且无法被检测到(即使在浏览器中搜索也不会显示)
・可用于自制的作品集或未公开的网站等。

尝试使用Spring Boot和Spring Security的基本身份验证。

环境

Windows10
Eclipse版本:2021-03(4.19.0)

方法

创建项目

需要先创建一个Gradle项目作为前提。
由于需要创建一个在认证后显示的页面,因此暂时按照以下方式创建项目。

image.png
image.png
image.png
image.png

我想这个项目已经创建成功了。

image.png

在 build.gradle 文件中添加 “spring-boot-starter-security”。

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
//以下の一文を追加する
    implementation 'org.springframework.boot:spring-boot-starter-security'
}

在application.yml文件中设置用于基本身份验证时输入的用户名称和密码。

 # SpringSecurity - user情報
spring:
 security:
   user:
     name: testuser
     password: pass001

确保认证界面显示出来。

我在下面的层次结构中创建了一个Java文件(文件名可以是任意的)。
我用文件名”SpringSecurityDemoConfig.java”进行创建。

image.png
package com.demo.security;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.firewall.DefaultHttpFirewall;

@Configuration
@EnableWebSecurity
public class SpringSecurityDemoConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //localhost:8080で表示する際にBasic認証を実施
        http.antMatcher("/").httpBasic()
                .and()
                //全てのURLリクエストは認証されているユーザーしかアクセスできないように設定
                .authorizeRequests().anyRequest().authenticated()
                .and()
                //リクエスト毎に認証を実施
                .sessionManagement().sessionCreationPolicy(
                                 SessionCreationPolicy.STATELESS);
    }

}
}

创建在认证成功后显示的页面

image.png
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>Home</title>
</head>
 <body>
  <h1>認証が成功したよ</h1>
 </body>
</html>

创建一个控制器

image.png
package com.demo.security;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class SecurityController {

    @GetMapping("/")
    public String getHome() {
        return "index";
    }

}

使用BootRun来启动应用程序。

image.png

应该会显示类似图像的窗口。
如果输入不正确,会再次提示输入,并在用户名栏中输入之前在application.yml中设置的“testuser”,在密码栏中输入“pass001”即可登录并跳转到index.html页面。

image.png

已经结束了。

总结

如果您希望将这篇文章作为起点,并进一步加深了解(例如使用PasswordEncoder对密码进行哈希等),请尝试查找其他相关内容。

非常感谢您阅读到这里,那么

广告
将在 10 秒后关闭
bannerAds