Shiro起步
2024-08-24 09:26:09
1、测试环境 IntelliJ Idea
2、pom配置
<?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">
<modelVersion>4.0.0</modelVersion> <groupId>com.mracale</groupId>
<artifactId>shiro</artifactId>
<version>1.0.0</version> <name>shiro</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.2</version>
</dependency> </dependencies> <build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
3、代码
package com.mracale; import static org.junit.Assert.assertTrue; import org.apache.shiro.SecurityUtils;
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; /**
* Unit test for simple App.
*/
public class AppTest
{
@Test
public void Test(){
//1、创建SecurityManager工厂对象:加载配置文件,创建工厂对象
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
//2、通过工厂对象,创建SecurityManager对象
SecurityManager securityManager = factory.getInstance();
//3、将securityManager绑定到当前运行环境中,让系统随时随地可以访问securityManager对象
SecurityUtils.setSecurityManager(securityManager);
//4、创建当前登录的主体,注意:此时主体没有进过认证
Subject subject = SecurityUtils.getSubject();
//5、收集主体登录的身份/凭证,即账号密码
UsernamePasswordToken token = new UsernamePasswordToken("test1","111");
//6、主体登录
try{
subject.login(token);
}catch (Exception e){
//e.printStackTrace();
}
//7、判断是否登录成功
System.out.println("验证登录是否成功"+subject.isAuthenticated());
//8、退出登录
subject.logout();
System.out.println("验证登录是否成功"+subject.isAuthenticated()); }
}
4、常见错误
最新文章
- Java递归算法——阶乘
- Extjs文本输入框
- UOJ 08 Quine 是在下输了
- 基于web的项目管理软件Redmine
- 关于开源框架GPUImage 的简单说明
- C# 实现对接电信交费易自动缴费 续(winio/winring0 自动填密码)
- MFC中在基于对话框的窗体中使用CFileDialog导致菜单栏变灰的解决方案
- date()中的getYear的小问题
- Javascript中Array(数组)对象常用的几个方法
- mac下更新自带的PHP版本到5.6
- subarray sum
- BBS--首页、个人站点、ORM
- Codeforces Round #408 (Div. 2) C. Bank Hacking
- Jmeter中的逻辑控制器(四)
- arp_filter/arp_ignore/rp_filter
- sass的基本语法与使用
- Oracle 12c登陆sys用户的方法
- 更新MySQL数据库( java.sql.SQLException: No value specified for parameter 1) 异常 解决方法
- python 基础之while无限循环
- html 标签附加文本属性
热门文章
- mysql 分区 限制
- 你不知道的JavaScript学习笔记1——作用域
- 关于Unity中的transform组件(三)
- 【BZOJ】1690: [Usaco2007 Dec]奶牛的旅行(分数规划+spfa)
- 漫游Kafka设计篇之性能优化(7)
- hdu 4291(矩阵+暴力求循环节)
- Spring_day01--课程安排_Spring概念_IOC操作&;IOC底层原理&;入门案例_配置文件没有提示问题
- 说说M451例程讲解之定时器
- 关于PostgreSQL的SQL注入必知必会
- UIWindow和UIScreen