当前位置: 首页 > news >正文

Spring Security 认证与授权实现机制


Spring Security 是一个功能强大且高度可定制的身份验证和访问控制框架,其认证和授权实现机制如下:

一、认证(Authentication)实现
1. 核心组件
AuthenticationManager:认证入口点,委托给AuthenticationProvider

AuthenticationProvider:实际执行认证逻辑

UserDetailsService:加载用户特定数据

SecurityContextHolder:存储当前安全上下文

2. 认证流程
用户提交凭证(用户名/密码等)

UsernamePasswordAuthenticationFilter拦截请求,创建Authentication对象

ProviderManager选择合适的AuthenticationProvider

DaoAuthenticationProvider调用UserDetailsService加载用户详情

PasswordEncoder验证密码

认证成功后构建完全填充的Authentication对象

将Authentication存入SecurityContext

3. 常用认证方式

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 内存认证
auth.inMemoryAuthentication()
.withUser("user").password(passwordEncoder().encode("password")).roles("USER");

// JDBC认证
auth.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("select username,password,enabled from users where username=?")
.authoritiesByUsernameQuery("select username,authority from authorities where username=?");

// 自定义UserDetailsService
auth.userDetailsService(customUserDetailsService)
.passwordEncoder(passwordEncoder());
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
二、授权(Authorization)实现
1. 核心概念
GrantedAuthority:授予用户的权限(如角色)

ConfigAttribute:访问资源所需的权限

AccessDecisionManager:做出最终的访问控制决策

AccessDecisionVoter:投票决定是否授予访问权限

2. 授权流程
FilterSecurityInterceptor拦截请求

获取请求资源的配置属性(所需权限)

AccessDecisionManager收集投票

基于投票结果允许/拒绝访问

3. 授权配置方式

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN") // 基于角色
.antMatchers("/api/**").hasAuthority("API_ACCESS") // 基于权限
.antMatchers("/public/**").permitAll() // 公开访问
.anyRequest().authenticated() // 其他请求需要认证
.and()
.formLogin() // 表单登录
.and()
.httpBasic(); // HTTP基本认证
}
三、高级特性
1. 方法级安全


@PreAuthorize("hasRole('ADMIN') or #user.username == authentication.name")
public void updateUser(User user) {
// ...
}

@PostAuthorize("returnObject.owner == authentication.name")
public Document getDocument(Long id) {
// ...
}

@Secured("ROLE_ADMIN")
public void adminOperation() {
// ...
}
2. 自定义认证逻辑

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Autowired
private CustomUserDetailsService userDetailsService;

@Autowired
private PasswordEncoder passwordEncoder;

    @Override
public Authentication authenticate(Authentication authentication) 
throws AuthenticationException {

String username = authentication.getName();
String password = authentication.getCredentials().toString();

CustomUser user = userDetailsService.loadUserByUsername(username);

if (passwordEncoder.matches(password, user.getPassword())) {
return new UsernamePasswordAuthenticationToken(
user, password, user.getAuthorities());
} else {
throw new BadCredentialsException("Authentication failed");
}
}

    @Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
3. OAuth2集成


@Configuration
@EnableAuthorizationServer
public class OAuth2Config extends AuthorizationServerConfigurerAdapter {

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("client")
.secret(passwordEncoder.encode("secret"))
.authorizedGrantTypes("authorization_code", "refresh_token")
.scopes("read", "write")
.redirectUris("http://localhost:8080/login/oauth2/code/custom");
}
}
四、实际应用建议
密码安全:始终使用强密码编码器(如BCrypt)

CSRF防护:生产环境必须启用

会话管理:合理配置会话固定保护和并发控制

CORS配置:根据实际需求精细控制

安全头部:启用XSS保护、HSTS等安全头部

http://www.dtcms.com/a/315281.html

相关文章:

  • 随机森林知识点整理:从原理到实战
  • 课题学习4——将原系统的BERT换为SBERT
  • 【网络运维】Linux:RAID存储技术
  • 单类别目标检测中的 Varifocal Loss 与 mAP 评估:从原理到实践(特别前景和背景类区分)
  • Transformer核心机制:QKV全面解析
  • 图片处理工具类:基于 Thumbnailator 的便捷解决方案
  • Unsloth 大语言模型微调工具介绍
  • 数据结构:反转链表(reverse the linked list)
  • 机器视觉的产品包装帖纸模切应用
  • 深度学习-卷积神经网络CNN-卷积层
  • JMeter的基本使用教程
  • 嵌入式学习之51单片机——串口(UART)
  • STM32F103C8-定时器入门(9)
  • slwl2.0
  • Azure DevOps — Kubernetes 上的自托管代理 — 第 5 部分
  • 05-Chapter02-Example02
  • 微软WSUS替代方案
  • Redis与本地缓存的协同使用及多级缓存策略
  • 【定位设置】Mac指定经纬度定位
  • Spring--04--2--AOP自定义注解,数据过滤处理
  • Easysearch 集成阿里云与 Ollama Embedding API,构建端到端的语义搜索系统
  • Shell第二次作业——循环部分
  • 【科研绘图系列】R语言绘制解释度条形图的热图
  • 中标喜讯 | 安畅检测再下一城!斩获重庆供水调度测试项目
  • 松鼠 AI 25 Java 开发 一面
  • 【慕伏白】Android Studio 配置国内镜像源
  • Vue3核心语法进阶(Hook)
  • selenium4+python—实现基本自动化测试
  • PostgreSQL——数据类型和运算符
  • MySQL三大日志详解(binlog、undo log、redo log)