打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Shiro 学习

参考 Shiro入门教程

1、pom

        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.2.3</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.6.1</version>
        </dependency>

2、shiro.ini

# -----------------------------------------------------------------------------
# Users and their (optional) assigned roles
# username = password, role1, role2, ..., roleN
# -----------------------------------------------------------------------------
[users]
root = secret, admin
guest = guest, guest
presidentskroob = 12345, president
darkhelmet = ludicrousspeed, darklord, schwartz
aihe = aihe, goodguy, client

# -----------------------------------------------------------------------------
# Roles with assigned permissions
# roleName = perm1, perm2, ..., permN
# -----------------------------------------------------------------------------
[roles]
admin = *
client = look:*
goodguy = winnebago:drive:eagle5

Tutorial

package com.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.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by aihe on 2017/6/14.
 */
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是org.apache.shiro.mgt.SecurityManager,而不是java.lang.SecurityManager
        // 加载配置文件
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");

        //2.解析配置文件,并且返回一些SecurityManger实例
        SecurityManager securityManager = factory.getInstance();

        //3.设置SecurityManager到静态内存区,单例模式
        SecurityUtils.setSecurityManager(securityManager);


        // 安全操作
        Subject currentUser = SecurityUtils.getSubject();

        // 在应用的当前会话中设置属性
        Session session = currentUser.getSession();
        session.setAttribute("key", "value");

        //当前我们的用户是匿名的用户,我们尝试进行登录,
        if (!currentUser.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken("aihe", "aihe");

            //this is all you have to do to support 'remember me' (no config - built in!):
            token.setRememberMe(true);

            //尝试进行登录用户,如果登录失败了,我们进行一些处理

            try {
                currentUser.login(token);

                //当我们获登录用户之后
                log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");


                // 查看用户是否有指定的角色
                if (currentUser.hasRole("client")) {
                    log.info("Look is in your role");
                } else {
                    log.info(".....");
                }

                // 查看用户是否有某个权限
                if (currentUser.isPermitted("look:desk")) {
                    log.info("You can look.  Use it wisely.");
                } else {
                    log.info("Sorry, you can't look.");
                }

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

                //登出

                currentUser.logout();

            } catch (UnknownAccountException uae) {
                //账户不存在的操作
            } catch (IncorrectCredentialsException ice) {
                //密码不正确
            } catch (LockedAccountException lae) {
                //用户被锁定了
            } catch (AuthenticationException ae) {
                //无法判断的情形
            }

        }


        System.exit(0);
    }
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
(七) 构建dubbo分布式平台-maven构建ant-framework框架的pom.xml文件配置(2)--代码生成工具
Apache Shiro 示例
spring boot2整合shiro安全框架实现前后端分离的JWT token登录验证
使用Shiro重写Session 自定义SESSION
spring boot 整合 shiro 权限框架
Apache MINA实战之 牛刀小试
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服