最近在学shiro,觉得入门程序还是有用的,记下来防止遗忘,也可供大家参考。

package cn.itcast.shiro.authentication;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test; /**
*
* @author yxf
* shiro验证用户的登录
*
*/
public class AuthenticationTest { //用户登录退出
@Test
public void testLoginAndLogout() {
//创建securityManager工厂
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-first.ini"); //创建securityManager
SecurityManager securityManager = factory.getInstance(); //securityManager设置到当前运行环境中
SecurityUtils.setSecurityManager(securityManager); //SecurityUtils创建一个subject
Subject subject = SecurityUtils.getSubject(); //认证提交前准备token
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "111111"); //执行认证提交
try {
subject.login(token);
} catch (AuthenticationException e) {
e.printStackTrace();
} //是否认证通过
boolean flag = subject.isAuthenticated(); System.out.println("是否认证通过:" + flag); //退出操作
subject.logout(); flag = subject.isAuthenticated(); System.out.println("是否认证通过:" + flag); }
}

shiro权限管理入门程序的更多相关文章

  1. shiro权限管理的框架-入门

    shiro权限管理的框架 1.权限管理的概念 基本上涉及到用户参与的系统都要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户可以访问而且只能 ...

  2. (补漏)Springboot2.0 集成shiro权限管理

    原文Springboot2.0 集成shiro权限管理 一.关于停止使用外键. 原本集成shiro建立用户.角色.权限表的时候使用了外键,系统自动创建其中两个关联表,用@JoinTable.看起来省事 ...

  3. (39.4) Spring Boot Shiro权限管理【从零开始学Spring Boot】

    在读此文章之前您还可能需要先了解: (39.1) Spring Boot Shiro权限管理[从零开始学Spring Boot] http://412887952-qq-com.iteye.com/b ...

  4. (39.3) Spring Boot Shiro权限管理【从零开始学Spring Boot】

    在学习此小节之前您可能还需要学习: (39.1) Spring Boot Shiro权限管理[从零开始学Spring Boot] http://412887952-qq-com.iteye.com/b ...

  5. (39.2). Spring Boot Shiro权限管理【从零开始学Spring Boot】

    (本节提供源代码,在最下面可以下载) (4). 集成Shiro 进行用户授权 在看此小节前,您可能需要先看: http://412887952-qq-com.iteye.com/blog/229973 ...

  6. (39.1) Spring Boot Shiro权限管理【从零开始学Spring Boot】

    (本节提供源代码,在最下面可以下载)距上一个章节过了二个星期了,最近时间也是比较紧,一直没有时间可以写博客,今天难得有点时间,就说说Spring Boot如何集成Shiro吧.这个章节会比较复杂,牵涉 ...

  7. Spring Boot Shiro 权限管理 【转】

    http://blog.csdn.net/catoop/article/details/50520958 主要用于备忘 本来是打算接着写关于数据库方面,集成MyBatis的,刚好赶上朋友问到Shiro ...

  8. SpringMVC+Shiro权限管理(转载)

    源码 http://pan.baidu.com/s/1pJzG4t1 SpringMVC+Shiro权限管理 博文目录 权限的简单描述 实例表结构及内容及POJO Shiro-pom.xml Shir ...

  9. Spring Boot Shiro 权限管理

    Spring Boot Shiro 权限管理 标签: springshiro 2016-01-14 23:44 94587人阅读 评论(60) 收藏 举报 .embody{ padding:10px ...

随机推荐

  1. error_match问题

    当nginx的sendfile处于on的情况下,在docker的环境下修改js文件会出现error_match的问题,应该要在nginx的配置中把sendfile设为off

  2. gym 101858

    我这个傻逼被治了一下午. 大好的橘势,两个小时6T,去看L,哇傻逼题.然后我跑的最短路T到自闭 最后十几分钟去想了下A,一直在想如何表示状态..就是想不到二进制搞一下... 然后游戏结束了..以后我就 ...

  3. filter的知识点 和 实例

    一.过滤器Filter 1.filter的简介 filter是对客户端访问资源的过滤,符合条件放行,不符合条件不放行,并且可以对目    标资源访问前后进行逻辑处理 2.快速入门 步骤: 1)编写一个 ...

  4. Lucene入门学习二

    接上篇:增删改查 增加:这里不做过多阐述. 删除:删除全部,根据条件删除 修该:先删除,后添加 查询(*):查询所有,精确查询,根据数值范围查询,组合查询,解析查询. package com.ithe ...

  5. CTextUI 文本控件 显示数字方法

    得将数字变成字符串才行 m_ptxtCurrentcharUI->SetText(util::int32ToCString(txtLength)); 或 String.valueOf(x) 或 ...

  6. @staticmethod和classmethod

    之前一直搞不清楚这两个类方法有什么区别,今天着重学习了一下 @staticmethod是静态方法,不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样. class C(object): ...

  7. adjacent_diffenerce

    版本1: template <class InputIterator,class OutputIterator> OutputIterator adjacent_diffenerce(In ...

  8. 浅谈 pid的原理与差异

    pid  官方语言就是:比例   积分 微分.究其本质意义,比例到底是什么,原理是什么,这三个到底如何在物理世界这种运作的,大概了解的人又很少.过惯了拿起数据公式无脑推的日子的人更是如此,数学公式是很 ...

  9. laravel发布订阅

    1.php artisan make:command RedisSubscribe 在app console中会生成RedisSubscribe.php文件 <?php namespace Ap ...

  10. Docker入门3------手动编辑自定义镜像

    手动编辑自定义镜像 查看本地现有镜像: 基于centos创建一个,会自动下载centos最新原始镜像 docker run -it --name=web centos /bin/bash 然后在容器内 ...