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

网站备案中是什么意思江山市建设局网站

网站备案中是什么意思,江山市建设局网站,js网站模板下载,湖州长兴县建设局网站Spring Security 是一个功能全面的安全框架#xff0c;用于处理基于 Spring 应用程序的身份验证和授权。 它提供了开箱即用的支持#xff0c;采用行业标准的做法和机制来保护你的应用。 无论你是开发简单的 Web 应用还是复杂的微服务架构#xff0c;理解 Spring Security …Spring Security 是一个功能全面的安全框架用于处理基于 Spring 应用程序的身份验证和授权。 它提供了开箱即用的支持采用行业标准的做法和机制来保护你的应用。 无论你是开发简单的 Web 应用还是复杂的微服务架构理解 Spring Security 的核心组件对于实施健壮的安全措施至关重要。 本文将通过示例引导你了解 Spring Security 的核心组件。 Spring Security 核心组件 Spring Security 的架构围绕几个关键组件展开这些组件协同工作以保护你的应用。我们将探讨这些组件并提供使用当前最佳实践的示例。 1. 身份验证 (Authentication) 身份验证是验证用户或系统身份的过程回答“你是谁”的问题。Spring Security 支持多种身份验证机制如表单登录、OAuth2 等。 代码示例 Configuration EnableWebSecurity public class SecurityConfig {Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.authorizeHttpRequests(authorize - authorize.anyRequest().authenticated()).httpBasic(withDefaults()).formLogin(withDefaults());return http.build();}Beanpublic UserDetailsService userDetailsService() {UserDetails userDetails User.withDefaultPasswordEncoder().username(user).password(password).roles(USER).build();return new InMemoryUserDetailsManager(userDetails);} }以上代码是一个典型的 Spring Security 配置示例用于设置基本的身份验证和授权。以下是对代码的详细解释 配置类注解 Configuration标记该类为配置类。EnableWebSecurity启用 Spring Security 的 Web 安全支持。 SecurityFilterChain Bean securityFilterChain 方法定义了安全过滤器链用于配置 HTTP 安全设置。authorizeHttpRequests配置请求的授权规则。 anyRequest().authenticated()要求所有请求都必须经过身份验证。 httpBasic(withDefaults())启用基本认证HTTP Basic。formLogin(withDefaults())启用表单登录。 UserDetailsService Bean userDetailsService 方法定义了用户详细信息服务。UserDetails创建一个用户详细信息对象包含用户名、密码和角色。withDefaultPasswordEncoder使用默认的密码编码器不推荐用于生产环境。InMemoryUserDetailsManager将用户详细信息存储在内存中。 2. 授权 (Authorization) 身份验证后授权确定经过身份验证的用户是否有权限执行特定操作或访问资源回答“你是否可以这样做”的问题。 代码示例 Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http.authorizeRequests(authorize - authorize.antMatchers(/admin/**).hasRole(ADMIN).antMatchers(/user/**).hasAnyRole(USER, ADMIN).antMatchers(/public/**).permitAll().anyRequest().authenticated()).formLogin(withDefaults()).httpBasic(withDefaults());return http.build(); }以上代码用于设置细粒度的请求授权规则。以下是对代码的详细解释 代码解释 配置类注解 Configuration标记该类为配置类。EnableWebSecurity启用 Spring Security 的 Web 安全支持。 SecurityFilterChain Bean filterChain 方法定义了安全过滤器链用于配置 HTTP 安全设置。authorizeRequests配置请求的授权规则。 antMatchers(/admin/**).hasRole(ADMIN)匹配 /admin/** 路径的所有请求要求用户具有 ADMIN 角色。antMatchers(/user/**).hasAnyRole(USER, ADMIN)匹配 /user/** 路径的所有请求要求用户具有 USER 或 ADMIN 角色。antMatchers(/public/**).permitAll()匹配 /public/** 路径的所有请求允许所有用户访问。anyRequest().authenticated()所有其他请求都必须经过身份验证。 formLogin(withDefaults())启用表单登录默认配置。httpBasic(withDefaults())启用基本认证HTTP Basic默认配置。 3. 主体 (Principal) 主体是指当前经过身份验证的用户详细信息在整个应用中可用于执行用户特定的操作。 使用示例 Authentication authentication SecurityContextHolder.getContext().getAuthentication(); String username authentication.getName(); // 使用用户名或其他身份验证对象中的详细信息4. 授予权限 (Granted Authority) 授予权限定义了经过身份验证的用户的权限指定他们可以执行的操作或访问的资源。 示例 Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http.authorizeRequests(authorize - authorize.antMatchers(/api/private/**).hasAuthority(ROLE_USER).anyRequest().permitAll()).httpBasic(withDefaults());return http.build(); }5. 安全上下文和 SecurityContextHolder Spring Security 的核心是 SecurityContext它保存当前经过身份验证的用户详细信息也称为主体。该上下文可以通过 SecurityContextHolder 在整个应用中访问允许你根据用户的认证状态和权限执行操作。 示例 Authentication authentication SecurityContextHolder.getContext().getAuthentication(); if (authentication ! null authentication.isAuthenticated()) {// 根据经过身份验证的用户执行操作 }6. 用户详细信息 (UserDetails) UserDetails 接口是 Spring Security 中的核心部分表示 Spring Security 用于身份验证和授权过程的用户信息。它向框架提供核心用户信息如 用户名用户的唯一标识符。密码用户的密码通常以哈希格式存储。启用状态指示用户是否已启用。禁用的用户无法进行身份验证。账户未过期、凭证未过期、账户未锁定这些布尔标志提供额外的详细信息支持复杂的安全部署需求如账户过期策略和锁定机制。权限表示分配给用户的角色或权限的 GrantedAuthority 对象集合对于授权决策至关重要。 实现 UserDetails 可以让你的应用程序用户实体与 Spring Security 无缝集成。 7. 用户详细信息服务 (UserDetailsService) UserDetailsService 是 Spring Security 中的一个核心接口用于从数据源如数据库、LDAP、内存等中加载用户特定的数据。这个接口的主要目的是提供一种标准化的方式来获取用户信息以便 Spring Security 进行身份验证和授权。 它有一个方法 loadUserByUsername(String username)根据用户名查找用户。返回的 UserDetails 对象随后可供 Spring Security 进一步的身份验证和授权过程使用。 实现你自己的 UserDetailsService 涉及创建一个与用户数据库或其他用户存储机制交互的服务以获取用户详细信息并将其转换为 UserDetails 对象。这个自定义服务成为你的用户数据与 Spring Security 需求之间的桥梁。 8. 认证管理器 (AuthenticationManager) Spring Security 身份验证过程的核心是 AuthenticationManager 接口。它定义了一个方法 authenticate(Authentication authentication)尝试对传递的 Authentication 对象进行身份验证。 AuthenticationManager 负责协调身份验证过程将请求委托给一个或多个 AuthenticationProvider 实例。 每个 AuthenticationProvider 可以处理特定类型的身份验证如用户名和密码、基于令牌的身份验证等。 AuthenticationManager 根据接收到的 Authentication 对象类型将身份验证请求路由到能够处理它的提供者。 成功的身份验证过程会生成一个包含主体和授予权限的完整 Authentication 对象该对象随后存储在 SecurityContext 中以便后续的授权检查。 配置 AuthenticationManager 在配置类中直接暴露一个 AuthenticationManager bean。以下是一个示例 EnableWebSecurity public class SecurityConfig {Beanpublic AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {return authenticationConfiguration.getAuthenticationManager();}Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.authorizeHttpRequests(authorize - authorize.anyRequest().authenticated()).httpBasic(withDefaults()).formLogin(withDefaults());return http.build();}Beanpublic UserDetailsService userDetailsService() {UserDetails userDetails User.withDefaultPasswordEncoder().username(user).password(password).roles(USER).build();return new InMemoryUserDetailsManager(userDetails);} }结论 总之掌握 Spring Security 的核心组件对于希望保护其 Spring 应用程序的开发者至关重要。从使用 UserDetails 和 UserDetailsService 进行身份验证到通过 GrantedAuthority 进行授权再到使用 SecurityContextHolder 管理安全上下文每个元素都在安全生态系统中发挥着关键作用。 从 WebSecurityConfigurerAdapter 到更模块化配置方法的演变体现了 Spring Security 对灵活性和定制化的承诺。通过理解核心组件并采用新的配置模型开发者可以实施针对其应用程序特定需求的健壮安全措施。 注WebSecurityConfigurerAdapter 是 Spring Security 5 之前版本中常用的抽象类用于简化安全配置。然而从 Spring Security 5 开始官方推荐直接使用 SecurityFilterChain Bean 来进行配置而不是继承 WebSecurityConfigurerAdapter。
http://www.hkea.cn/news/14453942/

