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

花钱做网站百度打开百度搜索

花钱做网站,百度打开百度搜索,日本软银集团股东结构,网络推广公司怎么接单Spring Security OAuth2 第一章 Spring Security 快速入门 第二章 Spring Security 自定义配置 第三章 Spring Security 前后端分离配置 第四章 Spring Security 身份认证 第五章 Spring Security 授权 第六章 OAuth2 文章目录 Spring Security OAuth21、基于request的授权1…

Spring Security + OAuth2

第一章 Spring Security 快速入门
第二章 Spring Security 自定义配置
第三章 Spring Security 前后端分离配置
第四章 Spring Security 身份认证
第五章 Spring Security 授权
第六章 OAuth2


文章目录

  • Spring Security + OAuth2
  • 1、基于request的授权
    • 1.1、用户-权限-资源
      • 需求
      • 配置权限
      • 授权权限
      • 请求未授权的接口
    • 1.2、用户-角色-资源
      • 配置角色
      • 授权角色
    • 1.3、用户-角色-权限-资源
  • 2、基于方法的授权
    • 2.1、开启方法授权
    • 2.2、给用户授予角色和权限
    • 2.3、常用授权注解


授权管理的实现在SpringSecurity中非常灵活,可以帮助应用程序实现以下两种常见的授权需求:

  • 用户-权限-资源:例如张三的权限是添加用户、查看用户列表、李四的权限是查看用户列表
  • 用户-角色-权限-资源:例如张三是角色是管理员、李四的角色是普通用户,管理员能做所有操作,普通用户只能查看信息

1、基于request的授权

1.1、用户-权限-资源

需求

  • 具有USER_LIST权限的用户可以访问/user/list
  • 具有USER_ADD权限的用户可以访问/user/add

配置权限

SecurityFilterChain

