Shiro学习总结(二)--Shiro的入门小例子
2017-03-05 23:17
423 查看
前文提到shiro的一些概念,接下来开始shiro的实际操作:
Shiro的入门小例子:
第一步:配置jar包,这里采用maven的方式,故pom.xml的配置文件如下:
第二步:暂时没有连接数据库,故采用静态数据,需要配置shiro.ini的文件模拟静态数据【user】表示用户,前面表示用户名
后面表示用户密码。
第三步:创建log4j的属性文件,这个日志管理。
Shiro的入门小例子:
第一步:配置jar包,这里采用maven的方式,故pom.xml的配置文件如下:
<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.12</version>
</dependency>
第二步:暂时没有连接数据库,故采用静态数据,需要配置shiro.ini的文件模拟静态数据【user】表示用户,前面表示用户名
后面表示用户密码。
[users]
taojian=123456
jack=123
第三步:创建log4j的属性文件,这个日志管理。
#
# 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[/code]## 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
第四步:编写关键的java代码;package com.shiro.hello;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.AuthenticationException;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;public class HelleWorld {public static void main(String[] args) {// 读取配置文件,初始化SecurityManager工厂Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:shiro.ini");// 获取securityManager实例SecurityManager securityManager=factory.getInstance();// 把securityManager实例绑定到SecurityUtilsSecurityUtils.setSecurityManager(securityManager);// 得到当前执行的用户Subject currentUser=SecurityUtils.getSubject();// 创建token令牌,用户名/密码UsernamePasswordToken token=new UsernamePasswordToken("taojian", "123456");try{// 身份认证currentUser.login(token);System.out.println("身份认证成功!");}catch(AuthenticationException e){e.printStackTrace();System.out.println("身份认证失败!");}// 退出currentUser.logout();}}
相关文章推荐
- Hibernate入门之自己写的小例子的总结
- 挑战30天C++入门极限-c/c++中指针学习的两个绝好例子
- Coolite官方例子学习笔记五:前四篇的总结心得
- Android学习 - (1) 入门例子
- Android 简单例子以及入门学习资料链接
- Hibernate入门之自己写的小例子的总结 zz
- Hadoop 学习总结之三:Map-Reduce入门(转载)
- jQuery学习入门总结之css()和addClass()的不同
- Spring学习笔记1--简介及入门例子
- Entity Framework 学习总结之六:EntitySQL 介绍入门
- asp.net 2.0 入门学习总结
- SAP ABAP/4学习--学习使用OO在ABAP中.简单入门概念.用个例子来说明
- Java 正则表达式学习总结和一些小例子
- jQuery入门学习之实现滑动效果:slideUp()和slideDown()小总结
- 我学习rmi的入门总结
- 通过例子学习Lua(5) ---- Lua与C交互入门 (转)
- 学习委托与事件的总结,附上一个例子
- Javascript入门学习第九篇 Javascript DOM 总结第1/2页
- 通过例子学习Lua(5) ---- Lua与C交互入门
- Entity Framework 学习总结之六:EntitySQL 介绍入门