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

金华官方网站建设百度指数网址

金华官方网站建设,百度指数网址,如何备份wordpress,饰品企业网站建设Spring Security 是一个功能强大且高度可定制的身份验证和访问控制框架,其认证和授权实现机制如下:一、认证(Authentication)实现 1. 核心组件 AuthenticationManager:认证入口点,委托给AuthenticationProviderAuthenticationProv…


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/595152.html

相关文章:

  • 长沙网站推广和优化网站开发语言用什么好
  • 专业网站建设集团crm客户管理系统源码
  • 电商网站有哪些类型flash 如何做游戏下载网站
  • 网格系统网站微信小程序平台入口
  • 乐清网站推广做美食视频的网站
  • 东莞网络营销价格咨询怎么对一个网站做优化
  • 北仑建网站价格宿迁网站建设公司
  • 做网站与做网页的区别网站标准宽度
  • 阳泉网站建设哪家便宜学做沪江网站要多久
  • 常用的网站建设程序有那些网站开发软件d
  • 打工人日报#20251110
  • 做手机网站多少钱思科网站建设配置站点dns服务
  • 双语网站建设网站网站建设公司每年可以做多少个网站
  • 网站后台模板 jquery现在的网站开发都用什么开发
  • 果洛州wap网站建设公司手机上如何设置wordpress
  • 深圳市坪山新区建设局网站企业网站seo推广技巧
  • 自助网站建设开发wordpress安装需要什么
  • 辽阳公司做网站郑州网站开发比较好的网络公司
  • 昆凌做的广告买化妆品网站山东闪电建站网
  • 无锡建设网站的公司简介直播网站的建设
  • 北海哪里做网站建设android应用开发基础
  • 网站主页面最开始在哪里做高端网站首页
  • 网站建设与网页设计美食怎样在手机安装wordpress
  • html网页设计环保网站重庆网站开发哪家好
  • 找建站公司电脑怎么制作图片
  • 上门做网站公司哪家好服装公司电子商务网站建设策划书
  • 网站制作费用申请沧州市做网站
  • 做网站有哪些公司好wordpress 与 dede
  • 如何创建微信小程序商店seo网站推广软件
  • OpenAI 悄然上线「网校」OpenAI Academy