角色权限控制合集

45 篇文章 0 订阅
21 篇文章 1 订阅

后端返回角色标识

前提条件

  • 已知固定两种角色
  • 已知每种角色需要展示的菜单

思路步骤

  • 登录获取角色标识
  • 根据角色在vuex中存储该权限拥有的权限数组
[{  path: '/foo',
	name:'foo', 
	redirect:'',
	meta: {
      icon: "weibo",
      title: "首页",
    }, 
    children: [],
    component: Foo }]
  • 根据路由数组生成路由文件和菜单数组
// 路由文件引入所有文件
const Foo = () => import('./Foo.vue')

注意

  • vuex中新设置的值,当网页刷新时会丢失,恢复初始值
  • 所以采用存在本地的方式

后台根据不同的角色返回路由

  • 当页面刷新或者第一次进来非登录页(页面刷新store中的数据会回复初始值)
  • 调取接口获取路由数据,并存储在vuex中
  • 使用router.addRoutes() 添加动态路由
  • 最后next({ … to }) 继续跳转页面
    在这里插入图片描述
  • 从vuex中获取数据,生成菜单
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Spring Boot中实现不同角色权限控制,可以使用Spring Security框架。Spring Security是一个基于Spring的安全框架,它提供了一套安全性认证和授权的解决方案,可以帮助我们实现各种权限控制。 以下是实现不同角色权限控制的步骤: 1. 添加Spring Security依赖 在pom.xml文件中添加Spring Security的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 创建Spring Security配置类 创建一个配置类,继承自WebSecurityConfigurerAdapter,并添加@EnableWebSecurity注释,该注解启用Spring Security的Web安全性。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { // ... } ``` 3. 配置用户和角色 在SecurityConfig类中,可以配置用户和角色。可以使用内存、数据库或LDAP等方式来存储用户和角色信息。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER") .and() .withUser("admin").password("{noop}password").roles("USER", "ADMIN"); } } ``` 4. 配置访问权限 在SecurityConfig类中,可以配置访问权限。可以使用antMatcher()、regexMatcher()、mvcMatcher()等方法来指定访问路径,使用hasRole()、hasAnyRole()、authenticated()等方法来指定角色权限。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/", "/home").permitAll() .antMatchers("/admin/**").hasRole("ADMIN") .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll(); } } ``` 以上是实现不同角色权限控制的基本步骤。当用户访问需要权限的资源时,Spring Security将验证用户是否拥有相应的角色权限。如果用户没有相应的角色权限,将会被拒绝访问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曲曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值