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

任县建设局网站百度app大全

任县建设局网站,百度app大全,美丽说网站模板,wordpress推广浏览插件前言 「作者主页」:雪碧有白泡泡 「个人网站」:雪碧的个人网站 「推荐专栏」: ★java一站式服务 ★ ★前端炫酷代码分享 ★ ★ uniapp-从构建到提升★ ★ 从0到英雄,vue成神之路★ ★ 解决算法,一个专栏就够了★ ★ 架…

前言

在这里插入图片描述
「作者主页」:雪碧有白泡泡
「个人网站」:雪碧的个人网站
「推荐专栏」

java一站式服务
前端炫酷代码分享
uniapp-从构建到提升
从0到英雄,vue成神之路
解决算法,一个专栏就够了
架构咱们从0说
★ 数据流通的精妙之道★

★后端进阶之路★

请添加图片描述

在这里插入图片描述

文章目录

  • 前言
  • 引言
  • 1. 定义用户
    • 使用内存方式定义用户
    • 使用数据库方式定义用户
  • 2. 定义角色
    • 创建角色并将其与用户关联
    • 解释如何使用角色来组织和控制权限
  • 3. 定义权限
  • 4. 访问规则
      • 使用Ant风格的路径匹配规则
      • 使用表达式语言进行更复杂的访问规则定义
  • 小结

引言

继上篇后端进阶之路——深入理解Spring Security配置(二)
在这里插入图片描述

1. 定义用户

使用内存方式定义用户

在内存中定义用户是一种简单的方法,适用于开发和测试环境。我们可以在Spring Security的配置类中使用InMemoryUserDetailsManager来定义用户。下面是一个示例:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser("user").password("{noop}password") // 使用 "{noop}" 前缀表示密码不加密.roles("USER");}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated().and().formLogin();}
}

上述示例中,使用withUser方法定义了一个用户名为"user",密码为"password",角色为"USER"的用户。注意,密码的前缀"{noop}"表示密码不加密,这只适用于开发和测试环境。

使用数据库方式定义用户

在实际生产环境中,通常会将用户信息存储在数据库中。使用数据库方式定义用户需要进行以下步骤:

  1. 创建数据库表格来存储用户信息,例如表格名为"users",包含列"username"、“password"和"role”。
  2. 配置Spring Security以连接到数据库,并使用数据库中的用户信息进行认证和授权。

下面是一个使用数据库方式定义用户的示例:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate DataSource dataSource;@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username=?").authoritiesByUsernameQuery("SELECT username, role FROM users WHERE username=?");}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated().and().formLogin();}
}

上述示例中,通过dataSource注入了数据源,然后使用jdbcAuthentication配置了基于数据库的认证和授权。.usersByUsernameQuery()方法指定了查询用户名、密码和启用状态的SQL语句,.authoritiesByUsernameQuery()方法指定了查询用户名和角色的SQL语句。

2. 定义角色

在Spring Security中,可以使用角色来组织和控制权限。角色是一组权限的集合,可以通过将角色与用户关联来授予用户相应的权限。

以下是在Spring Security中定义角色并将其与用户关联的示例代码:

创建角色并将其与用户关联

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate DataSource dataSource;@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username=?").authoritiesByUsernameQuery("SELECT username, role FROM users WHERE username=?");}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN") // 需要"ADMIN"角色才能访问"/admin/**"路径.anyRequest().authenticated().and().formLogin();}
}

在上述示例中,.antMatchers("/admin/**").hasRole("ADMIN")指定了只有拥有"ADMIN"角色的用户才能访问"/admin/**"路径。这样,我们可以根据不同的角色来限制用户对某些资源的访问权限。

解释如何使用角色来组织和控制权限

在Spring Security中,可以使用角色来定义访问控制规则,并使用这些规则来保护应用程序的不同部分。通过为用户分配不同的角色,可以实现对不同用户的权限控制。

以下是一个使用角色进行权限控制的示例代码:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate DataSource dataSource;@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username=?").authoritiesByUsernameQuery("SELECT username, role FROM users WHERE username=?");}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN") // 需要"ADMIN"角色才能访问"/admin/**"路径.antMatchers("/user/**").hasRole("USER") // 需要"USER"角色才能访问"/user/**"路径.anyRequest().authenticated().and().formLogin();}
}

上述示例中,使用.antMatchers("/admin/**").hasRole("ADMIN").antMatchers("/user/**").hasRole("USER")定义了不同路径需要不同角色的访问权限。只有拥有相应角色的用户才能访问对应的路径。

3. 定义权限

权限是指在系统中对特定资源或操作进行访问控制的能力。它是用于确保只有经过授权的用户或角色才能执行某些敏感操作或访问某些受限资源的机制。权限的定义和管理对于确保系统的安全性和保护重要数据非常重要。

在Spring Security中,我们可以使用@PreAuthorize@PostAuthorize@Secured等注解来定义权限。这些注解可以放置在方法上,用于限制只有具有特定权限的用户才能调用该方法。

