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 ...
随机推荐
- Spring Boot中验证码实现kaptcha
要生成验证码网上的方案比较多,基本是基于两大类:1为自定义生成,操作用Image类,2为kaptcha生成,有模糊算法. 当然也可以直接交由前端进行处理 1.基于kaptcha 首先不要怀疑的是报名是 ...
- vue-alioss-组件封装
<template> <div class="vui_alioss_upload"> <div @click="uloadImg()&quo ...
- POJ3255 Roadblocks 【次短路】
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7760 Accepted: 2848 Descri ...
- Android c/c++ 应用向linux 平台迁移执行
鉴于近期在minicamera (linux + FPGA) 平台上模拟 安卓系统camera流程遭遇不断升级同步的困扰.尤其是 不开放源代码后 , 应用要链接封闭动态库造成的困难.以及在sprdis ...
- spring 学习资料
spring 官方教程 1.3.1 http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/index.html ...
- Angular2.x-主/细节组件
此刻,HeroesComponent显示heroes列表和所选heroes的详细信息. 随着应用程序的增长保持一个组件中的所有功能将不可维护.您需要将大型组件分成更小的子组件,每个组件都专注于特定的任 ...
- Hadoop-08-Hive本地独立式安装
1.安装mysql sudo apt-get install mysql-server mysql-client 2.使用root账户登录mysql数据库,新建存放hive元数据的数据库.如果叫hiv ...
- 输入年份,然后打印出该年的万年历,以及标识出当天日期。相似于linux下的cal -y结果。
public class Permanent { public static boolean isLeapYear(int year){//能被4整除但不能被100整除.或者能被400整除 boole ...
- Linux下怎么添加和查看PATH环境变量
linux下查看和添加PATH环境变量来自:http://apps.hi.baidu.com/share/detail/32942984 $PATH:决定了shell将到哪些目录中寻找命令或程序,PA ...
- Python爬虫开发【第1篇】【机器视觉及Tesseract】
ORC库概述 在读取和处理图像.图像相关的机器学习以及创建图像等任务中,Python 一直都是非常出色的语言.虽然有很多库可以进行图像处理,但在这里我们只重点介绍:Tesseract 1.Tesser ...