shiro--简单入门

基于maven 的简单项目(非web)

pom.xml


  4.0.0

  com.xh.shiroTest
  shiroTest
  1.0-SNAPSHOT
  jar

  shiroTest
  http://maven.apache.org

  
    UTF-8
  

  
    
      junit
      junit
      3.8.1
      test
    

    
      org.apache.shiro
      shiro-core
      1.1.0
    
    
    
      org.slf4j
      slf4j-simple
      1.7.21
      runtime
    

  


shiro.ini

[users]
username=passwd,role1,role2
root=123456,admin
guest=123,guest
any=123,any
[roles]
roleName=prim1,prim2
admin=*
guest=gu:*
any=any:*

java

package com.xh.shiroTest;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by root on 17-6-3.
 */
public class Tutorial {
    private static final transient Logger log = LoggerFactory.getLogger(Tutorial.class);
    public static void main(String[] args) {
        log.info("My First Apache Shiro Application");
        IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);

        Subject curentUser = SecurityUtils.getSubject();
        Session session = curentUser.getSession();
        session.setAttribute("someKey","someValue");

        //如果未登录,就赋予它匿名用户
        if (!curentUser.isAuthenticated()){
            //UsernamePasswordToken token=new UsernamePasswordToken("any","123");
            UsernamePasswordToken token=new UsernamePasswordToken("guest","123");
            //UsernamePasswordToken token=new UsernamePasswordToken("root","123456");
            token.setRememberMe(true);
            try{
                curentUser.login(token);
                //判断角色
                if (curentUser.hasRole("any")){
                    log.info(token.getUsername()+"has role any");
                }
                if (curentUser.hasRole("guest")){
                    log.info(token.getUsername()+"has role guest");
                }
                if (curentUser.hasRole("admin")){
                    log.info(token.getUsername()+"has role admin");
                }
                //判断权限
                if (curentUser.isPermitted("any:create")){
                    log.info(token.getUsername()+"has permition any:create");
                }
                if (curentUser.isPermitted("gu:*")){
                    log.info(token.getUsername()+"has permition guest:* ,maybe guest");
                }
                if (curentUser.isPermitted("*")){
                    log.info(token.getUsername()+"has permition *,maybe admin");
                }

            }catch (Exception e){
                log.error(">>>"+token.getUsername()+":"+token.getPassword());
                log.error(">>>"+e.toString());
            }


        }
        curentUser.logout();
        System.exit(0);
    }
}








你可能感兴趣的:(shiro--简单入门)