spring-security
spring security介绍:
https://spring.io/projects/spring-security SpringSecurity 特点:
⚫ 和 Spring 无缝整合。
⚫ 全面的权限控制。
⚫ 专门为 Web 开发而设计。 ◼旧版本不能脱离 Web 环境使用。 ◼新版本对整个框架进行了分层抽取,分成了核心模块和 Web 模块。单独 引入核心模块就可以脱离 Web 环境。
⚫ 重量级。
1 添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
需要添加的配置类:
@Configuration public class SecurityConfigextends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.formLogin() // 表单登录 .and() .authorizeRequests() // 认证配置 .anyRequest() // 任何请求 .authenticated(); // 都需要身份验证 } }
权限管理中的相关概念 2.3.1 主体 英文单词:principal 使用系统的用户或设备或从其他系统远程登录的用户等等。简单说就是谁使用系 统谁就是主体。 2.3.2 认证 英文单词:authentication 权限管理系统确认一个主体的身份,允许主体进入系统。简单说就是“主体”证 明自己是谁。 笼统的认为就是以前所做的登录操作。 2.3.3 授权 英文单词:authorization 将操作系统的“权力”“授予”“主体”,这样主体就具备了操作系统中特定功 能的能力。 所以简单来说,授权就是给用户分配权限。
SpringSecurity 基本原理: SpringSecurity 本质是一个过滤器链: 从启动是可以获取到过滤器链: org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFil ter org.springframework.security.web.context.SecurityContextPersistenceFilter org.springframework.security.web.header.HeaderWriterFilter org.springframework.security.web.csrf.CsrfFilter org.springframework.security.web.authentication.logout.LogoutFilter org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter org.springframework.security.web.savedrequest.RequestCacheAwareFilter org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter org.springframework.security.web.authentication.AnonymousAuthenticationFilter org.springframework.security.web.session.SessionManagementFilter org.springframework.security.web.access.ExceptionTranslationFilter org.springframework.security.web.access.intercept.FilterSecurityInterceptor 代码底层流程:重点看三个过滤器: FilterSecurityInterceptor:是一个方法级的权限过滤器, 基本位于过滤链的最底部。
super.beforeInvocation(fi) 表示查看之前的 filter 是否通过。 fi.getChain().doFilter(fi.getRequest(), fi.getResponse());表示真正的调用后台的服务。 ExceptionTranslationFilter:是个异常过滤器,用来处理在认证授权过程中抛出的异常
UsernamePasswordAuthenticationFilter :对/login 的 POST 请求做拦截,校验表单中用户名,密码。
UserDetailsService 接口讲解
当什么也没有配置的时候,账号和密码是由 Spring Security 定义生成的。而在实际项目中
账号和密码都是从数据库中查询出来的。 所以我们要通过自定义逻辑控制认证逻辑。
如果需要自定义逻辑时,只需要实现 UserDetailsService 接口即可。接口定义如下:
⚫ 返回值 UserDetails
这个类是系统默认的用户“主体”// 表示获取登录用户所有权限 Collection<? extends GrantedAuthority> getAuthorities(); // 表示获取密码 String getPassword(); // 表示获取用户名 String getUsername(); // 表示判断账户是否过期 boolean isAccountNonExpired(); // 表示判断账户是否被锁定 boolean isAccountNonLocked(); // 表示凭证{密码}是否过期 boolean isCredentialsNonExpired(); // 表示当前用户是否可用 boolean isEnabled();
PasswordEncoder 接口讲解
// 表示把参数按照特定的解析规则进行解析 String encode(CharSequence rawPassword); // 表示验证从存储中获取的编码密码与编码后提交的原始密码是否匹配。如果密码匹 配,则返回 true;如果不匹配,则返回 false。第一个参数表示需要被解析的密码。第二个 参数表示存储的密码。 boolean matches(CharSequence rawPassword, String encodedPassword); // 表示如果解析的密码能够再次进行解析且达到更安全的结果则返回 true,否则返回 false。默认返回 false。 default boolean upgradeEncoding(String encodedPassword) { return false; }
BCryptPasswordEncoder 是 Spring Security 官方推荐的密码解析器,平时多使用这个解析
器。
BCryptPasswordEncoder 是对 bcrypt 强散列方法的具体实现。是基于 Hash 算法实现的单
向加密。可以通过 strength 控制加密强度,默认 10.
@Test public void test01(){ // 创建密码解析器 BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); // 对密码进行加密 String atguigu = bCryptPasswordEncoder.encode("atguigu"); // 打印加密之后的数据 System.out.println("加密之后数据:\t"+atguigu); //判断原字符加密后和加密之前是否匹配 boolean result = bCryptPasswordEncoder.matches("atguigu", atguigu); // 打印比较结果 System.out.println("比较结果:\t"+result); }
SpringBoot 对 Security 的自动配置
eg1:需要在properties文件中添加security中的配置
#认证的用户名,比如root spring.security.user.name=root spring.security.user.password=root
方式二:编写类实现接口
@Configuration public class SecurityConfig { // 注入 PasswordEncoder 类到 spring 容器中 @Bean public PasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } } @Service public class LoginService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // 判断用户名是否存在 if (!"admin".equals(username)){ throw new UsernameNotFoundException("用户名不存在!"); } // 从数据库中获取的密码 atguigu 的密文 String pwd = "$2a$10$2R/M6iU3mCZt3ByG7kwYTeeW0w7/UqdeXrb27zkBIizBvAven0/na"; // 第三个参数表示权限 return new User(username,pwd, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,")); } }
方式二:使用UserDetailsService 实现类
@Configuration public class SelfSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(this.getEncode()); } @Bean public PasswordEncoder getEncode() { return new BCryptPasswordEncoder(); } } /** * @author tong.luo * @description SelfUserDetailsService * @date 2021/11/16 11:33 */ @Service public class SelfUserDetailsService implements UserDetailsService { private static HashMap<String, String> staticMap = new HashMap<>(); static { //代替数据库中的数据查询 staticMap.put("daily", "dailypwd"); staticMap.put("admin", "pwd"); staticMap.put("123", "333"); } /** * Locates the user based on the username. In the actual implementation, the search * may possibly be case sensitive, or case insensitive depending on how the * implementation instance is configured. In this case, the <code>UserDetails</code> * object that comes back may have a username that is of a different case than what * was actually requested.. * * @param username the username identifying the user whose data is required. * @return a fully populated user record (never <code>null</code>) * @throws UsernameNotFoundException if the user could not be found or the user has no * GrantedAuthority */ @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { //使用其中的utils生成权限的数据 List<GrantedAuthority> grantedAuthorities = AuthorityUtils.createAuthorityList("roles", "admin", "admins"); String key = "daily"; //可以将下面的固定值修改为从数据库中查询 return new User(key, new BCryptPasswordEncoder().encode(staticMap.get(key)), grantedAuthorities); } }
本文来自博客园,作者:diligently,转载请注明原文链接:https://www.cnblogs.com/luo12828-foxmail/p/16964004.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!