相关文章:

  • 网站开发计划怎么写网站后台软件可以自己做吗
  • 百度爱采购网站企业门户网站建设论文
  • 上线了建站回龙观手机网站建设服务
  • 唐朝网站手机网站用模版
  • 网站标题有什么作用合肥网络推广有限公司
  • 省级别网站建设方案深圳福田保安公司
  • 从音乐网站下载歌曲做铃音要收费吗做网站可以在哪儿接活
  • 长宁建设机械网站长沙做网站湖南微联讯点不错
  • 厦门网站制作收费做引流网站怎么赚钱赚谁的钱
  • 论文引用网站怎样做脚注wordpress主题2019
  • 用网站做平台房屋装修设计app免费
  • 北京大型网站开发线上做网站赚钱
  • 天津品牌网站建设是什么网站建设qq
  • 网站建设时间计划表马良行网站3d模型预览怎么做的
  • 帝国cms地方门户网站模板十大网站app软件
  • 邯郸做网站最好的公司京东商城网上购物官网
  • 中国建设工程造价管理协会登录网站淘宝网站制作
  • 流量对网站的作用wordpress添加标签插件
  • 定制型网站制作公司德国和俄罗斯和做视频网站
  • 什么企业网站能自己做wordpress修改固定连接403
  • 专业建设网站企业网站单页站群
  • 开发网站现实网络传输失败小程序开发费用多少
  • 苏州做企业网站建设吴忠住房和城乡建设局网站
  • 如何找人帮我做网站推广wordpress 文章在数据库
  • 合肥网站建设王道下拉強做网店运营新手入门教程
  • 宁德网站开发公司上海商场网站开发
  • 建筑工程素材资源网站肇东网页设计
  • 物流网站首页图片企业官网模版制作
  • 电商网站建设行情郴州微游网络科技有限公司
  • WordPress的目录大纲seo有些什么关键词