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

Shiro授权-SSM

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

Shiro授权-SSM

一、shiro授权

 

原因:登录进来有的菜单看不到

有的没有权限

1、 在ShiroUserMapper.xml中新增内容

  select r.roleid from t_shiro_user u,t_shiro_user_role ur,t_shiro_role r
  where u.userid = ur.userid and ur.roleid = r.roleid
    and u.userid = #{userid}

2、Service层(ShiroUserMapper)
public Set getRolesByUserId(@Param("userid")Integer userid);

public Set getPersByUserId(@Param("userid")Integer userid);
3、ShiroUserService
public Set getRolesByUserId(Integer userid);

public Set getPersByUserId(Integer userid);
4、ShiroUserServiceImpl实现接口类
@Override
public Set getRolesByUserId(Integer userid) {
    return shiroUserMapper.getRolesByUserId(userid);
}

@Override
public Set getPersByUserId(Integer userid) {
    return shiroUserMapper.getPersByUserId(userid);
}
5、重写自定义realm中的授权方法

MyRealm:

//重写授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        
        System.out.println("用户授权...");
        String username = principals.getPrimaryPrincipal().toString();
        ShiroUser user = shiroUserService.queryByName(username);
        Set roles = shiroUserService.getRolesByUserId(user.getUserid());
        Set pers = shiroUserService.getPersByUserId(user.getUserid());

//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        info.addRoles(roles);
//        info.addStringPermissions(pers);

        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        info.setRoles(roles);
        info.setStringPermissions(pers);

        return info;
    }
6、结果

每一次操作的时候都会进入授权方法,每点一次操作都会进行一次判断你是什么角色,安全得到了保证,但是性能不好(可以使用缓存管理),

只有admin有权限,修改它为4 

 

 能访问到了

 

 总结:

①、正常的不使用shiro缓存管理,性能非常的差,每次操作都会进入方法,同时也证明了shiro非常安全,

②、做shiro开发不要想当然

二、注解式开发

1、常用注解介绍

  @RequiresAuthenthentication:表示当前Subject已经通过login进行身份验证;即 Subject.isAuthenticated()返回 true

  @RequiresUser:表示当前Subject已经身份验证或者通过记住我登录的

  @RequiresGuest:表示当前Subject没有身份验证或者通过记住我登录过,即是游客身份

  @RequiresRoles(value = {"admin","user"},logical = Logical.AND):表示当前Subject需要角色admin和user

  @RequiresPermissions(value = {"user:delete","user:b"},logical = Logical.OR):表示当前Subject需要权限user:delete或者user:b

2、注解的使用

使用注解式开发的原因:不可能什么都在applicationContext-shiro.xml中写

 ①、新建AnnotationController类
package com.mwy.aspect;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class AnnotationController {
//登录才能访问
    @RequiresUser
    @RequestMapping("/passUser")
    public String passUser(HttpServletRequest request){
        return "admin/addUser";
    }
    //角色才能访问
    @RequiresRoles(value = {"1","4"},logical = Logical.AND)
    @RequestMapping("/passRole")
    public String passRole(HttpServletRequest request){
        return "admin/listUser";
    }
    //权限才能访问
    @RequiresPermissions(value = {"user:update","user:view"},logical = Logical.OR)
    @RequestMapping("/passPer")
    public String passPer(HttpServletRequest request){
        return "admin/resetPwd";
    }

    @RequestMapping("/unauthorized")
    public String unauthorized(){
        return "unauthorized";
    }
}
②、添加配置(Spring-mvc.xml)

      depends-on="lifecycleBeanPostProcessor">
    


    



    
        
            
                unauthorized
            

        

    

    

③、结束jsp中测试

我在hello.jsp中测试


        shiro注解
        

  •         用户认证
        

  •     

  •         角色
        

  •     

  •         权限认证
        

界面:

 

zs只能查看身份认证的按钮内容

ls、ww可以看权限认证按钮内容

zdm可以看所有按钮的内容

 

结束!!!

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

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

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