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

营销网站如何建设国内最新新闻事件

营销网站如何建设,国内最新新闻事件,WordPress vidropro,你认为视频网站如何做推广Spring Boot整合 1、RBAC 权限模型 RBAC模型(Role-Based Access Control:基于角色的访问控制) 在RBAC模型里面,有3个基础组成部分,分别是:用户、角色和权限,它们之间的关系如下图所示 SELECT…

 Spring Boot整合

1、RBAC 权限模型

RBAC模型(Role-Based Access Control:基于角色的访问控制)

在RBAC模型里面,有3个基础组成部分,分别是:用户、角色和权限,它们之间的关系如下图所示

SELECT * FROM sec_permission;
SELECT * FROM sec_role_permission ;
SELECT * FROM sec_role;
SELECT * FROM sec_user_role;
SELECT * FROM sec_user;

2、启动器依赖引入

啥配置也没做,啥类也没写。只是增加了一个启动器依赖

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>

重新访问首页:http://localhost:8080/


3、用户名密码

默认:

用户名默认:user

密码在服务启动时打印在了控制台

自定义:

 当然我们也可以通过application.yml指定配置用户名密码

  • security.user.name 指定默认的用户名,默认为user.

  • security.user.password 默认的用户密码.

spring:security:user:name: adminpassword: admin

关闭security验证:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().permitAll().and().logout().permitAll();//配置不需要登录验证}
}

WebSecurityConfigurerAdapter 是由Spring Security提供的Web应用安全配置的适配器

WebSecurityConfigurerAdapter 是一个适配器类,允许开发者通过重写特定的方法来自定义其 Web 安全配置

创建一个配置类WebSecurityConfig继承WebSecurityConfigurerAdapter这个抽象类并重写configure(HttpSecurity http)方法,可以精确地定义哪些URL可以由哪些角色访问。

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.formLogin() // 表单方式.and().authorizeRequests() // 授权配置.anyRequest().authenticated(); //所有未匹配的请求都需要用户进行身份验证}
}

Spring Security提供了这种链式的方法调用。上面配置指定了认证方式为表单登录,并且所有请求都需要进行认证。

HttpSecurity 是 Spring Security 中用于构建安全配置的一个类。通过该类,开发者可以配置许多与 HTTP 安全相关的选项,如认证、授权、CORS、CSRF 保护等

.formLogin() 是 HttpSecurity 类的一个方法,用于启用基于表单的身份验证。当你调用这个方法时,Spring Security 会自动配置登录表单的相关设置,如登录页面的 URL、登录成功和失败的处理等。你可以进一步定制这些设置,以适应你的应用程序需求。

-------------------------------

http.authorizeRequests() 是 HttpSecurity 类的一个方法,用于定义 URL 的访问权限。通过该方法,你可以指定哪些 URL 需要特定的角色或权限才能访问,哪些 URL 可以公开访问等。

--------------

.anyRequest().authenticated() 表示所有未匹配的请求都需要用户进行身份验证。


4、基于数据库的登录认证

Spring Security支持通过实现UserDetailsService接口的方式来提供用户认证授权信息。主要功能:根据用户名查询用户信息

@Service
public class CustomUserDetailsService implements UserDetailsService {@Autowiredprivate UserDao userDao;@Autowiredprivate RoleDao roleDao;@Autowiredprivate PermissionDao permissionDao;@Resourceprivate PasswordEncoder passwordEncoder;@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {//通过用户名从数据库获取用户信息User user = userDao.findByUsername(username).orElseThrow(() -> new UsernameNotFoundException("未找到用户信息 : " + username));//定义权限列表List<GrantedAuthority> authorities = new ArrayList<>();authorities.add(new SimpleGrantedAuthority("a"));authorities.add(new SimpleGrantedAuthority("b"));authorities.add(new SimpleGrantedAuthority("c"));//返回spring security的User对象//user.getPassword() 数据库中的密码已经是密文存储return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), authorities);}
}

返回值类型是UserDetails,我们可以直接使用Spring Security提供的UserDetails接口实现类org.springframework.security.core.userdetails.User


5、授权GrantedAuthority

GrantedAuthority则表示用户验证通过后被授予的权限。

SimpleGrantedAuthority

SimpleGrantedAuthority是默认的授权实现,它只存储权限(存储授予Authentication对象的权限的String表示形式),是一种简易的授权实现。

