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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/andy_zhang2007/article/details/84774774

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

基于缺省配置启用Spring Security

假定你已经有了一个基于Springboot 的Web应用,想启用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#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

猜你喜欢

转载自blog.csdn.net/andy_zhang2007/article/details/84774774
今日推荐