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

阿里云做企业网站手机网站开发模拟器

阿里云做企业网站,手机网站开发模拟器,管理培训课程,电商平台运营Spring Boot整合 1、RBAC 权限模型 RBAC模型#xff08;Role-Based Access Control#xff1a;基于角色的访问控制#xff09; 在RBAC模型里面#xff0c;有3个基础组成部分#xff0c;分别是#xff1a;用户、角色和权限#xff0c;它们之间的关系如下图所示 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、启动器依赖引入 啥配置也没做啥类也没写。只是增加了一个启动器依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-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));//定义权限列表ListGrantedAuthority 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直译验证 给我的感觉就是权限就是一个字符串难道什么样的字符串都行吗为啥定义的这么模糊 那我们就姑且给他ab,c。。看看它怎么说 AuthorityUtils此类一般用于UserDetailsService的实现类中的loadUserByUsername方法 作用为给user账户添加一个或多个权限用逗号分隔底层调用的是createAuthorityList方法唯一区别在于此方法把所有的权限包含进一个字符串参数中只不过用逗号分隔。 Service public class UserDetailsServiceImpl implements UserDetailsService{AutowiredPasswordEncoder passwordEncoder;Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {//比较密码String passpasswordEncoder.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 {ListGrantedAuthority listAuthorityUtils.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 7、权限控制 Spring Security支持方法级别的权限控制。在此机制上我们可以在任意层的任意方法上加入权限注解加入注解的方法将自动被Spring Security保护起来仅仅允许特定的用户访问从而还到权限控制的目的 PreAuthorize() 该注解用于方法前验证权限 //使用权限注解标明只有拥有“admin”权限的人才能访问       PreAuthorize(hasAuthority(admin)) RestController public class HelloController {RequestMapping(/hello)public String sayHello() {return hello;}RequestMapping(/a)PreAuthorize(hasAuthority(a))public String sayA() {return aaaaa;}RequestMapping(/d)PreAuthorize(hasAuthority(d))public String sayB() {return ddddd;} } Spring Security默认是禁用注解的要想开启注解要在继承WebSecurityConfigurerAdapter的类加EnableGlobalMethodSecurity()注解并在该类中将AuthenticationManager定义为Bean。说实话我没有注入AuthenticationManager这个bean的时候也做到了权限校验。。这到底有啥用 Configuration EnableWebSecurity EnableGlobalMethodSecurity(prePostEnabled true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter {Resourceprivate UserDetailsService userDetailsService;Beanpublic PasswordEncoder passwordEncoder() {//使用默认的BCryptPasswordEncoder加密方案return new BCryptPasswordEncoder();}BeanOverridepublic AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean();}/*** 配置用户详细信息的服务和密码编码器** 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(); //所有未匹配的请求都需要用户进行身份验证} } 我们看到EnableGlobalMethodSecurity 分别有prePostEnabled 、securedEnabled、jsr250Enabled三个字段其中每个字段代码一种注解支持默认为falsetrue为开启。 重新登录访问。记得之前我随便给的权限字符串a,b,c。。。访问a是没问题的 访问d会返回403错误码。 自定义权限不足处理器来处理权限不足时候的操作 8、Session管理 用户登录成功后信息保存在服务器Session中。如Tomcat 登录后可以看到cookie中存储了JSESSIONID的cookie。 Session超时设置 如可以设置session有效期为1小时 server:session:timeout: 3600 这时候就涉及到一个session共享 当应用集群部署的时候用户在A应用上登录认证了后续通过负载均衡可能会把请求发送到B应用而B应用服务器上并没有与该请求匹配的认证Session信息所以用户就需要重新进行认证 Spring Security默认的退出登录URL为/logout Spring Security OAuth2 1、什么是OAuth OAuth是一种用来规范令牌Token发放的授权机制主要包含了四种授权模式授权码模式、简化模式、密码模式和客户端模式 OAuth相关的名词 Third-party application 第三方应用程序比如这里的虎牙直播 HTTP service HTTP服务提供商比如这里的QQ腾讯; Resource Owner 资源所有者就是QQ的所有人你 User Agent 用户代理这里指浏览器 Authorization server 认证服务器这里指QQ提供的第三方登录服务 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注解来开启此功能。 ConfigurationEnableResourceServerpublic 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.sadfv.cn/news/78902/

相关文章:

  • 做网站需要源码无忧网站模板
  • 衣服网站设计市场调研方案怎么写
  • 温州网站提升排名自己网站视频直播怎么做
  • 网站服务器搬迁wordpress淘客采集
  • wordpress做网站好吗wordpress邮件函数
  • 个人网站可以做百度推广江苏seo外包
  • 软件综合课设做网站php网站容量
  • 扬中网站优化公司电商网站html模板
  • 企业网站优化方案模板中国建设银行网站诚聘英才频道
  • 桂林相关网站包头市建设工程质量监督站网站
  • 邯郸网站建设选哪家好采集侠 wordpress
  • 美食网站建设的功能全球咨询公司排名
  • 做漂亮的二维码网站wordpress数组
  • 哪些网站可以做视频直播为某公司或企业做的门户网站
  • 网站设计内容做任务给钱的网站
  • 如何做网站结构分析湖北工程建设信息网官网
  • 网站开发小程序开发公司公司网站建设行为规定
  • 网站运营外包鲜花电子商务网站建设规划书
  • 青岛博海建设集团有限公司网站临夏网站制作
  • 单页面网站怎么优化阿里云 wordpress 建站
  • 河南省建设招投标网站电子商务网站设计怎么做
  • CMS网站建设实战试题网络论坛有些什么平台
  • 常州手机网站效果深圳品牌网站开发
  • 模仿大型门户网站做ppt做自媒体小视屏哪个网站好
  • 江门网站推广网页设计市场价
  • 为企业开发网站网页设计工资怎么样
  • 手机能用的网站流程图
  • asp做的网站数据库在哪里wordpress中front-page
  • 郑州企业网站排名优化自己搭建云手机服务器
  • 哪个网站可以免费做网页制作网架厂家