资源简介

这是shiro的学习过程中做的小示例

资源截图

代码片段和文件信息

package com.shiro.test;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
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 GWCheng on 2016/3/6.
 */
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“);

        //1.获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager
        IniSecurityManagerFactory factory = new IniSecurityManagerFactory(“classpath:shiro.ini“);

        //2.得到SecurityManager实例 并绑定给SecurityUtils
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);

        // get the currently executing user:
        Subject currentUser = SecurityUtils.getSubject();

        // Do some stuff with a Session (no need for a web or EJB container!!!)
        Session session = currentUser.getSession();
        session.setAttribute(“someKey“ “aValue“);
        String value = (String) session.getAttribute(“someKey“);
        if (value.equals(“aValue“)) {
            log.info(“Retrieved the correct value! [“ + value + “]“);
        }

        // let‘s login the current user so we can check against roles and permissions:
        if (!currentUser.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken(“lonestarr“ “vespa“);
            token.setRememberMe(true);
            try {
                currentUser.login(token);
            } catch (UnknownAccountException uae) {
                log.info(“There is no user with username of “ + token.getPrincipal());
            } catch (IncorrectCredentialsException ice) {
                log.info(“Password for account “ + token.getPrincipal() + “ was incorrect!“);
            } catch (LockedAccountException lae) {
                log.info(“The account for username “ + token.getPrincipal() + “ is locked.  “ +
                        “Please contact your administrator to unlock it.“);
            }
            // ... catch more exceptions here (maybe custom ones specific to your application?
            catch (AuthenticationException ae) {
                //unexpected condition?  error?
            }
        }

        //say who they are:
        //print their identifying principal (in this case a username):
        log.info(“User [“ + currentUser.getPrincipal() + “] logged in successfully.“);

        //test a role:
        if (currentUser.hasRole(“schwartz“)) {
            log.info(“May the Schwartz be with you!“);
        } else {
            log.info(“Hello mere mortal.“);
        }

        //test a typed permission (not instance-level)
        if (curr

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件         10  2016-03-06 20:31  ShiroLearn\.idea\.name

     文件        299  2016-03-06 20:38  ShiroLearn\.idea\artifacts\ShiroWeb_war.xml

     文件       5303  2016-03-09 09:51  ShiroLearn\.idea\artifacts\ShiroWeb_war_exploded.xml

     文件       1323  2016-03-07 16:37  ShiroLearn\.idea\compiler.xml

     文件         76  2016-03-06 20:31  ShiroLearn\.idea\copyright\profiles_settings.xml

     文件        200  2016-03-06 20:31  ShiroLearn\.idea\encodings.xml

     文件        241  2016-03-08 14:49  ShiroLearn\.idea\inspectionProfiles\profiles_settings.xml

     文件        751  2016-03-08 14:49  ShiroLearn\.idea\inspectionProfiles\Project_Default.xml

     文件        560  2016-03-08 11:43  ShiroLearn\.idea\libraries\Java_EE_6_Java_EE_6.xml

     文件        462  2016-03-07 17:27  ShiroLearn\.idea\libraries\Maven__antlr_antlr_2_7_7.xml

     文件        514  2016-03-07 16:52  ShiroLearn\.idea\libraries\Maven__aopalliance_aopalliance_1_0.xml

     文件        594  2016-03-06 20:43  ShiroLearn\.idea\libraries\Maven__commons_beanutils_commons_beanutils_1_8_3.xml

     文件        605  2016-03-07 18:02  ShiroLearn\.idea\libraries\Maven__commons_fileupload_commons_fileupload_1_3_1.xml

     文件        503  2016-03-07 18:02  ShiroLearn\.idea\libraries\Maven__commons_io_commons_io_2_4.xml

     文件        558  2016-03-07 16:52  ShiroLearn\.idea\libraries\Maven__commons_logging_commons_logging_1_2.xml

     文件        522  2016-03-07 17:27  ShiroLearn\.idea\libraries\Maven__com_fasterxml_classmate_1_3_0.xml

     文件        548  2016-03-08 11:37  ShiroLearn\.idea\libraries\Maven__com_google_code_kaptcha_kaptcha_2_3_2.xml

     文件        493  2016-03-08 11:32  ShiroLearn\.idea\libraries\Maven__com_mchange_c3p0_0_9_5_2.xml

     文件        598  2016-03-08 11:32  ShiroLearn\.idea\libraries\Maven__com_mchange_mchange_commons_java_0_2_11.xml

     文件        462  2016-03-07 17:27  ShiroLearn\.idea\libraries\Maven__dom4j_dom4j_1_6_1.xml

     文件        437  2016-03-08 11:33  ShiroLearn\.idea\libraries\Maven__jstl_jstl_1_2.xml

     文件        462  2016-03-07 16:50  ShiroLearn\.idea\libraries\Maven__junit_junit_3_8_1.xml

     文件        469  2016-03-08 11:56  ShiroLearn\.idea\libraries\Maven__log4j_log4j_1_2_17.xml

     文件        574  2016-03-08 17:38  ShiroLearn\.idea\libraries\Maven__mysql_mysql_connector_java_5_1_36.xml

     文件        519  2016-03-07 16:53  ShiroLearn\.idea\libraries\Maven__net_sf_ehcache_ehcache_2_10_0.xml

     文件        547  2016-03-07 17:58  ShiroLearn\.idea\libraries\Maven__net_sf_ehcache_ehcache_core_2_5_0.xml

     文件        556  2016-03-08 11:43  ShiroLearn\.idea\libraries\Maven__org_apache_commons_commons_lang3_3_4.xml

     文件        654  2016-03-07 17:27  ShiroLearn\.idea\libraries\Maven__org_apache_geronimo_specs_geronimo_jta_1_1_spec_1_1_1.xml

     文件        541  2016-03-06 20:43  ShiroLearn\.idea\libraries\Maven__org_apache_shiro_shiro_core_1_2_2.xml

     文件        541  2016-03-07 17:55  ShiroLearn\.idea\libraries\Maven__org_apache_shiro_shiro_core_1_2_4.xml

............此处省略1197个文件信息

评论

共有 条评论