  • GrantedAuthority:直译"授予权限"
  • Authentication:直译"验证"

给我的感觉就是权限就是一个字符串,难道什么样的字符串都行吗?为啥定义的这么模糊

那我们就姑且给他"a","b","c"。。看看它怎么说

AuthorityUtils:此类一般用于UserDetailsService的实现类中的loadUserByUsername方法

作用为给user账户添加一个或多个权限,用逗号分隔,底层调用的是createAuthorityList方法,唯一区别在于此方法把所有的权限包含进一个字符串参数中,只不过用逗号分隔。

@Service
public class UserDetailsServiceImpl implements UserDetailsService{@AutowiredPasswordEncoder passwordEncoder;@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {//比较密码String pass=passwordEncoder.encode("123");//加密return new User(username,pass,AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal"));}}

createAuthorityList

将权限转换为List,如

@Service
public class UserDetailsServiceImpl implements UserDetailsService{@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {List<GrantedAuthority> list=AuthorityUtils.createAuthorityList("admin","normal");//一个权限一个参数return new User(username,pass,list);}
}

1


6、配置类中配置

实际项目中我们不会把密码明文存储在数据库中。只需要使用把BCryptPasswordEncoder对象注入Spring容器中,SpringSecurity就会使用该PasswordEncoder来进行密码校验

Spring Security实现的BCryptPasswordEncoder已经足够强大,它对相同的密码进行加密后可以生成不同的结果

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Resourceprivate UserDetailsService userDetailsService;@Beanpublic PasswordEncoder passwordEncoder() {//使用默认的BCryptPasswordEncoder加密方案return new BCryptPasswordEncoder();}/*** 配置用户详细信息的服务和密码编码器** @param auth* @throws Exception*/@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {//数据库读取的用户进行身份认证auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.formLogin() // 表单方式.and().authorizeRequests() // 授权配置.anyRequest().authenticated(); //所有未匹配的请求都需要用户进行身份验证}
}

Spring Security中的BCryptPasswordEncoder方法采用SHA-256 +随机盐+密钥对密码进行加密。SHA系列是Hash算法,不是加密算法,使用加密算法意味着可以解密(这个与编码/解码一样),但是采用Hash处理,其过程是不可逆的。

1)加密(encode):注册用户时,使用SHA-256+随机盐+密钥把用户输入的密码进行hash处理,得到密码的hash值,然后将其存入数据库中。

2)密码匹配(matches):用户登录时,密码匹配阶段并没有进行密码解密(因为密码经过Hash处理,是不可逆的),而是使用相同的算法把用户输入的密码进行hash处理,得到密码的hash值,然后将其与从数据库中查询到的密码hash值进行比较。如果两者相同,说明用户输入的密码正确。

再次访问接口:http://127.0.0.1:8089/hello

使用账号密码登录 admin/123456


Spring Security OAuth2

1、什么是OAuth

OAuth是一种用来规范令牌(Token)发放的授权机制,主要包含了四种授权模式:授权码模式、简化模式、密码模式和客户端模式

OAuth相关的名词

  1. Third-party application 第三方应用程序,比如这里的虎牙直播;

  2. HTTP service HTTP服务提供商,比如这里的QQ(腾讯);

  3. Resource Owner 资源所有者,就是QQ的所有人,你;

  4. User Agent 用户代理,这里指浏览器;

  5. Authorization server 认证服务器,这里指QQ提供的第三方登录服务;

  6. Resource server 资源服务器,这里指虎牙直播提供的服务,比如高清直播,弹幕发送等(需要认证后才能使用)。

Spring Security OAuth2主要包含认证服务器和资源服务器这两大块的实现:

认证服务器主要包含了四种授权模式的实现和Token的生成与存储

资源服务器主要是在Spring Security的过滤器链上加了OAuth2AuthenticationProcessingFilter过滤器,即使用OAuth2协议发放令牌认证的方式来保护我们的资源


2、认证授权服务器

创建认证服务器很简单,只需要在Spring Security的配置类上使用@EnableAuthorizationServer注解标注即可

使用 @EnableAuthorizationServer 注解,在应用中自动开启和配置 Spring Security OAuth 的授权服务组件。

 @EnableAuthorizationServer 注解主要是导入两个配置类,分别是:

  • AuthorizationServerEndpointsConfiguration,这个配置类主要配置授权端点,获取token的端点。大家就把对应的端点想象成controller即可,在这个controller下开放了若干个@RequestMapping,比如常见的有:/oauth/authorize(授权路径)/oauth/token(获取token)
  • AuthorizationServerSecurityConfiguration,主要是做spring-security的安全配置

3、资源服务器

资源服务器的配置也很简单,只需要在配置类上使用@EnableResourceServer注解标注即可:

通过资源服务器来保护我们指定的资源,必须在获取授权认证的时候才能访问。在SpringBoot当中,我们可以通过@EnableResourceServer注解来开启此功能。

    @Configuration@EnableResourceServerpublic class ResourceConfigure extends ResourceServerConfigurerAdapter {@Overridepublic void configure(HttpSecurity http) throws Exception {http.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED).and().authorizeRequests().antMatchers("/free/**").permitAll().and().authorizeRequests().anyRequest().authenticated().and().formLogin().permitAll();//必须认证过后才可以访问}}

http://www.hkea.cn/news/49870/

相关文章:

  • 东莞市做网站公司seo怎样
  • ps做网站大小尺寸应用商店优化
  • 网站站群建设方案知名网页设计公司
  • 广州网站建设公司哪家好专业的seo搜索引擎优化培训
  • 外国人做汉字网站seo搜索排名影响因素主要有
  • 外贸五金网站建设网站制作优化排名
  • 义乌网站建设多少钱网络平台营销
  • 怀仁有做网站的公司吗磁力搜索引擎2023
  • 建站行业都扁平化设计合肥网站推广公司哪家好
  • 做企业网站织梦和wordpress哪个好百度指数查询工具app
  • 郑州网站服务公司优化神马排名软件
  • 茶叶网站建设的优势南宁seo外包平台
  • 高古楼网站 做窗子北京seo技术交流
  • 南阳建设网站制作网络最有效的推广方法
  • 纯静态网站seoseo排名优化北京
  • 开封网站建设哪家好指数计算器
  • 网站开发 架构石家庄seo关键词排名
  • 可以免费做商业网站的cms百度seo霸屏软件
  • 哪家网站建设专业快速建站教程
  • 坪山网站建设行业现状优化seo方案
  • 做网站需要架构师吗网站平台有哪些
  • 网站建设丿选择金手指15凡科建站官网
  • 可以做外国网站文章武汉企业seo推广
  • 天津网站建设公司最好太原做网站哪家好
  • 网站代下单怎么做百度指数数据分析平台入口
  • 淘宝做动效代码的网站seo的优化方向
  • 番禺建网站公司网站搜索工具
  • 安徽万振建设集团网站长春网站推广公司
  • 网站怎么制作 推广seo超级外链工具免费
  • 中小学网站建设探讨东莞seo整站优化火速