shiro入门级demo
来源:互联网 发布:unity3d arduino 编辑:程序博客网 时间:2024/05/18 23:15
Demo的目录结构:
1、创建一个Maven Project
在pom.xml添加依赖包,如果你不知道shiro要添加那些包,可以点击http://www.mvnrepository.com/,输入shiro,即可查到相应的包
<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>org.shiro.test</groupId> <artifactId>testShiro</artifactId> <version>0.0.1-SNAPSHOT</version> <name>testShiro</name> <dependencies> <dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>1.2.4</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.19</version></dependency> </dependencies></project>
2、在src/main/resource中,创建一个shiro.ini 配置文件,添加相应的用户,这里是以键值对的方式(账号=密码)
[users]root=123456user=user
3、创建log4j.properties 日志文件
## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.#log4j.rootLogger=INFO, stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m %n# General Apache librarieslog4j.logger.org.apache=WARN# Springlog4j.logger.org.springframework=WARN# Default Shiro logginglog4j.logger.org.apache.shiro=TRACE# Disable verbose logginglog4j.logger.org.apache.shiro.util.ThreadContext=WARNlog4j.logger.org.apache.shiro.cache.ehcache.EhCache=WARN
4.在src/main/java,创建一个测试类
public class TestShiro {public static void main(String[] args) {//读取配置文件,初始化SeurityManager工厂Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");//获取SecurityManager实例SecurityManager sm = factory.getInstance();//把securityManager实例绑定到SecurityUtils中 SecurityUtils.setSecurityManager(sm); //获取当前用户 Subject currentUser = SecurityUtils.getSubject(); //令牌 UsernamePasswordToken token = new UsernamePasswordToken("root", "123456"); try{ currentUser.login(token); System.out.println("身份认证成功!"); }catch (AuthenticationException e) { e.printStackTrace(); System.out.println("身份认证失败!"); } //退出 currentUser.logout();}}
运行结果:在控制台打印出身份认证成功!
当然如果你账号密码错误,就打印出身份认证失败!
0 0
- shiro入门级demo
- shiro入门demo
- Shiro之详细入门附Demo
- shiro权限框架简单入门demo
- shiro demo
- shiro身份验证入门结合shiro的MD5加密技术自定义Realm-简单Demo
- shiro入门级小程序
- shiro入门
- shiro入门
- Shiro入门
- shiro入门
- shiro入门
- Shiro 入门
- Shiro 入门
- Shiro 入门
- Shiro 入门
- shiro入门
- shiro入门
- oc 内存管理 -堆栈操作
- 01背包和完全背包问题
- Linux中的文件描述符与打开文件之间的关系
- Matlab中的常用快捷键
- 【SDUT2414】An interesting game——最小费用
- shiro入门级demo
- Android_实现页面跳转(intent的应用)及进阶
- (译)在Android中使用并发提高应用性能
- SVN服务器搭建和使用(三)
- 【游戏】猜数字
- 数据结构课设 修理牧场 (哈夫曼树基础)
- java双缓冲
- 【c++】冒泡法排序
- 新建虚拟机