参考文章:https://spring.io/guides/gs/securing-web/

导入maven

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

配置spring security

package com.example.springboottest.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain; @Configuration
@EnableWebSecurity
public class BaseConfiguration { /**
* 用户信息服务(配置用户账号、密码、角色)
* @param passwordEncoder 密码加密器
* @return 在内存用户详细信息管理器中
*/
@Bean
public InMemoryUserDetailsManager userDetailsService(PasswordEncoder passwordEncoder) {
UserDetails user = User.withUsername("user")
.password(passwordEncoder.encode("123456"))
.roles("user")
.build(); UserDetails admin = User.withUsername("admin")
.password(passwordEncoder.encode("123456"))
.roles("user", "admin")
.build(); return new InMemoryUserDetailsManager(user, admin);
} /**
* 过滤链
* @param http http安全实例
* @return 安全过滤链实例
* @throws Exception
*/
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(resp -> {
resp.requestMatchers("/", "/index").permitAll();
resp.requestMatchers("/hello").hasRole("admin");
})
.formLogin(form -> {
form.loginPage("/login").defaultSuccessUrl("/index").permitAll();
})
.logout(logout -> {
logout.permitAll();
}); return http.build();
} /**
* 密码加密器
* @return 密码加密器的实例
*/
@Bean
public PasswordEncoder passwordEncoder() {
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
return encoder;
}
}

login页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
<head>
<title>Spring Security Example </title>
</head>
<body>
<form th:action="@{/login}" method="post">
<div><label> User Name : <input type="text" name="username"/> </label></div>
<div><label> Password: <input type="password" name="password"/> </label></div>
<div><input type="submit" value="Sign In"/></div>
</form>
</body>
</html>

spring boot 3.x 配置spring security的更多相关文章

  1. spring boot rest 接口集成 spring security(2) - JWT配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  2. spring boot rest 接口集成 spring security(1) - 最简配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  3. spring boot 2.0.3+spring cloud (Finchley)9、 安全组件Spring Boot Security

    官方文档 一.Spring Security介绍 Spring Security是Spring Resource社区的一个安全组件,Spring Security为JavaEE企业级开发提供了全面的安 ...

  4. Spring boot application.properties 配置

    原文链接: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.ht ...

  5. spring boot web相关配置

    spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何web相关的配置便能提供web服务,这还得归于spri ...

  6. 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置

    在上篇博客初识Spring Boot框架中我们初步见识了SpringBoot的方便之处,很多小伙伴可能也会好奇这个Spring Boot是怎么实现自动配置的,那么今天我就带小伙伴我们自己来实现一个简单 ...

  7. Spring Boot 2.0 配置图文教程

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 ...

  8. Spring boot 的自动配置

    Xml 配置文件 日志 Spring Boot对各种日志框架都做了支持,我们可以通过配置来修改默认的日志的配置: #设置日志级别 logging.level.org.springframework=D ...

  9. spring boot多数据源配置(mysql,redis,mongodb)实战

    使用Spring Boot Starter提升效率 虽然不同的starter实现起来各有差异,但是他们基本上都会使用到两个相同的内容:ConfigurationProperties和AutoConfi ...

  10. Spring Boot系列——日志配置

    日志,通常不会在需求阶段作为一个功能单独提出来,也不会在产品方案中看到它的细节.但是,这丝毫不影响它在任何一个系统中的重要的地位. 为了保证服务的高可用,发现问题一定要即使,解决问题一定要迅速,所以生 ...

随机推荐

  1. 基于 Traefik 的激进 TLS 安全配置实践

    前言 Traefik是一个现代的HTTP反向代理和负载均衡器,使部署微服务变得容易. Traefik可以与现有的多种基础设施组件(Docker.Swarm模式.Kubernetes.Marathon. ...

  2. Netty-BIO、NIO、AIO、零拷贝-2

    Java BIO 编程 一.I/O 模型 1.I/O 模型简单的理解:就是用什么样的通道进行数据的发送和接收,很大程度上决定了程序通信的性能 2.Java 共支持 3 种网络编程模型/IO 模式:BI ...

  3. 网络监测工具之Zabbix的搭建与测试方法(三) ---Zabbix Agent

    安装客户端 在官方网站下载最新版本zabbix agent v6.2.6,然后默认安装,其中配置服务端的界面如下图: 其他一律默认即可. 启用发现功能 如上图所示,开启发现规则,默认搜索zabbix ...

  4. [论文总结] 农业工程领域中App和Web相关应用论文笔记

    文章目录 1. Tomato leaf segmentation algorithms for mobile phone applications using deep learning 2. Int ...

  5. 巧用Fiddler开启运营商定制版路由器被阉割的功能,免去刷公版固件的风险

    前言: 三大运营商都有自己的定制版路由器,一般会在自家营销活动中作为赠品送给用户 正巧我家里就有两台电信定制版的华为路由器,都是这两年双十一在某宝上买宽带时送的 两台路由器型号分别是TC7001和TC ...

  6. Nodejs后端自动化测试

    偶然看到收藏一下 const puppeteer = require('puppeteer'); const fs = require('fs'); (async () => { const b ...

  7. 商城网站商品sku选择的js简易实现

    商城网站商品sku选择的js简易实现 <!DOCTYPE HTML> <html lang="en-US"> <head> <meta c ...

  8. 鸿蒙系统应用开发之基于API6的蓝牙开发

    写在前面 由题意得,我今天讲的是基于鸿蒙系统的兼容JS的类Web开发范式的软件应用开发之蓝牙开发,它是基于API6的,至于为什么是基于API6,请你花几分钟看一下我之前写的这个系列教程的第四篇&quo ...

  9. Java 进阶P-7.2+P-7.3

    控制反转 很长一段时间里,我对控制反转和依赖注入这两个概念很模糊,闭上眼睛想一想,总有一种眩晕的感觉.但为了成为一名优秀的 Java 工程师,我花了一周的时间,彻底把它们搞清楚了. 紧耦合 在我们编码 ...

  10. FAQ 关于pip你应该知道的一些技巧

    pip简介 pip是安装了python之后的一个应用程序,包管理程序,有点类似于yum.npm.apt等工具 物理位置一般是python.exe所在目录下的scripts下 以我为例,我Python安 ...