package com.security.demo.config;
import ...@Configuration //配置类
public class WebSecurityConfig {@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {// authorizeHttpRequests() 开启授权保护// anyRequest() 对所以请求开启授权保护// authenticated()已认证请求会自动授权。http.authorizeHttpRequests(authorize -> authorize//具有USER_LIST权限的用户可以访问/user/list                   .requestMatchers("/user/list").hasAnyAuthority("USER_LIST")//具有USER_ADD权限的用户可以访问/user/addd.requestMatchers("/user/add").hasAnyAuthority("USER_ADD")//对所有请求开启授权保护.anyRequest()//已认证的请求会被自动授权.authenticated());...
}

授权权限

DBUserDetailsManager中的loadUserByUsername方法:

 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {QueryWrapper<User> queryWrapper = new QueryWrapper();queryWrapper.eq("username",username);User user = userMapper.selectOne(queryWrapper);if (user==null){throw  new UsernameNotFoundException(username);}else{Collection<GrantedAuthority> authorities = new ArrayList<>();//模拟权限authorities.add(()->"USER_LIST");authorities.add(()->"USER_ADD");return   new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPassword(),user.getEnabled(),true,//用户账号是否过期。true,//用户凭证是否过期true,//未被锁定authorities//权限列表);}}

请求未授权的接口

实现AccessDeniedHandler 接口

package com.security.demo.config;public class MyAccessDeniedHandler implements AccessDeniedHandler {@Overridepublic void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException exception) throws IOException, ServletException {//创建结果对象HashMap result = new HashMap();result.put("code",-1);result.put("message","没有权限");//转换成json字符串String json = JSON.toJSONString(result);//返回响应response.setContentType("application/json;charset=UTF-8");response.getWriter().println(json);}
}

SecurityFilterChain添加配置请求未授权的处理。

package com.security.demo.config;
import ...@Configuration //配置类
public class WebSecurityConfig {@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {...http.exceptionHandling(exception -> {exception.accessDeniedHandler(new MyAccessDeniedHandler());//请求未授权的处理});...
}

1.2、用户-角色-资源

配置角色

package com.security.demo.config;
import ...@Configuration //配置类
public class WebSecurityConfig {@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http.authorizeHttpRequests(authorize -> authorize.requestMatchers("/user/**").hasRole("ADMIN").anyRequest().authenticated());...
}

授权角色

@Component
public class DBUserDetailsManager implements UserDetailsManager, UserDetailsPasswordService {@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {QueryWrapper<User> queryWrapper = new QueryWrapper();queryWrapper.eq("username",username);User user = userMapper.selectOne(queryWrapper);if (user==null){throw  new UsernameNotFoundException(username);}else{//模拟授权return org.springframework.security.core.userdetails.User.withUsername(user.getUsername()).password(user.getPassword()).disabled(!user.getEnabled())//用户是否禁用.credentialsExpired(false)//是否过期.accountLocked(false).roles("ADMIN").build();}}
}

1.3、用户-角色-权限-资源

RBAC(Role-Based Access Control,基于角色的访问控制)是一种常用的数据设计方案,它将用户的权限分配和管理与角色相关联。以下是一个基本的RBAC数据库设计方案的示例:

1、用户表(User table):包含用户的基本信息,例如用户名、密码和其他身份验证信息。

列名数据类型描述
user_idint用户ID
usernamevarchar用户名
passwordvarchar密码
emailvarchar电子邮件

2、角色表(Role table):储存所有可能得角色及其描述。

列名数据类型描述
role_idint角色ID
role_namevarchar角色名称
descriptionvarchar角色描述

3、权限表(Permission table):定义系统中所有可能得权限

列名数据类型描述
permission_idint权限ID
permission_namevarchar权限名称
descriptionvarchar角色描述

4、用户角色关联表(User-Role table):将用户与角色关联起来。

列名数据类型描述
user_role_idint用户角色关联ID
user_idint用户ID
role_idint角色ID

5、角色权限关联表(Role-Permission table):将角色与权限关联起来。

列名数据类型描述
role_permission_idint用户角色关联ID
role_idint角色ID
permission_idint权限ID

在这个设计方案中,用户可以被分配一个或多个角色,而每个角色又可以具有一个或多个权限。通过对用户角色关联和角色权限关联表进行操作,可以实现灵活的权限管理和访问控制。

2、基于方法的授权

2.1、开启方法授权

在配置文件中添加@EnableMethodSecurity注解,并修改authorizeHttpRequests

@Configuration //配置类
@EnableMethodSecurity
public class WebSecurityConfig {...http.authorizeHttpRequests(authorize -> authorize.anyRequest().authenticated());
}

2.2、给用户授予角色和权限

DBUserDetailsManager中的loadUserByUsername方法:

	 return org.springframework.security.core.userdetails.User.withUsername(user.getUsername()).password(user.getPassword()).disabled(!user.getEnabled())//用户是否禁用.credentialsExpired(false)//是否过期.accountLocked(false).roles("ADMIN")//authorities和roles不能同时使用会覆盖.authorities("USER_ADD","USER_LIST").build();}

2.3、常用授权注解

    @GetMapping(path = "/list")@PreAuthorize("hasRole('ADMIN') and authentication.name == 'abc'")public List<User> getList(){return userService.list();}@PreAuthorize("hasAuthority('USER_ADD')")@PostMapping(path = "/add")public void add(@RequestBody User user){userService.saveUserDetails(user);}
http://www.wangmingla.cn/news/107904.html

相关文章:

  • 怎么做代刷网站临沂森拓网络科技有限公司
  • 小说网站做公众号好还是网站好长沙网站seo排名
  • 电商网站制作价格网络营销推广公司名称
  • 正规网站建设价格实时热榜
  • 做黄色网站多少年今日头条收录入口
  • 富士康做电商网站怎么自己做网站
  • 一键做网站佛山seo教程
  • 做qq的网站互联网营销师报考条件
  • 泰州哪家做网站建设比较好深圳市seo上词多少钱
  • 品牌展示榜ui做的好的网站淘宝关键词指数查询
  • 欧洲美女网站网络营销网站
  • 网站建设特点淘宝关键词查询
  • 中组部两学一做网站公司企业网站制作需要多少钱
  • 在郑州建设网站这么做腰椎间盘突出压迫神经腿疼怎么治
  • wap手机网站开发软件企业培训
  • 佛山 做网站域名查询ip138
  • 企业网站注销流程创建软件平台该怎么做
  • 上海地产网站建设郑州seo公司
  • 浙江网缘科技有限公司上海百度首页优化
  • qq邮箱 wordpress南昌seo公司
  • 朋友叫我去柬埔寨做彩票网站推广宁夏百度公司
  • 绍兴做网站比较专业的公司合肥头条今日头条新闻最新消息
  • 一个人可做几次网站备案百度高级搜索引擎
  • 两个网站做反向代理广告传媒公司主要做什么
  • 武汉设计工程学院教务处东莞市网络seo推广价格
  • 做微信号公众号用网站还是App最近三天的国内新闻
  • 网站 相对路径优化seo网站
  • 做网站找那些公司交友网站有哪些
  • 百度云服务器做asp网站网站收录查询
  • 为企业为什么做网站火星时代教育培训机构官网