shiro之SimpleAccountRealm
我使用的是maven构建的工程,junit测试
Shiro认证过程
创建SecurityManager---》主体提交认证---》SecurityManager认证---》Authenticsto认证---》Realm验证 Shiro授权过程
创建SecurityManager---》主体授权---》ecurityManager授权---》Authorizer授权---》Realm获取角色权限数据
1.导入依赖
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ylht-shiro</artifactId>
<groupId>com.ylht</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>shiro-test</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency> <!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency> </dependencies> </project>
2.测试类
package com.ylht.shiro.test; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Before;
import org.junit.Test; public class AuthenticateTest { //定义realm
SimpleAccountRealm realm = new SimpleAccountRealm(); @Before
public void addUser(){
//realm定义多个角色
//username,password,roles(后面用逗号隔开)
realm.addAccount("admin","admin","admin","user");
} @Test
public void testAuthenticate() {
//1.创建SecurityManager
DefaultSecurityManager securityManager = new DefaultSecurityManager();
securityManager.setRealm(realm); //2.主题提交认证
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken("admin", "admin");
subject.login(token); System.out.println(subject.isAuthenticated()); //subject.logout(); //System.out.println(subject.isAuthenticated());
//验证多个授权
subject.checkRoles("admin","user"); }
}
shiro之SimpleAccountRealm的更多相关文章
- 构建 shiro struts2 spring3 mybatis 的maven项目
书接上回 构建 struts2 spring3 mybatis 的maven项目 构建 pom.xml 继续在原有框架下 融合shiro ,具体shiro是啥 这里就不解释了,恩 反正功能挺强大的 本 ...
- shiro权限控制(一):shiro介绍以及整合SSM框架
shiro安全框架是目前为止作为登录注册最常用的框架,因为它十分的强大简单,提供了认证.授权.加密和会话管理等功能 . shiro能做什么? 认证:验证用户的身份 授权:对用户执行访问控制:判断用户是 ...
- Shiro安全框架【快速入门】就这一篇!
Shiro 简介 照例又去官网扒了扒介绍: Apache Shiro™ is a powerful and easy-to-use Java security framework that perfo ...
- shiro三连斩之第一斩
通过JavaSE,创建不同的 realm ,由简单到复杂一步步的深入的理解shiro完成认证与授权内在联系 推荐从下向上一步步的测试,每一个方法都有详细的注释,说明 从哪里来-->到哪里去,理 ...
- Shiro安全框架入门笔记
入门 1.simpleRealmTest package cn.realm; import org.apache.shiro.SecurityUtils; import org.apache.shir ...
- Shiro的认证授权
shiro安全框架入门整理 package com.shiro.test; import org.apache.shiro.SecurityUtils; import org.apache.shiro ...
- shiro 基本知识测试
shiro 基本知识测试 <!--shiro核心包--> <dependency> <groupId>org.apache.shiro</groupId> ...
- shiro与spring集成
简介 Apache Shiro 是 Java 的一个安全(权限)框架.主要提供了认证.授权.加密和会话管理等功能. Authentication:身份认证/登录,验证用户是不是拥有相应的身份:Auth ...
- Shiro认证、角色、权限
Apache Shiro 是 Java 的一个安全框架.Shiro 可以帮助我们完成:认证.授权.加密.会话管理.与 Web 集成.缓存等. Shiro的内置Realm:IniRealm和JdbcRe ...
随机推荐
- Java开发笔记(一百)线程同步synchronized
多个线程一起办事固然能够加快处理速度,但是也带来一个问题:两个线程同时争抢某个资源时该怎么办?看来资源共享的另一面便是资源冲突,正所谓鱼与熊掌不可兼得,系统岂能让多线程这项技术专占好处?果然是有利必有 ...
- LinkedList总结
1,LinkedList也是继承了List的接口 所以在LinkedList中存储的也是有序的,不唯一的数据 它采用的是链表式储存,所以比较适合用来执行插入,删除等功能 2,LinkedList特有的 ...
- java.util.MissingResourceException: Can't find resource for bundle oracle.sysman.db.rsc.LoginResourc
http://blog.itpub.net/197458/viewspace-1055358/ oracle 10.2.0.4 windows 2003 X64 平台 系统安装EMCA正常.第一次 ...
- Codeforces div.2 B. The Child and Set
题目例如以下: B. The Child and Set time limit per test 1 second memory limit per test 256 megabytes input ...
- Java线程池的简单使用
最近由于公司的业务需求,需要使用线程池来进行对数据进行处理,所以就简单的学习了一下线程池的东西,刚接触感觉挺难的,不过使用了就不感觉那么难了,还是蛮简单的, package com.yd.sms.jo ...
- How to Use SFTP ?
Usage Build a SFTP session with your linux like server, e.g, by the tool "Xshell" or any y ...
- Hadoop DistributedCache使用案例
背景 公司数据处理具有两个计算框架,单机框架和MR框架.眼下我已经抽象出一套API interface, 供业务计算开发者使用. 并分别在两个计算框架下实现了API的运行调度.应用开发者有时间须要通过 ...
- Visual Studio Code Unit Testing
1.NUnit project.json { "version": "1.0.0-*", "testRunner": "nunit ...
- FMDB中常用SQL使用
大家工作中,最常用到的无非是 增.删.查.改... 在SQL中对应的语句为:INSERT DELETE SELECT UPDATE 首先,你可以使用一款叫做“sqlite database brows ...
- [学习笔记]overthewire bandit 通关秘籍
1.第一关 使用putty等工具连入linux即可,注意port等设置. ls 列目录内文件: cat readme,显示文件内容,即可看到密码. 2.第二关 如何查看文件名为-的文件? cat ./ ...