springSecurity基本原理

本文通过启动项目展示了Spring Security的基本认证流程,用户需输入默认的user和密码进行认证。此外,还提到了使用basic认证的形式。文章核心围绕Spring Security的过滤器链工作机制,解释了绿色部分的配置可自定义,而其他部分则是固定位置的过滤器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

啥也不说先贴代码:

spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/imooc-demo?useUnicode=yes&characterEncoding=UTF-8
spring.datasource.username = root
spring.datasource.password = 123456

spring.session.store-type = none

#这里关闭security的配置要注释掉
#security.basic.enabled = false

server.port = 8060
package com.imooc.security.browser;

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

@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter{

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		
		http.formLogin()//认证
		.and()
		.authorizeRequests()//授权
		.anyRequest()
		.authenticated();
	}
    
}

启动项目:会看到一个默认密码:

然后访问一个请求会让你输入认证,默认  user和控制台默认密码 看上图,然后认证成功返回响应。

当然也可以使用最经典的basic形式进行认证:

@Override
	protected void configure(HttpSecurity http) throws Exception {
		
//		http.formLogin()//认证
		http.httpBasic()
		.and()
		.authorizeRequests()//授权
		.anyRequest()
		.authenticated();
	}

好了请看原理图:

图中绿色部分是可以通过配置生效的,其他颜色过滤器不能设置的 它们一定在指定的位置上。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值