栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

从0搭建一套spring boot项目(通用拦截器+查询到用户角色+修改用户和角色的关联表)二

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从0搭建一套spring boot项目(通用拦截器+查询到用户角色+修改用户和角色的关联表)二

目录

六、加入通用拦截器(基于路径的权限校验)

 七、通过userId查询到用户角色

 八、修改用户和角色的关联表


六、加入通用拦截器(基于路径的权限校验)

第一个是登陆验证第二个是权限校验

1、可以加多个拦截器

2、配置拦截器WebMvcConfig

//配置类
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor())
                // 拦截规则 ,拦截那些路径
                .addPathPatterns("
        // 是我们的conrtoller中的方法
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        String token = request.getHeader(GlobalConstant.HEADER_TOKEN);
        if (!TokenUtil.verify(token)) {
            // 未登录跳转到登录界面
            throw  new RuntimeException("no login!");
        }
        return true;
    }
}

localhost:8080/user/listAll

登录拦截器已经做好


4、路径的权限校验拦截器

5、配置拦截器WebMvcConfig

//配置类
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor())
                // 拦截规则 ,拦截那些路径
                .addPathPatterns("
        // 是我们的conrtoller中的方法
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        String token = request.getHeader(GlobalConstant.HEADER_TOKEN);
        User user = TokenUtil.getUser(token);
        // 一定是登陆验证成功的
       if(!user.getMenuUrlList().contains(uri)){
           throw  new RuntimeException("403 Forbidden!");
       }
        return true;
    }
}

先登录,复制下token到posman里面 访问一个没权限的

 七、通过userId查询到用户角色

1、把数据库中role实现出来

2、xml中查询出角色id


    
        select role_id from ref_user_role where user_id = #{userId}
    

3、在impl层实现查询所有角色==》查询当前用户的角色==》设置选中

@Service
public class RoleServiceImpl extends ServiceImpl implements IRoleService {
    @Override
    public List listRoleByUserId(Long userId) {
        //查询到所有的角色
        List roleAllList = this.list();
        //查询到当前用户的角色
        Set userRoleSet =  this.baseMapper.listRoleIdByUserId(userId);
        //遍历设置选中
        for(Role role : roleAllList){
            if (userRoleSet.contains(role.getId())) {
                role.setChecked(true);
            }
        }

        return roleAllList;
    }
}

4、在RoleController中

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/listRoleByUserId")
    public WebResultJson listRoleByUserId(Long userId){
        List  roleList = roleService.listRoleByUserId(userId);
        return WebResultJson.ok(roleList);
    }
}

先登录,拿到token,再去posman里面

 记得提前到数据库menu表中加入

 八、修改用户和角色的关联表

1、UserController里面

 2、UserServiceImpl里面

@Service
public class UserServiceImpl extends ServiceImpl implements IUserService {
    @Override
    public void updateUserRoles(UserRoleVo userRoleVo) {
        //多对多关联表的更新
        //1、先删除原有数据
        this.baseMapper.deleteUserRoleByUserId(userRoleVo.getUserId());
        //2、插入新数据
       // List userRoleVoList = new ArrayList<>();
        this.baseMapper.insertBatchUserRole(userRoleVo);

    }
}

3、UserMapper里面实现

public interface UserMapper extends BaseMapper {
    void deleteUserRoleByUserId(@Param("userId") Long userId);

    void insertBatchUserRole(@Param("userRoleVo") UserRoleVo userRoleVo);
}

4、UserMapper





    
        insert into ref_user_role values
        
            (
            #{userRoleVo.userId},
            #{item}
            )
        
    
    
        delete from ref_user_role where user_id = #{userId}
    

先登录+获取token

 注:

项目代码的gitee地址https://gitee.com/hbuws/jwlb.git

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939196.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号