Shiro授权数据的持久化

本文介绍了如何使用Shiro框架进行用户授权时,如何设计数据库表结构、创建Role实体并实现Role与User的关联,以及如何通过Spring Data JPA进行数据持久化和查询。重点讲解了SQL映射和字符串资源的配置。
摘要由CSDN通过智能技术生成

Shiro授权数据的持久化


1. 表结构

  • 既然要做持久化,那我们就得需要数据库表, 先来看看我们的表结构吧!
    在这里插入图片描述
  • role

    在这里插入图片描述
  • role_user

    在这里插入图片描述

2. 角色

  • 创建实体Role
    public class Role {
        private int id;
        private String name;
        ...
    
  • 在User中设置Role的属性
    public class User {
    
    private Integer id;
    private String username;
    private String password;
    private String salt;
    private String role;
    
    private List<Role> roles;
    ...
    
  • 通过username查询对应的Role
    public class CustomerRealm extends AuthorizingRealm {
    
        @Autowired
        private UserService userService;
    
        /**
         * 授权
         * @param principalCollection
         * @return
         */
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    
            //getUsername
            String username = (String) principalCollection.getPrimaryPrincipal();
    
            //getRole
            User user = userService.queryUserRoleByUserName(username);
    
            //roles
            List<Role> roles = user.getRoles();
    
            //SimpleAuthorizationInfo
            SimpleAuthorizationInfo simpleAuthorizationInfo = null;
    
            //添加角色
            if (!CollectionUtils.isEmpty(roles)){
                //SimpleAuthorizationInfo
                simpleAuthorizationInfo = new SimpleAuthorizationInfo();
                for (Role role: roles) {
                    simpleAuthorizationInfo.addRole(role.getRole());
                }
            }
    
            return simpleAuthorizationInfo;
        }
    
  • sql
    
    <resultMap id="UserRoleResultMap" type="com.cm.shiro.shirotest.model.User">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="username" jdbcType="VARCHAR" property="username" />
        <result column="password" jdbcType="VARCHAR" property="password" />
        <result column="salt" jdbcType="VARCHAR" property="salt" />
        <result column="role" jdbcType="VARCHAR" property="role" />
        <collection property="roles" ofType="com.cm.shiro.shirotest.model.Role" select="selectRole" column="id">
            <id column="id" jdbcType="BIGINT"  property="id"/>
            <result column="name" jdbcType="VARCHAR"  property="name"/>
        </collection>
    </resultMap>
    
    <!--通过username主身份信息查询role-->
        <select id="selectUserRoleByUsername" resultMap="UserRoleResultMap">
          select id,username,password,salt from test where username = #{username}
        </select>
    
        <select id="selectRole" resultType="com.cm.shiro.shirotest.model.Role">
            select role.id id,role.name name
            from
            t_role role
            join
            (select roleid from t_role_user where userid = #{id}) roleuser
            on
            role.id = roleuser.roleid
        </select>
    

3. 字符串资源

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值