Springboot Web应用中基于缺省配置启用Spring Security时的效果总结

本文介绍如何在SpringBoot项目中快速启用SpringSecurity及其缺省配置细节,包括依赖添加、注解启用、缺省安全配置、过滤器、认证方式及URL地址等关键信息。

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

前提: 本文假定你已经拥有一个使用 maven管理基于springbootweb项目。

基于缺省配置启用Spring Security

假定你已经有了一个基于SpringbootWeb应用,想启用Spring Security并使用缺省配置,以下是启用步骤 :

  1. 项目依赖中增加依赖spring-boot-starter-security;
        <!-- Spring Security 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
  1. 添加注解@EnableWebSecurity启用Spring Security;
// 启用 web security  
@EnableWebSecurity  <========
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

都有哪些缺省配置表现

缺省情况使用的web安全配置

WebSecurityConfigurerAdapter是缺省使用的安全配置适配器。以下代码摘自WebSecurityConfigurerAdapter#getHttp方法,HttpSecurity http对象刚刚被创建之后。这段逻辑在缺省情况下被执行并最终生效。

	http
		.csrf().and()
		.addFilter(new WebAsyncManagerIntegrationFilter())
		.exceptionHandling().and()
		.headers().and()
		.sessionManagement().and()
		.securityContext().and()
		.requestCache().and()
		.anonymous().and()
		.servletApi().and()
		.apply(new DefaultLoginPageConfigurer<>()).and()
		.logout();

以下代码摘自WebSecurityConfigurerAdapter#configure方法。 该方法在上面代码之后立即被执行,对HttpSecurity http对象补充更多设置。 这段逻辑在缺省情况下被执行并最终生效。

	http
		.authorizeRequests()
			.anyRequest().authenticated().and()
		.formLogin().and()
		.httpBasic();	

缺省启用的Spring Security Filter

名称
WebAsyncManagerIntegrationFilter
SecurityContextPersistenceFilter
HeaderWriterFilter
CsrfFilter
LogoutFilter
UsernamePasswordAuthenticationFilter
DefaultLoginPageGeneratingFilter
DefaultLogoutPageGeneratingFilter
BasicAuthenticationFilter
RequestCacheAwareFilter
SecurityContextHolderAwareRequestFilter
AnonymousAuthenticationFilter
SessionManagementFilter
ExceptionTranslationFilter
FilterSecurityInterceptor

缺省提供的认证方式

基于内存保持的一个用户账号

  • 用户名称为 user
  • 密码明文会在程序启动时显示在控制台上

例子 :

Using generated security password: 333166dc-91a6-4555-9adb-d632a2cb7e68
  • 相应的UserDetailsService是一个InMemoryUserDetailsManager实例。
  • 相应的AuthenticationManager是一个ProviderManager实例,它自己包含一个AuthenticationProvider:AnonymousAuthenticationProvider不支持用户名/密码表单认证,但它的双亲AuthenticationManager:ProviderManager包含了一个AuthenticationProvider:DaoAuthenticationProvider使用上面的用户名/密码支持用户名/密码认证。
  • 相应的密码加密和比较器使用代理DelegatingPasswordEncoder,最终使用NoOpPasswordEncoder进行明文比较。

密码验证方式

缺省使用的URL地址

URL功能
GET /login登录页面
展示包含一个登录表单的HTML页面,
有两个输入框供用户输入username,password,一个_csrf token隐藏字段,和一个表单提交按钮,
表单提交地址是POST /login
表单提交的密码采用明文传输
POST /login登录请求处理地址
1.登陆自动跳转:/
2.登录失败自动跳转:/login?error
GET /logout退出登录页面
展示包含一个退出登录表单的HTML页面,
有一个退出登录提示消息,一个_csrf token隐藏字段,和一个表单提交按钮
表单提交地址是POST /logout
POST /logout退出登录请求处理地址
1.退出登录成功自动跳转:/login?logout
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值