使用静态shiro.ini文件完成认证

创建项目到爆

     <dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.1</version>
</dependency>
<!-- Shiro uses SLF4J for logging. We'll use the 'simple' binding
in this example app. See http://www.slf4j.org for more info. -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.21</version>
<scope>test</scope>
</dependency>

核心的shiro和log4j依赖

顺便创建log4j文件

创建shiro.ini

import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject; public class TestAuthenticationApp {
//日志输出工具
private static final transient Logger log = LoggerFactory.getLogger(TestAuthenticationApp.class);
public static void main(String[] args) { String username = "zhangsan";
String password = "123456"; log.info("My First Apache Shiro Application");
//1 创建安全管理器的工厂对象
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
//2 使用工厂创建安全管理器
SecurityManager securityManager = factory.getInstance();
//3 把当前的安全管理器绑定到线程
SecurityUtils.setSecurityManager(securityManager);
//4 使用SecurityUtils.getSubject() 得到主体
Subject currentUser = SecurityUtils.getSubject();
//5 封装用户名
AuthenticationToken arg0 = new UsernamePasswordToken(username, password);
currentUser.login(arg0);
System.out.println("认证通过");
}
}

当用户名或者密码不正确时,会抛出相应的异常

使用cry cateh 抛出相应的中文提示即可

最新文章

  1. 负载均衡的几种算法Java实现代码
  2. Bootstrap3生成响应式的特价商品展示布局
  3. 【PRML读书笔记-Chapter1-Introduction】1.6 Information Theory
  4. MySql的Delete、Truncate、Drop分析
  5. java选项及系统属性
  6. Win+PHP+IECapt完整实现网页批量截图并创建缩略图
  7. Android开发中Handler的经典总结--转载至网络
  8. c++參数传递
  9. 腾讯2014在广州站实习生offer经验(TEG-开发背景)
  10. RTC-高效率实现TimerTicker编解码
  11. Chapter 5 Blood Type——4
  12. 错误解决记录-------------验证启动HDFS时遇到的错误
  13. 泊爷带你学go -- 加锁的问题
  14. Shiro中的Rememberme后出现浏览器500错误
  15. 2.4G还是5G?带你选择最正确的路由器
  16. MATLAB插 值 法
  17. 嵌入式Linux学习(一)
  18. RTMP之后,SRT与QUIC
  19. 【oauth2.0】【2】JAVA 客户端模式
  20. MySQL 单条记录长度最大65535

热门文章

  1. Bind+DLZ+MySQL智能DNS的正向解析和反向解析实现方法
  2. 写代码?程序猿?你不能不懂的八大排序算法的Python实现
  3. 4)drf序列化组件 Serializer(偏底层)、ModelSerializer(重点)、ListModelSerializer(辅助群改)
  4. 从零开始通过webhooks实现前端自动化
  5. 「译」JVM是如何使用那些你从未听过的x86魔幻指令实现String.compareTo的
  6. 还在用迭代器处理集合吗?试试Stream,真香
  7. P2201 数列编辑器
  8. 扩欧(exgcd讲解)
  9. spring 事务管理配置
  10. 计算机组成及系统结构-第十章 输入输出(I/O)系统