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
原创粉丝点击