SpringBoot - 简单集成 SpringSecurity


活动地址:CSDN21天学习挑战赛

概述

SpringSecurity - 初识 SpringSecurity 这篇文章中,我们初步认识了 SpringSecurity,并且遗留了几个问题,我们知道 SpringBoot 的最大的特性就是 约定大于配置SpringBoot 底层为我们做了大量的封装,集成的每一个 Starter 框架都提供了默认配置,并且提供了可配置项,接下来我们就利用 SpringBoot 为我们提供的可配置项来自定义我们的 SpringSecurity

自定义用户

SpringSecurity - 初识 SpringSecurity 这篇文章中,我们知道 SpringSecurity 默认会给我们生成一个用户:user,这里我们可以在 SpringBoot 的配置类中自定义默认用户:

spring:
  security:
    user:
      name: admin
      password: 123456
      roles:
        - user
        - sales

以上配置就会代替默认的用户 user用户名密码角色 都可以定义

自定义配置类

SpringSecurity 官方文档 给出了 SpringBoot 集成 SpringSecurity最简配置

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

	// @formatter:off
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
				.authorizeRequests(authorize -> authorize
					.antMatchers("/css/**", "/index").permitAll()
					.antMatchers("/user/**").hasRole("USER")
				)
				.formLogin(formLogin -> formLogin
					.loginPage("/login")
					.failureUrl("/login-error")
				);
	}
	// @formatter:on

	@Bean
	@Override
	public UserDetailsService userDetailsService() {
		UserDetails userDetails = User.withDefaultPasswordEncoder()
				.username("user")
				.password("password")
				.roles("USER")
				.build();
		return new InMemoryUserDetailsManager(userDetails);
	}
}

以上配置我们暂时不需要理解是什么意思,也不需要理解为什么要这么配置,因为我们还不具备自定义配置的能力,比如上面配置中出现的 @EnableWebSecurity 注解、WebSecurityConfigurerAdapterHttpSecurityUserDetailsServiceUserDetails 等概念我们还不理解,所以我们至少要先弄懂这里出现的几个概念,在之后的文章中我们一点点来阅读 SpringSecurity 的源码,从而达到自定义配置,实现我们个性化需求的目的。

总结

以上我们利用了 SpringBoot 为我们提供的配置项自定义了 SpringSecurity,但是这些都只是 SpringSecurity 的简单应用,SpringSecurity 能做的远远不止于此,比如我们常见的 对接现有用户数据库实现认证授权,因为我们不可能每次都遇到新项目,有可能我们接手的是以前的老项目,需要使用 Security 来改造升级,所以我们要对 Security 有一个全面的了解,才能使用起来得心应手。

接下来我们就在 SpringSecurity 学习 专栏中进一步探讨 SpringSecurity

下篇文章: SpringSecurity - 启动流程分析(一)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: SpringBoot 3.0可以使用Spring Security来实现安全认证和授权控制,可以通过在Spring Boot项目的Maven依赖中添加spring-boot-starter-security依赖来实现。 ### 回答2: 要集成Spring SecuritySpring Boot 3.0中,可以按照以下步骤操作: 1. 在pom.xml文件中添加Spring Security的依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 创建一个配置类,例如SecurityConfig,该类需要继承自WebSecurityConfigurerAdapter,并使用@EnableWebSecurity注解启用Spring Security配置。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/public/**").permitAll() // 设置公开访问的URL .anyRequest().authenticated() // 其他URL需要认证 .and() .formLogin() // 使用表单登录 .and() .logout().permitAll(); // 允许注销 } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() // 使用内存中的用户进行身份认证 .withUser("admin").password("{noop}password").roles("ADMIN") .and() .withUser("user").password("{noop}password").roles("USER"); } } ``` 3. 在configure(HttpSecurity http)方法中配置URL的访问权限,设置公开访问的URL和需要认证的URL。这里演示了两个简单的示例,所有以/public/开头的URL都允许公开访问,其他URL需要认证。 4. 在configure(AuthenticationManagerBuilder auth)方法中配置用户的身份认证信息。这里使用了内存中的用户,用户名为admin和user,密码为password,分别具有ADMIN和USER角色。 5. 可以根据自己的需求进行进一步配置,例如添加自定义的登录页面、自定义登录成功/失败的处理逻辑等。 通过以上步骤,就可以将Spring Security集成Spring Boot 3.0中。需要注意的是,这只是一个简单的示例,实际应用中可能需要根据实际情况进行调整和配置。 ### 回答3: 要将Spring Boot 3.0集成Spring Security,需要执行以下步骤: 1. 首先,在项目的pom.xml文件中添加Spring Security的依赖项。可以在Spring Boot官方文档中找到最新版本的依赖项坐标。 2. 创建一个继承自WebSecurityConfigurerAdapter的配置类,并使用@EnableWebSecurity注解标记该类。这个配置类将用于定义安全规则。 3. 在配置类中,覆盖configure(HttpSecurity http)方法来配置安全策略。这个方法允许你定义哪些URL路径需要被保护,哪些路径可以被公开访问,以及如何进行登录认证等。 4. 如果需要自定义登录页面,可以创建一个继承自WebMvcConfigurer的配置类,并覆盖addViewControllers方法来注册自定义登录页面的URL路径。 5. 如果需要自定义认证逻辑,可以创建一个实现UserDetailsService接口的自定义用户服务类,并将其注册为Spring组件。在configure方法中使用userDetailsService()方法来指定这个自定义用户服务类。 6. 可以使用@EnableGlobalMethodSecurity注解来启用全局方法级别的安全性,并设置相关的注解。 7. 最后,可以使用SecurityContextHolder来实现在应用程序中获得当前用户的信息。 通过以上步骤,就可以将Spring Boot 3.0和Spring Security集成起来。这样就可以保护应用程序的URL路径,实现用户认证和授权的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值