zoukankan      html  css  js  c++  java
  • shiro认证

    shiro权限认证:

           

    具体的认证流程是这样的:

    一般流程:

           

    通过.ini的文件来初始化工厂,.ini的文件的好处是可以创建多个组,而.properties的文件只能创建一组。

    系统默认有shiro.ini的文件,但是一般我们是自定义数据源Realm:来存放数据;

    该类如下:这里采用了模拟数据库;

    package cn.itcast.shiro;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.apache.shiro.authc.AuthenticationException;
    import org.apache.shiro.authc.AuthenticationInfo;
    import org.apache.shiro.authc.AuthenticationToken;
    import org.apache.shiro.authc.SimpleAuthenticationInfo;
    import org.apache.shiro.authz.AuthorizationInfo;
    import org.apache.shiro.realm.AuthorizingRealm;
    import org.apache.shiro.subject.PrincipalCollection;
    
    public class TestCustomRealm extends AuthorizingRealm{
    	  //模拟数据库
    	private static HashMap<String,String> userInfo=new HashMap<String,String>();
    	static{
    		userInfo.put("zhangsan","123456");
    		userInfo.put("lisi","1234");
    	}
         @Override
    	public void setName(String name) {
    		// TODO Auto-generated method stub
    		super.setName("testCustomRealm");
    	}
       //认证功能
    		@Override
    		protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    		  String userCode=(String) token.getPrincipal();
    		  String pwd=null;
    		  for (Map.Entry<String,String> entry:userInfo.entrySet()) {
    			  pwd=entry.getValue();
    			  break;
    		}
    		  SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(userCode, pwd,this.getName());
    			return simpleAuthenticationInfo;
    		}
    
    	//授权功能
    	@Override
    	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
    		
    		return null;
    	}
    
    
    }
    

      测试的话就是跟之前一样创建工厂,不同的是运用了.ini的文件换了。

          

  • 相关阅读:
    Winform中让回车键完成TAB键的功能
    ASP.NET跨页传值方法汇总
    SQL SERVER中使用Unicode字符的注意问题
    如何为Oracle配置多个监听器
    如何实现上一条、下一条的功能
    "文件中的备份集是由BACKUP DATABASE...FILE=创建的,无法用于此还原操作"的解决办法
    [psp][lumines]dat数据包解包程序
    meteos@pc, the remake制作中...
    最近在仿照Lumines写
    建立huffman树,当然用堆排序
  • 原文地址:https://www.cnblogs.com/fengli9998/p/6561372.html
Copyright © 2011-2022 走看看