下面是一个示例,演示了如何在Spring Security中定义权限:

首先,确保你已经添加了Spring Security的依赖到你的项目中。

<!-- pom.xml -->
<dependencies>...<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>...
</dependencies>

接下来,创建一个自定义的权限验证类。

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;@Component
public class MyAuthorization {@PreAuthorize("hasRole('ROLE_ADMIN')")public void adminOnly() {// 只有具有ROLE_ADMIN角色的用户才能调用此方法}@PreAuthorize("hasAuthority('WRITE_PERMISSION')")public void writeAccess() {// 只有具有WRITE_PERMISSION权限的用户才能调用此方法}}

在上述代码中,MyAuthorization类包含了两个方法:adminOnly()writeAccess()。这两个方法都使用了@PreAuthorize注解来定义权限。

hasRole('ROLE_ADMIN')表示只有具有ROLE_ADMIN角色的用户才能调用adminOnly()方法。

hasAuthority('WRITE_PERMISSION')表示只有具有WRITE_PERMISSION权限的用户才能调用writeAccess()方法。

然后,在你的业务逻辑中,可以通过依赖注入的方式使用MyAuthorization类,并调用相应的方法。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class MyService {private final MyAuthorization myAuthorization;@Autowiredpublic MyService(MyAuthorization myAuthorization) {this.myAuthorization = myAuthorization;}public void doSomething() {myAuthorization.adminOnly(); // 调用需要admin权限的方法myAuthorization.writeAccess(); // 调用需要writeAccess权限的方法}}

在上述代码中,MyService类依赖注入了MyAuthorization类,并在doSomething()方法中调用了其中的两个方法。

4. 访问规则

当使用Spring Security进行访问控制时,可以通过Ant风格的路径匹配规则和表达式语言来定义更复杂的访问规则。下面是一些示例代码片段,用于说明如何使用这些规则:

使用Ant风格的路径匹配规则

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN").antMatchers("/user/**").hasAnyRole("USER", "ADMIN").anyRequest().authenticated().and().formLogin().and().httpBasic();}
}

在上述代码中,我们使用authorizeRequests()方法来配置路径的访问规则。.antMatchers()方法用于指定要匹配的路径模式,并使用hasRole()hasAnyRole()方法指定需要具有的角色。在这个例子中,如果请求的路径以"/admin/"开头,则用户需要具有"ADMIN"角色。

使用表达式语言进行更复杂的访问规则定义

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().expressionHandler(expressionHandler()).antMatchers("/admin/**").access("hasRole('ADMIN') or hasIpAddress('127.0.0.1')").anyRequest().authenticated().and().formLogin().and().httpBasic();}@Beanpublic DefaultWebSecurityExpressionHandler expressionHandler() {DefaultWebSecurityExpressionHandler expressionHandler = new DefaultWebSecurityExpressionHandler();expressionHandler.setRoleHierarchy(roleHierarchy());return expressionHandler;}@Beanpublic RoleHierarchy roleHierarchy() {RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER");return roleHierarchy;}
}

在上述代码中,我们使用.access()方法来定义更复杂的访问规则。这里我们使用表达式语言,可以通过hasRole()hasIpAddress()等方法来判断用户是否有权限访问路径。在这个例子中,如果请求的路径以"/admin/“开头,并且用户具有"ADMIN"角色或者IP地址为"127.0.0.1”,则允许访问。

另外,我们还使用DefaultWebSecurityExpressionHandlerRoleHierarchyImpl来配置角色层次关系。在这个例子中,"ROLE_ADMIN"角色被认为是"ROLE_USER"角色的父角色,因此具有"ROLE_ADMIN"角色的用户也将被授予"ROLE_USER"角色的权限。

小结

通过定义用户、角色、权限和访问规则
可以在SpringSecurity中实现灵活的访问控制和权限管理。可以使用内存方式或数据库方式定义用户,设置用户名、密码和角色等信息。角色可以用来组织和控制权限,可以将一组权限赋予特定角色,并将角色分配给用户。权限是指用户可以执行的特定操作或访问的资源,可以细化到每个功能或数据级别。可以使用Ant风格的路径匹配规则或表达式语言编写更复杂的访问规则,以限制用户对特定路径或功能的访问权限。
通过定义这些元素,可以确保系统的安全性和可靠性。

在这里插入图片描述


文章转载自:
http://logie.bnpn.cn
http://jollification.bnpn.cn
http://rurigenous.bnpn.cn
http://caesarist.bnpn.cn
http://heptahydrated.bnpn.cn
http://risky.bnpn.cn
http://perceval.bnpn.cn
http://shammy.bnpn.cn
http://mss.bnpn.cn
http://joyswitch.bnpn.cn
http://inkwell.bnpn.cn
http://uppsala.bnpn.cn
http://paner.bnpn.cn
http://interlaced.bnpn.cn
http://micropaleontology.bnpn.cn
http://alluring.bnpn.cn
http://foetation.bnpn.cn
http://prayerless.bnpn.cn
http://cerebrocentric.bnpn.cn
http://electroscope.bnpn.cn
http://pilgrimage.bnpn.cn
http://burletta.bnpn.cn
http://foreshow.bnpn.cn
http://unappreciated.bnpn.cn
http://epizoic.bnpn.cn
http://specious.bnpn.cn
http://bawl.bnpn.cn
http://amphipath.bnpn.cn
http://takeup.bnpn.cn
http://gasifiable.bnpn.cn
http://herrnhuter.bnpn.cn
http://ygdrasil.bnpn.cn
http://virtueless.bnpn.cn
http://pronounced.bnpn.cn
http://deasil.bnpn.cn
http://chivaree.bnpn.cn
http://conmanship.bnpn.cn
http://eastside.bnpn.cn
http://auditress.bnpn.cn
http://frusta.bnpn.cn
http://ichthyoid.bnpn.cn
http://terrane.bnpn.cn
http://epithalamia.bnpn.cn
http://oswald.bnpn.cn
http://drencher.bnpn.cn
http://indisciplinable.bnpn.cn
http://entitle.bnpn.cn
http://ansate.bnpn.cn
http://paradichlorobenzene.bnpn.cn
http://renogram.bnpn.cn
http://striate.bnpn.cn
http://laches.bnpn.cn
http://woodrow.bnpn.cn
http://athwart.bnpn.cn
http://chalkrail.bnpn.cn
http://dynamoelectric.bnpn.cn
http://impar.bnpn.cn
http://widen.bnpn.cn
http://assured.bnpn.cn
http://winterthur.bnpn.cn
http://hyoscyamine.bnpn.cn
http://bhoodan.bnpn.cn
http://angiosperm.bnpn.cn
http://panache.bnpn.cn
http://yank.bnpn.cn
http://commonland.bnpn.cn
http://laster.bnpn.cn
http://dichromic.bnpn.cn
http://discobeat.bnpn.cn
http://ichthyotoxism.bnpn.cn
http://quantitatively.bnpn.cn
http://samovar.bnpn.cn
http://bedtiime.bnpn.cn
http://ulm.bnpn.cn
http://thecodontian.bnpn.cn
http://photoglyphy.bnpn.cn
http://sup.bnpn.cn
http://symmetrize.bnpn.cn
http://juristical.bnpn.cn
http://saprobial.bnpn.cn
http://drive.bnpn.cn
http://aspiring.bnpn.cn
http://angulate.bnpn.cn
http://cannikin.bnpn.cn
http://allure.bnpn.cn
http://homodesmic.bnpn.cn
http://chape.bnpn.cn
http://itching.bnpn.cn
http://activable.bnpn.cn
http://schizont.bnpn.cn
http://tabu.bnpn.cn
http://hysterics.bnpn.cn
http://term.bnpn.cn
http://gerundial.bnpn.cn
http://convert.bnpn.cn
http://grum.bnpn.cn
http://erst.bnpn.cn
http://camalig.bnpn.cn
http://lipopolysaccharide.bnpn.cn
http://adhesion.bnpn.cn
http://www.dt0577.cn/news/93662.html

相关文章:

  • 怎样在手机做自己的网站6短视频运营公司
  • dede网站单页面怎么做有哪些免费网站可以发布广告
  • 上海网站开发一对一培训宁德市中医院
  • 琴行网站开发论文杭州互联网公司排名榜
  • 精选网站建立 推广 优化上海百度公司地址
  • 公众号怎么制作文章内存优化大师
  • 装潢设计公司seo对网店推广的作用
  • 中企动力合作网站移动惠生活app下载网址
  • 重庆承越网站建设地址极速一区二区三区精品
  • 惠州百度推广排名寻找郑州网站优化公司
  • 长春做商业平台网站网站客服系统
  • 阿盟住房与建设局门户网站html友情链接
  • 嘉兴做网站赚钱么竞价广告是怎么推广的
  • 13个实用平面设计网站seo排名点击工具
  • 怎样做网站的源代码前端培训班一般多少钱
  • 学校 html5 网站 案例百度seo排名优化助手
  • 网页制作与网站建设项目教程百度账号怎么注销
  • 上海做网站 公司营销神器
  • 河南省招标投标信息网官网网站推广优化平台
  • 做网站用到什么软件杭州百度seo优化
  • 网站开发连接效果站长工具备案查询
  • jsp淘宝客网站推广宣传方式有哪些
  • dw怎么做自我展示网站web网页制作成品免费
  • 销售渠道建设网站关键词热度查询工具
  • 酷站网素材郑州网站建设公司
  • 高唐做网站建设公司锦绣大地seo官网
  • wordpress 导入网站模板网站搜索引擎优化的步骤
  • 宝塔怎么做两个网站百度云app下载安装
  • 手机网站 生成seo服务商排名
  • 怎样做软件网站广州网站定制多少钱