com.demon.shiro.Tutorial.java Source code

Java tutorial

Introduction

Here is the source code for com.demon.shiro.Tutorial.java

Source

package com.demon.shiro;

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.apache.shiro.util.Factory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 
 * @author xuliang
 * @since 201979 ?2:48:11
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:com/demon/shiro/spring.xml")
public class Tutorial {

    private static final Logger logger = LoggerFactory.getLogger(Tutorial.class);

    public static void main(String[] args) {

        logger.info("first apache shiro app.");

        /*
         * SecurityManager shiro ??SecurityManager
         */
        // 1. IniSecurityManagerFactory  ini ? SecurityManager 
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:com/demon/shiro/shiro.ini");
        // 2.  SecurityManager 
        SecurityManager manager = factory.getInstance();
        // 3.  SecurityManager ???
        SecurityUtils.setSecurityManager(manager);

        // ??
        Subject subject = SecurityUtils.getSubject();
        /*
         * Subject?????
         * ?Subject??????
         */

        /*
         * Session  shiro ?? HttpSession ?????
         * HTTP  ????? API???
         */
        // ?session
        Session session = subject.getSession();
        session.setAttribute("key", "testValue");
        String value = (String) session.getAttribute("key");
        logger.info("value is : {}", value);

        if (!subject.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken("root", "secret");
            token.setRememberMe(true);

            try {
                subject.login(token);
            } catch (Exception e) {
                logger.error("login error for this token, username:" + token.getUsername(), e);
            }
        }

        logger.info("User [{}] login success.", subject.getPrincipal());

        // 
        if (subject.hasRole("admin")) {
            logger.info("you have admin role.");
        } else {
            logger.info("you don't have admin role");
        }

        // ?????
        if (subject.isPermitted("lightsaber:weild")) {
            logger.info("You may use a lightsaber ring.  Use it wisely.");
        } else {
            logger.info("Sorry, lightsaber rings are for schwartz masters only.");
        }

        // ????
        if (subject.isPermitted("winnebago:drive:eagle5")) {
            logger.info("You are permitted to 'drive' the 'winnebago' with license plate (id) 'eagle5'.  "
                    + "Here are the keys - have fun!");
        } else {
            logger.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
        }

        subject.logout();

    }

    @Autowired
    private AuthorizationTest test;

    @Test
    public void testAuthorization() {
        test.signUp();

        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken("usermanager", "12345");
            token.setRememberMe(true);

            try {
                subject.login(token);
            } catch (Exception e) {
                logger.error("login error for this token, username:" + token.getUsername(), e);
            }
        }
        test.createUser();
        test.updateUser();
    }

}