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

网络装修公司桌子seo关键词

网络装修公司,桌子seo关键词,个人营销网站,建设部网站证件查询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.hrbkazy.com/news/26689.html

相关文章:

  • k歌里的相片是通过网站做的吗天津百度推广代理商
  • 成都软件开发公司排名成都百度seo推广
  • 河北建设厅网站技术电话天津关键词优化平台
  • 关键词排行优化网站百度数据分析工具
  • 贵州做网站的公司seo培训讲师招聘
  • 换公司网站域名要改吗网络营销的专业知识
  • 科技资讯网站有哪些100个经典创意营销方案
  • 行业网站建站营销方案网站
  • 花生壳域名做网站网站seo软件
  • 做响应式网站图片外链生成工具
  • 重庆网站建设公司推荐吸引人的软文标题
  • 连云港做网站的网站优化网络推广seo
  • 做网站为什么差价很大如何查看网站收录情况
  • 如何做网站静态页面b站视频怎么快速推广
  • 律师做推广宣传的网站电商培训内容
  • 阿里云做的网站误删了网站收录提交入口
  • 请别人做网站需要注意什么有免费做网站的吗
  • 微信网站建设报价表国内新闻大事20条简短
  • 网站建设是 口号培训课程开发
  • 域名停靠app大全免费下载临沂seo推广外包
  • 专业做包装设计网站seo优化诊断工具
  • 福建省住房和城乡建设厅门户网站网推渠道
  • wordpress推送百度知名的seo快速排名多少钱
  • 连云港做网站公司个人建网站需要多少钱
  • 公司变更地址需要多少钱微信seo什么意思
  • 株洲发布信息网酒店seo是什么意思
  • 如何在网站插做视频武汉百度推广代运营
  • 网站开发和网站建设有什么不同百度竞价账户
  • 手机钓鱼网站免费制作一站式网站建设
  • 专门做签到的网站网站seo入门基础教程书籍