zoukankan      html  css  js  c++  java
  • shiro自定义realm

    [main]
    myAuthorRealm=cn.shiro.mytest.realm.MyAuthorRealm
    securityManager.realms=$myAuthorRealm

    /**
     * 自定义realm
     * 在这个类中处理身份认证及权限判断
     * @author Administrator
     *
     */
    public class MyAuthorRealm extends AuthorizingRealm {
    
        /**
         * 完成授权
         */
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) {
            //从登录界面传递过来 username = "zhangwanfu"
            String username = (String)pc.getPrimaryPrincipal();
            //查询数据库得到授权
            String permission = "add,delete,update,select";
            String[] pers = permission.split(",");
            SimpleAuthorizationInfo simpleAuthorizationInfo
                 = new SimpleAuthorizationInfo();
            //将4条字符串添加到授权集合
            for(String pr:pers){
                simpleAuthorizationInfo.addStringPermission(pr);
            }
            
            return simpleAuthorizationInfo;
        }
    
        /**
         * 完成身份认证
         */
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(
                AuthenticationToken token) throws AuthenticationException {
            
            //从登录传过来的username  zhangwanfu
            String username = (String)token.getPrincipal();
            //查询数据库得到zhangwanfu的密码
            String password = "123456";
            SimpleAuthenticationInfo simpleAuthenticationInfo
                 = new SimpleAuthenticationInfo(username, password, "MyAuthorRealm");
            return simpleAuthenticationInfo;
        }
    
    }
  • 相关阅读:
    我的第一个开源项目
    读headFirst设计模式
    读headFirst设计模式
    读headFirst设计模式
    eclipse中svn插件的安装和tortoiseSVN的安装
    读headFirst设计模式
    浅析博客园的保存密码并自动登录, 然后自己写一个demo
    java中易遗忘的知识,不定时更新……
    POI操作Excel
    java泛型基础
  • 原文地址:https://www.cnblogs.com/yoyo198212/p/8413091.html
Copyright © 2011-2022 走看看