当前位置: 首页 > news >正文

An Introduction to Shiro (formerly JSecurity) – A Beginner’s Tutorial Part 1

An Introduction to Shiro (formerly JSecurity) – A Beginner’s Tutorial Part 1

Introduction

NOTE: Updated in January 2011.

I recently took over a project that used Apache Shiro for web application security. Shiro was previously known as JSecurity (and briefly also called Ki). Not having used Shiro before, I needed to do some research and learn the basics. This blog entry is designed to assist other developers in applying Shiro to a web application. Please understand that I'm just a beginner in using Shiro. Any mistakes in my explanations or code are my responsibility. If you do notice that something is wrong, please post a comment.

From the Shiro web site:

Apache Shiro is a powerful and easy-to-use Java security framework that performs authentication, authorization, cryptography, and session management.
(http://shiro.apache.org - accessed January 2011)

In March 2009, JSecurity's developers changed the JSecurity name to Ki and then to Shiro for various reasons. So you may hear/read about reference to all three names. According to the Apache Software Foundation: "Ki entered the Apache Incubator in June, 2008" (http://incubator.apache.org/projects/ki.html, accessed on April 5, 2009). In September 2010, Shiro became a Apache Top Level Project. The current release (January 2011) of Shiro is 1.1.0.

Shiro provides powerful capabilities with minimal setup--if you're able to use Shiro's configuration defaults in your project. But even if you're not able to use the defaults, you can provide your configuration to Shiro or you can extend one or more of the Shiro classes to customize how it works.

Tutorial Description and Setup

Part 1 of this tutorial starts with a basic web application that has no security. You can download an archived Eclipse dynamic web project here. The project uses Maven to manage dependencies. If you're not familiar with Maven, see the references below. Eclipse has an excellent Maven plugin (see: http://www.sonatype.com/books/m2eclipse-book/reference/index.html ). You should be able to import the archived project directly into Eclipse. The project's name is nosecurity. If you don't use Eclipse, just unzip the downloaded file (nosecurity_mvn.zip) and copy the source code to your own Java IDE.

You must also download the separate Apache Derby database as I'm going to show you how to use Shiro when your web application is storing usernames and passwords in a database. Note that Shiro comes with support for many other kinds of storage mechanisms. Unzip this download into c:/derby on your computer.

To use the Derby database with Tomcat and connection pooling you'll need to download the Derby lib files at http://db.apache.org/derby/releases/release-10.4.2.0.html. After unzipping the download copy the derby.jar to your Tomcat's /lib folder. The nosecurity web application uses connection pooling to manage connections to the Derby database. For Tomcat to find the correct driver class to connect to Derby the derby jar file must be in Tomcat's lib directory.

The no security web application includes an index.jsp page and a separate folder (named secure) that has two web pages (users.jsp and index.jsp). The pages under the secure folder are supposed to be available only to logged in (authenticated) users. Under the src/main/java folder are packages with the data access, model, and Servlet classes.

Testing The Database Connection

After you've got everything setup you can test the application's connection to Derby by running the TestConnectionFactory class that is in the dao package. This class is just a standard Java class with a main method. In the main method, it uses the ConnectionFactory class to get a connection to the Derby database, then displays some information about Derby, and shows the results of querying the users table. The users table is where the project is storing the usernames and passwords for the tutorial. Note if you did not unzip the Derby database (securityDB) into c:/derby you will need to change the parth to securityDB in class ConnectionFactory, method getConnection, in the context.xml file under the META-INF folder, and in jetty-env.xml in WEB-INF folder.

Running The Tutorial

After confirming that the project can connect to the Derby database, you can build and deploy the web application to a Java Servlet container and web server such as Tomcat or Jetty. The tutorial was tested on Tomcat version 6 and Jetty. Remember if you're using Eclipse, you'll need the Maven Eclipse plugin so that all the dependent jars will be provided to the project. See the reference below for how to get the Maven Eclipse plugin.

You can also use the Maven jetty plugin (see reference below for how to install Maven if you've don't already have Maven) to run the web application if you're not using Eclipse and Tomcat. Just open a command window and navigate to where you unzipped the nosecurity_mvn.zip download. Make sure you're in the nosecurity directory. Then do the following (in this example I unzipped nosecurity_mvn.zip to c:\jsecurity_examples):

c:\jsecurity_examples\nosecurity\mvn clean

c:\jsecurity_examples\nosecurity\mvn jetty:run

Once you see [INFO] Started Jetty Server in the command window, open your web browser and go to this URL: http://localhost:8080/nosecurity/. You should see the contents of the index.jsp. To stop the Jetty server type control-c in the command window.

Since this web application has no security you should be able to click on all the links and all the web pages should display, including the secure/users.jsp which displays the records from the users table that is the Derby database.

What's Next?

I realize this seems like a lot of setup just to get a simple web application to run. But I want to ensure that you can run this web application that uses a Derby database to store user information. In future tutorials, I'll be adding to this web application features from Shiro to enable security. So it's important that you get the basic web application working correctly in your development environment so that if you run into problems in the future tutorials you'll be able to more easily determine the cause.

In part 2 of this tutorial, I'll add basic security using Shiro to the web application to prevent users who have not logged in from viewing the pages in the secure folder.

References

  1. No Security Example Application, http://www.brucephillips.name/jsecurity_examples/nosecurity_mvn.zip
  2. Apache Shiro http://shiro.apache.org/
  3. Apache Shiro API, http://shiro.apache.org/static/current/apidocs/
  4. Apache Shiro Mailing Lists, http://shiro.apache.org/mailing-lists.html
  5. Apache Derby, http://db.apache.org/derby/
  6. Apache Tomcat, http://tomcat.apache.org/
  7. Jetty, http://jetty.mortbay.org/jetty5/index.html
  8. Maven: The Definitive Guide, http://www.sonatype.com/books/maven-book/reference/public-book.html
  9. Developing with Eclipse and Maven, http://www.sonatype.com/books/m2eclipse-book/reference/index.html

转载于:https://www.cnblogs.com/Earl/archive/2011/08/16/2140092.html

相关文章:

  • 去网络视频广告方法——虽过时,但效果依然很好(亲测)
  • 评国内三大B2C网站首页的信息架构
  • struts2环境搭建及详细示例
  • 小小的发现
  • 同步处理(LockContext),期待大家的意见
  • 高朋网13个地方分站整体被撤
  • 搜集点shell资料
  • VMware vCloud Director Administration Guide
  • Android应用程序在新的进程中启动新的Activity的方法和过程分析
  • SEO基本概念入门
  • Linux系统下启动MySQL的命令及相关知识
  • Forefront Client Security部署及配置
  • 一起谈.NET技术,走向ASP.NET架构设计——第七章:阶段总结,实践篇(中篇)...
  • android每日一问1【2011-09-06】
  • 深度剖析:远程控制软件如何实现隐性监控
  • Android 初级面试者拾遗(前台界面篇)之 Activity 和 Fragment
  • C++类中的特殊成员函数
  • NLPIR语义挖掘平台推动行业大数据应用服务
  • Python学习之路13-记分
  • React Transition Group -- Transition 组件
  • SegmentFault 2015 Top Rank
  • Swoft 源码剖析 - 代码自动更新机制
  • 看完九篇字体系列的文章,你还觉得我是在说字体?
  • 聊聊sentinel的DegradeSlot
  • 前端设计模式
  • 微信开源mars源码分析1—上层samples分析
  • ​ 无限可能性的探索:Amazon Lightsail轻量应用服务器引领数字化时代创新发展
  • # MySQL server 层和存储引擎层是怎么交互数据的?
  • #if和#ifdef区别
  • #我与Java虚拟机的故事#连载07:我放弃了对JVM的进一步学习
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • (2022 CVPR) Unbiased Teacher v2
  • (5)STL算法之复制
  • (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
  • (java版)排序算法----【冒泡,选择,插入,希尔,快速排序,归并排序,基数排序】超详细~~
  • (pojstep1.1.1)poj 1298(直叙式模拟)
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (十六)Flask之蓝图
  • (数据结构)顺序表的定义
  • (译) 函数式 JS #1:简介
  • (转载)Linux 多线程条件变量同步
  • .net core 控制台应用程序读取配置文件app.config
  • .Net Remoting常用部署结构
  • .net 中viewstate的原理和使用
  • .Net中wcf服务生成及调用
  • .NET中两种OCR方式对比
  • /etc/X11/xorg.conf 文件被误改后进不了图形化界面
  • @Autowired和@Resource的区别
  • [ C++ ] STL---string类的使用指南
  • [ vulhub漏洞复现篇 ] JBOSS AS 5.x/6.x反序列化远程代码执行漏洞CVE-2017-12149
  • [2669]2-2 Time类的定义
  • [android] 看博客学习hashCode()和equals()
  • [Android]使用Retrofit进行网络请求
  • [Angular 基础] - 数据绑定(databinding)
  • [CodeForces-759D]Bacterial Melee