Shiro

Shiro

一.Shiro 简介

Shiro:
	1.Apache Shiro 是 Java 的一个安全(权限)框架。
	2.Shiro 可以非常容易的开发出足够好的应用,其不仅可以用在 JavaSE 环境,也可以用在 JavaEE 环境。
	3.Shiro 可以完成:认证、授权、加密、会话管理、与 Web 集成、缓存等。
	4.下载: http://shiro.apache.org/
	

功能简介:
	Authentication    Authorization         Primary Concerns
	Session Management  Cryptography        Primary Concerns
	  
	Web Support        Caching                 Supporting Features
	Concurrency Testing  Run As Remember Me    Supporting Features
	
	Authentication : 身份认证/登录,验证用户是不是拥有相应的身份;
	Authorization : 授权,即权限验证,验证某个已认证的用户是否拥有某个权限;即判断用户是否能进行什么操作,
					如:验证某个用户是否拥有某个角色。或者细粒度的验证某个用户对某个资源是否具有某个权限。
	Session Manager : 会话管理,即用户登录后就是一次会话,在没有退出之前,它的所有信息都在会话中;
					会话可以是普通 JavaSE 环境,也可以是 Web 环境的。
	Cryptography : 加密,保护数据的安全性,如密码加密存储到数据库,而不是明文存储;
	Web Support : Web 支持,可以非常容易的集成到 Web 环境。
	Caching : 缓存,比如用户登录后,其用户信息、拥有的角色/权限不必每次去查,这样可以提高效率。
	Concurrency : Shiro 支持多线程应用的并发特征,即如在一个线程中开启另一个线程,能把权限自动传播过去;
	Testing : 提供测试支持;
	Run As : 允许一个用户假装为另一个用户(如果他们允许)的身份访问;
	Remember Me : 记住我,即一次登录后,下次再来的话不用登录了。
	

Shiro 架构(Shiro 外部来看)
 	从外部来看 Shiro,即从应用程序角度来观察如何使用 Shiro 完成工作:
 	Application Code ——> Subject(the current user) ——> 
 		Shiro SecurityManager(manages all Subjects) ——> Realm(access your security data)
        
   Subject : 应用代码直接交互的对象就是 Subject,也就是说 Shiro 的对外 API 核心就是 Subject。
   			Subject 代表了当前"用户",这个用户不一定是一个具体的人,与当前应用交互的任何东西都是
   			Subject, 如网络爬虫,机器人等;与 Subject 的所有交互都会委托给 SecurityManager;
   SecurityManager : 安全管理器;即所有与安全有关的操作都会与 SecurityManager 交互;且其管理着所有
   			 Subject;可以看出它是 Shiro 的核心,它负责与 Shiro 的其他组件进行交互,它相当于 SpringMVC 中
   			 DispatcherServlet 的角色。
   Realm : Shiro 从 Realm 获取安全数据(如用户、角色、权限),就是说 SecurityManager 要验证用户身份,那么它需要
   			从 Realm 获取相应的用户进行比较以确定用户身份是否合法;也需要从 Realm 得到用户相应的角色/权限进行
   			验证用户是否能进行操作;可以把 Realm 看成 DataSource.
                

Shiro 架构(内部)
    Subject : 任何可以与应用交互的"用户";
	SecurityManager : 相当于 SpringMVC 中的 DispatcherServlet;是 Shiro 的心脏;所有具体的交互都通过 SecutiryManager
			进行控制;它管理着所有 Subject、且负责进行认证、授权、会话即缓存的管理。
	Authenticator : 负责 Subject 认证,是一个扩展点,可以自定义实现;可以使用认证策略(Authentication Strategy),即什么情况下
			算用户认证通过了。
	Authorizer : 授权器、即访问控制器,用来决定主体是否有权限进行相应的操作;即控制着用户能访问应用中的哪些功能;
	Realm : 可以有 1 个或多个 Realm,可以认为是安全实体数据源,即用于获取安全实体的;可以是 JDBC 实现,也可以是内存实现等等;由用户
			提供;所以一般在应用中都需要实现自己的 Realm.
    SessionManager : 管理 Session 生命周期的组件;而 Shiro 并不仅仅可以用在 Web 环境,也可以用在如普通的 JavaSE 环境。
    CacheManager : 缓存控制器,来管理如用户、角色、权限等的缓存的;因为这些数据基本上很少改变,放到缓存中后可以提高访问的性能。
    Cryptography : 密码模块,Shiro 提高了一些常见的加密组件用于如密码加密/解密。

二.Shiro

1.搭建开发环境

HelloWorld.java

加入如下 jar 包:
	- shiro-all-1.3.2.jar
	- log4j-1.2.15.jar
	- slf4j-api-1.6.1.jar
	- slf4j-log4j12-1.6.1.jar
package com.laughing.shiro.helloworld;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Quickstart {

	 private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class);


	    public static void main(String[] args) {

	        // The easiest way to create a Shiro SecurityManager with configured
	        // realms, users, roles and permissions is to use the simple INI config.
	        // We'll do that by using a factory that can ingest a .ini file and
	        // return a SecurityManager instance:

	        // Use the shiro.ini file at the root of the classpath
	        // (file: and url: prefixes load from files and urls respectively):
	        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
	        SecurityManager securityManager = factory.getInstance();

	        // for this simple example quickstart, make the SecurityManager
	        // accessible as a JVM singleton.  Most applications wouldn't do this
	        // and instead rely on their container configuration or web.xml for
	        // webapps.  That is outside the scope of this simple quickstart, so
	        // we'll just do the bare minimum so you can continue to get a feel
	        // for things.
	        SecurityUtils.setSecurityManager(securityManager);

	        // Now that a simple Shiro environment is set up, let's see what you can do:

	        // get the currently executing user:
	        
	        
	        // 获取当前的 Subject. 调用 SecurityUtils.getSubject();
	        Subject currentUser = SecurityUtils.getSubject();

	        // Do some stuff with a Session (no need for a web or EJB container!!!)
	        
	        
	        // 测试使用 Session 
	        // 获取 Session: Subject#getSession()
	        Session session = currentUser.getSession();
	        session.setAttribute("someKey", "aValue");
	        String value = (String) session.getAttribute("someKey");
	        if (value.equals("aValue")) {
	            log.info("---> Retrieved the correct value! [" + value + "]");
	        }

	        // let's login the current user so we can check against roles and permissions:
	        
	        
	        // 测试当前的用户是否已经被认证. 即是否已经登录. 
	        // 调动 Subject 的 isAuthenticated() 
	        if (!currentUser.isAuthenticated()) {
	        	// 把用户名和密码封装为 UsernamePasswordToken 对象
	            UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
	            // rememberme
	            token.setRememberMe(true);
	            try {
	            	// 执行登录. 
	                currentUser.login(token);
	            } 
	            // 若没有指定的账户, 则 shiro 将会抛出 UnknownAccountException 异常. 
	            catch (UnknownAccountException uae) {
	                log.info("----> There is no user with username of " + token.getPrincipal());
	                return; 
	            } 
	            // 若账户存在, 但密码不匹配, 则 shiro 会抛出 IncorrectCredentialsException 异常。 
	            catch (IncorrectCredentialsException ice) {
	                log.info("----> Password for account " + token.getPrincipal() + " was incorrect!");
	                return; 
	            } 
	            // 用户被锁定的异常 LockedAccountException
	            catch (LockedAccountException lae) {
	                log.info("The account for username " + token.getPrincipal() + " is locked.  " +
	                        "Please contact your administrator to unlock it.");
	            }
	            // ... catch more exceptions here (maybe custom ones specific to your application?
	            // 所有认证时异常的父类. 
	            catch (AuthenticationException ae) {
	                //unexpected condition?  error?
	            }
	        }

	        //say who they are:
	        //print their identifying principal (in this case, a username):
	        log.info("----> User [" + currentUser.getPrincipal() + "] logged in successfully.");

	        //test a role:
	        // 测试是否有某一个角色. 调用 Subject 的 hasRole 方法. 
	        if (currentUser.hasRole("schwartz")) {
	            log.info("----> May the Schwartz be with you!");
	        } else {
	            log.info("----> Hello, mere mortal.");
	            return; 
	        }

	        //test a typed permission (not instance-level)
	        // 测试用户是否具备某一个行为. 调用 Subject 的 isPermitted() 方法。 
	        if (currentUser.isPermitted("lightsaber:weild")) {
	            log.info("----> You may use a lightsaber ring.  Use it wisely.");
	        } else {
	            log.info("Sorry, lightsaber rings are for schwartz masters only.");
	        }

	        //a (very powerful) Instance Level permission:
	        // 测试用户是否具备某一个行为. 
	        if (currentUser.isPermitted("user:delete:zhangsan")) {
	            log.info("----> You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +
	                    "Here are the keys - have fun!");
	        } else {
	            log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
	        }

	        //all done - log out!
	        // 执行登出. 调用 Subject 的 Logout() 方法. 
	        System.out.println("---->" + currentUser.isAuthenticated());
	        
	        currentUser.logout();
	        
	        System.out.println("---->" + currentUser.isAuthenticated());

	        System.exit(0);
	    }
}

shiro.ini

#
# 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.
#
# =============================================================================
# Quickstart INI Realm configuration
#
# For those that might not understand the references in this file, the
# definitions are all based on the classic Mel Brooks' film "Spaceballs". ;)
# =============================================================================

# -----------------------------------------------------------------------------
# Users and their assigned roles
#
# Each line conforms to the format defined in the
# org.apache.shiro.realm.text.TextConfigurationRealm#setUserDefinitions JavaDoc
# -----------------------------------------------------------------------------
[users]
# user 'root' with password 'secret' and the 'admin' role
root = secret, admin
# user 'guest' with the password 'guest' and the 'guest' role
guest = guest, guest
# user 'presidentskroob' with password '12345' ("That's the same combination on
# my luggage!!!" ;)), and role 'president'
presidentskroob = 12345, president
# user 'darkhelmet' with password 'ludicrousspeed' and roles 'darklord' and 'schwartz'
darkhelmet = ludicrousspeed, darklord, schwartz
# user 'lonestarr' with password 'vespa' and roles 'goodguy' and 'schwartz'
lonestarr = vespa, goodguy, schwartz

# -----------------------------------------------------------------------------
# Roles with assigned permissions
# 
# Each line conforms to the format defined in the
# org.apache.shiro.realm.text.TextConfigurationRealm#setRoleDefinitions JavaDoc
# -----------------------------------------------------------------------------
[roles]
# 'admin' role has all permissions, indicated by the wildcard '*'
admin = *
# The 'schwartz' role can do anything (*) with any lightsaber:
schwartz = lightsaber:*
# The 'goodguy' role is allowed to 'delete' (action) the user (type) with
# license plate 'zhangsan' (instance specific id)
goodguy = user:delete:zhangsan

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, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m %n

# General Apache libraries
log4j.logger.org.apache=WARN

# Spring
log4j.logger.org.springframework=WARN

# Default Shiro logging
log4j.logger.org.apache.shiro=TRACE

# Disable verbose logging
log4j.logger.org.apache.shiro.util.ThreadContext=WARN
log4j.logger.org.apache.shiro.cache.ehcache.EhCache=WARN

2.集成 Spring

1.加入 Spring 和 Shiro 的 jar 包
2.配置 Spring 及 SpringMVC
3.参照 1.3.2\shiro-root-1.3.2-source-release\shiro-root-1.3.2\samples\spirng 配置 web.xml 文件和
	Spring 的配置文件

3.与 Web 集成

1.Shiro 提供了与 Web 集成的支持,其通过 一个 ShiroFilter 入口来拦截需要安全控制的URL,然后进行相应的控制。
2.ShiroFilter 类似于如 SpringMVC 这种 Web 框架的前端控制器,是安全控制的入口点,其负责读取配置(如 ini 配置文件),
	然后判断 URL 是否需要登录/权限等工作。


3.1.ShiroFilter 工作原理

ShiroFilter 的工作原理:

	浏览器 ————> ShiroFilter(filterChainDefinitions/loginUrl) ————> 经过认证或不被拦截的页面/没有经过认证

3.2.ShiroFilter

DelegatingFilterProxy 作用是自动到 Spring 容器查找名字为 ShiroFilter(filter-name) 的 bean 并把所有 Filter 的操作委托给它。
<filter>
	<filter-name>shiroFilterfilter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
    <init-param>
    	<param-name>targetFilterLifecycleparam-name>
        <param-value>trueparam-value>
    init-param>
filter>
<filter-mapping>
	<filter-name>shiroFilterfilter-name>
    <url-pattern>/*url-pattern>
filter-mapping>

部分细节:
1.[urls] 部分的配置,其格式是: "url=拦截器[参数],拦截器[参数]";
2.如果当前请求的 url 匹配 [urls] 部分的某个 url 模式,将会执行其配置的拦截器。
3.anon (anonymous) 拦截器表示匿名访问(即不需要登录即可访问)
4.authc (authentication) 拦截器表示需要身份认证通过后才能访问。

3.3.URL 匹配模式

URL 匹配模式:
	1.URL 模式使用 Ant 风格模式
	2.Ant 路径通配符支持 ?***,注意通配符匹配不包括目录分隔符"/" :
		? : 匹配一个字符,/admin?将匹配/admin1, 但不匹配 /admin 或 /admin/;
		* : 匹配零个或多个字符串,/admin 将匹配 /admin、/admin123,但不匹配 /admin/1;
		** : 匹配路径中的零个或多个路径,/admin/** 将匹配 /admin/a 或 /admin/a/b

3.4. URL 匹配顺序

URL 匹配顺序:
	1.URL 权限采取第一次匹配优先的方式,即从头开始使用第一个匹配的 URL 模式对应的拦截器链。
		如:
			/bb/**=filter1
			/bb/aa=filter2
			/**=filter3
			如果请求的 url 是 "/bb/aa", 因为按照声明顺序进行匹配,那么将使用 filter1 进行拦截。

4.认证

身份验证:
	1.身份认证:一般需要提供身份 ID 等一些标识信息来表明登录者的身份,如提供 email, 用户名/密码来证明。
	2.在 shiro 中,用户需要提供 principals(身份)credentials(证明)给 shiro,从而应用能验证用户身份。
	3.principals : 身份,即主体的标识属性,可以是任何属性,如用户名、邮箱等,唯一即可。一个主体可以有多个 principals,但
			只有一个 Primary principals,一般是用户名/邮箱/手机号。
	4.credentials : 证明/凭证,即只有主体知道的安全值,如密码/数字证书等。
	5.最常见的 principals 和 credentials 组合就是用户名/密码了。

AuthenticationException 
	1.如果身份验证失败请捕获 AuthenticationException 或其子类
	2.最好使用如 "用户名/密码错误" 而不是 "用户名错误"/"密码错误",防止一些恶意用户非法扫描账号库。

身份验证基本流程:
	1.收集用户身份/凭证,即如用户名/登录。
	2.调用 Subject.login 进行登录,如果失败将得到相应的 AuthenticationException 异常,根据异常提示用户的错误信息,否则登录成功。
	3.创建自定义的 Relam 类,继承 org.apache.shiro.realm.AuthorizingRealm 类,实现 doGetAuthenticationInfo() 方法。
	
	
		//1.获取当前的 Subject
		Subject currentUser = SecurityUtils.getSubject();
		
		//2.测试当前的用户是否已经被认证。即是否已经登录。
		if(!currentUser.isAuthenticated()) {
			//3.若没有被认证,则把用户名和密码封装为 UsernamePasswordToken 对象
			UsernamePasswordToken token = new UsernamePasswordToken(username, password);
			//remember me
			token.setRememberMe(true);
			try {
				System.out.println("1." + token.hashCode());
				//执行登录:调用 Subject 的 login(AuthenticationToken) 方法
				currentUser.login(token);
			} catch (AuthenticationException e) {
				System.out.println("登录失败:" +e.getMessage());
			}
		}



身份认证流程:
	1.首先调用 Subject.login(token) 进行登录,其会自动委托给 SecurityManager
	2.SecurityManager 负责真正的身份验证逻辑;它会委托给 Authenticator 进行身份验证。
	3.Authenticator 才是真正的身份验证者,Shiro API 中核心的身份认证入口点,此处可以自定义插入自己的实现。
	4.Authenticator 可能会委托给相应的 AuthenticationStrategy 进行多 Realm 身份验证,默认 ModularRealmAuthenticator 会调用
				AuthenticationStrategy 进行多 Realm 身份验证;
	5.Authenticator 会把相应的 token 传入 Realm, 从 Realm 获取身份验证信息,如果没有返回/抛出异常表示身份验证失败了。此处可以配置
				多个 Realm,将按照相应的顺序及策略进行访问。

认证:

	1.获取当前的 Subject.调用 SecurityUtils.getSubject();
	2.测试当前的用户是否已经被认证。即是否已经登录。调用 Subject 的 isAuthenticated() 方法;
	3.若没有被认证,则把用户名和密码封装为 UsernamePasswordToken 对象:
		3.1.创建一个表单页面
		3.2.把请求提交到 SpringMVC de Handler
		3.3.获取用户名和密码。
	4.执行登录:调用 Subject 的 login(AuthenticationToken) 方法。
	5.自定义 Realm 的方法,从数据库中获取对应的记录,返回给 Shiro.
        5.1.实际上需要继承 org.apache.shiro.realm.AuthenticationRealm 类。
        5.2.实现 doGetAuthenticationInfo(AutheneicationToken) 方法。
    6.由 shiro 完成对密码的比对。
    
Realm:
	Realm : Shiro 从 Realm 获取安全数据(如用户、角色、权限),即 SecurityManager 要验证用户身份,那么它需要从 Realm 获取相应的用户进行
			比较以确定用户身份是否合法;也需要从 Realm 得到用户相应的角色/权限进行验证用户是否能进行操作
	Realm 接口如下:
		String getName() :  //返回一个唯一的 Realm 名字
		boolean supports(AuthenticationToken token): 判断此 boolean 是否支持此 Token
		AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException:
				根据 Token 获取认证信息
	一般继承 AuthorizingRealm(授权)即可;其继承了AuthenticatingRealm(即身份验证),而且也间接继承了CachingRealm(带有缓存实现)。
	

Authenticator
	1.Authenticator 的职责是验证用户账号,是 Shiro API 中身份验证核心的入口点:如果验证成功,将返回 AuthenticationInfo 验证信息;
			此信息中包含了身份及凭证;如果验证失败将抛出相应的 AuthenticationException 异常。
	2.SecurityManager 接口继承了 Authenticator,另外还有一个 ModularRealmAuthenticator 实现,其委托给多个 Realm 进行验证,验证
			规则通过 AuthenticationStrategy 接口指定
			
			
AuthenticationStrategy:
	1.AuthenticationStrategy 接口的默认实现:
		1.FirstSuccessfulStrategy : 只要有一个 Realm 验证成功即可,只返回第一个 Realm 身份验证成功的认证信息,其他的忽略。
		2.AtLeastOntSuccessfulStrategy : 只要有一个 Realm 验证成功即可,和 FirstSuccessfulStrategy 不同,将返回所有 Realm 身份验证
				成功的认证信息;
		3.AllSuccessfulStrategy : 所有 Realm 验证成功才算成功,且返回所有 Realm 身份验证成功的认证信息,如果有一个失败就失败了。
		4.ModularRealmAutheneicator 默认是 AtLeastOneSuccessfulStrategy 策略。


5.密码比对

密码比对:

	通过 AuthenticatingRealm 的 credentialsMatcher 属性来进行密码的比对。

6.密码的 MD5 加密

替换当前 Realm 的 credentialsMatcher 属性。直接使用 HashedCredentialsMatcher 对象,并设置加密算法即可。

<bean id="jdbcRealm" class="com.laughing.shiro.realms.ShiroRealm">
    <property name="credentialsMatcher">
    	<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
    		<property name="hashAlgorithmName" value="MD5"></property>
    		<property name="hashIterations" value="1024"></property>
    	</bean>
    </property>
</bean>


MD5 盐值加密:用户密码相同,但是数据库中存储的加密字符串不同。
如何实现:
	1.在 doGetAuthenticationInfo 方法返回值创建 SimpleAuthenticationInfo 对象的创建,
		需要使用 new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName) 构造器。
	2.使用 ByteSource.Util.bytes() 来计算盐值。
	3.盐值需要唯一:一般使用随机字符串或 user id
	4.使用 new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations) 来计算盐值加密后的密码的值。




//6.根据用户的情况,来构建 AuthenticationInfo 对象并返回.通常使用的实现类为:SimpleAuthenticationInfo
//以下信息是从数据库中获取的。
//1.principal : 认证的实体信息。可以是 username,也可以是数据表对应的用户的实体类对象。
Object principal = username;
//2.credentials : 密码
Object credentials = null;
if ("admin".equals(username)) {
	credentials = "038bdaf98f2037b31f1e75b5b4c9b26e";
} else if("user".equals(username)){
	credentials = "098d2c478e9c11555ce2823231e02ec1";
}
//3.realmName : 当前 realm 对象的 name.调用父类的 getName() 方法即可
String realmName = getName();
//4.盐值。
ByteSource credentialsSalt = ByteSource.Util.bytes(username);
SimpleAuthenticationInfo info = null;
info = new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName);

public static void main(String[] args) {
	String hashAlgorithmName = "MD5";
	Object credentials = "123456";
	Object salt = ByteSource.Util.bytes("user");
	int hashIterations = 1024;
		
	Object result = new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
	System.out.println(result);
}

7.多 Realm 验证

身份认证流程:
	1.首先调用 Subject.login(token) 进行登录,其会自动委托给 SecurityManager
	2.SecurityManager 负责真正的身份验证逻辑;它会委托给 Authenticator 进行身份验证。
	3.Authenticator 才是真正的身份验证者,Shiro API 中核心的身份认证入口点,此处可以自定义插入自己的实现。
	4.Authenticator 可能会委托给相应的 AuthenticationStrategy 进行多 Realm 身份验证,默认 ModularRealmAuthenticator 会调用
				AuthenticationStrategy 进行多 Realm 身份验证;
	5.Authenticator 会把相应的 token 传入 Realm, 从 Realm 获取身份验证信息,如果没有返回/抛出异常表示身份验证失败了。此处可以配置
				多个 Realm,将按照相应的顺序及策略进行访问。

认证:

	1.获取当前的 Subject.调用 SecurityUtils.getSubject();
	2.测试当前的用户是否已经被认证。即是否已经登录。调用 Subject 的 isAuthenticated() 方法;
	3.若没有被认证,则把用户名和密码封装为 UsernamePasswordToken 对象:
		3.1.创建一个表单页面
		3.2.把请求提交到 SpringMVC de Handler
		3.3.获取用户名和密码。
	4.执行登录:调用 Subject 的 login(AuthenticationToken) 方法。
	5.自定义 Realm 的方法,从数据库中获取对应的记录,返回给 Shiro.
        5.1.实际上需要继承 org.apache.shiro.realm.AuthenticationRealm 类。
        5.2.实现 doGetAuthenticationInfo(AutheneicationToken) 方法。
    6.由 shiro 完成对密码的比对。
    
Realm:
	Realm : Shiro 从 Realm 获取安全数据(如用户、角色、权限),即 SecurityManager 要验证用户身份,那么它需要从 Realm 获取相应的用户进行
			比较以确定用户身份是否合法;也需要从 Realm 得到用户相应的角色/权限进行验证用户是否能进行操作
	Realm 接口如下:
		String getName() :  //返回一个唯一的 Realm 名字
		boolean supports(AuthenticationToken token): 判断此 boolean 是否支持此 Token
		AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException:
				根据 Token 获取认证信息
	一般继承 AuthorizingRealm(授权)即可;其继承了AuthenticatingRealm(即身份验证),而且也间接继承了CachingRealm(带有缓存实现)。
	

Authenticator
	1.Authenticator 的职责是验证用户账号,是 Shiro API 中身份验证核心的入口点:如果验证成功,将返回 AuthenticationInfo 验证信息;
			此信息中包含了身份及凭证;如果验证失败将抛出相应的 AuthenticationException 异常。
	2.SecurityManager 接口继承了 Authenticator,另外还有一个 ModularRealmAuthenticator 实现,其委托给多个 Realm 进行验证,验证
			规则通过 AuthenticationStrategy 接口指定
			
			
AuthenticationStrategy:
	1.AuthenticationStrategy 接口的默认实现:
		1.FirstSuccessfulStrategy : 只要有一个 Realm 验证成功即可,只返回第一个 Realm 身份验证成功的认证信息,其他的忽略。
		2.AtLeastOntSuccessfulStrategy : 只要有一个 Realm 验证成功即可,和 FirstSuccessfulStrategy 不同,将返回所有 Realm 身份验证
				成功的认证信息;
		3.AllSuccessfulStrategy : 所有 Realm 验证成功才算成功,且返回所有 Realm 身份验证成功的认证信息,如果有一个失败就失败了。
		4.ModularRealmAutheneicator 默认是 AtLeastOneSuccessfulStrategy 策略。
1.MD5 

<bean id="jdbcRealm" class="com.laughing.shiro.realms.ShiroRealm">
    <property name="credentialsMatcher">
    	<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
    		<property name="hashAlgorithmName" value="MD5">property>
    		<property name="hashIterations" value="1024">property>
    	bean>
    property>
bean>
package com.laughing.shiro.realms;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;

public class ShiroRealm extends AuthenticatingRealm{

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		System.out.println("doGetAuthenticationInfo: " +token.hashCode());
		//1.把 AuthenticationToken 转换为 UsernamePasswordToken 
		UsernamePasswordToken upToken = (UsernamePasswordToken)token;
		//2.从 UsernamePasswordToken 中来获取 username
		String username = upToken.getUsername();
		//3.调用数据库的方法,从数据库中查询 username 对应的用户记录。
		System.out.println("从数据库中获取 username: " + username + " 所对应的用户信息.");
		
		//4.若用户不存在,则可以抛出 AuthenticationException 异常
		if ("unknown".equals(username)) {
			throw new UnknownAccountException("用户不存在");
		}
		
		//5.根据用户信息的情况,决定是否需要抛出其他的 AuthenticationException 异常
		if ("monster".equals(username)) {
			throw new LockedAccountException("用户被锁定");
		}
		//6.根据用户的情况,来构建 AuthenticationInfo 对象并返回.通常使用的实现类为:SimpleAuthenticationInfo
		//以下信息是从数据库中获取的。
		//1.principal : 认证的实体信息。可以是 username,也可以是数据表对应的用户的实体类对象。
		Object principal = username;
		//2.credentials : 密码
		Object credentials = null;
		if ("admin".equals(username)) {
			credentials = "038bdaf98f2037b31f1e75b5b4c9b26e";
		} else if("user".equals(username)){
			credentials = "098d2c478e9c11555ce2823231e02ec1";
		}
		//3.realmName : 当前 realm 对象的 name.调用父类的 getName() 方法即可
		String realmName = getName();
		//4.盐值。
		ByteSource credentialsSalt = ByteSource.Util.bytes(username);
		SimpleAuthenticationInfo info = null;
		info = new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName);
		return info;
	}
	
	public static void main(String[] args) {
		String hashAlgorithmName = "MD5";
		Object credentials = "123456";
		Object salt = ByteSource.Util.bytes("user");
		int hashIterations = 1024;
		
		Object result = new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
		System.out.println(result);       //生成的密码(MD5)
	}

}

2.SHA1

<bean id="secondRealm" class="com.laughing.shiro.realms.SecondRealm">
    <property name="credentialsMatcher">
    	<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
    		<property name="hashAlgorithmName" value="SHA1">property>
    		<property name="hashIterations" value="1024">property>
    	bean>
    property>
bean>
package com.laughing.shiro.realms;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;

public class SecondRealm extends AuthenticatingRealm{

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		System.out.println("[SecondRelam] doGetAuthenticationInfo: " +token.hashCode());
		//1.把 AuthenticationToken 转换为 UsernamePasswordToken 
		UsernamePasswordToken upToken = (UsernamePasswordToken)token;
		//2.从 UsernamePasswordToken 中来获取 username
		String username = upToken.getUsername();
		//3.调用数据库的方法,从数据库中查询 username 对应的用户记录。
		System.out.println("从数据库中获取 username: " + username + " 所对应的用户信息.");
		
		//4.若用户不存在,则可以抛出 AuthenticationException 异常
		if ("unknown".equals(username)) {
			throw new UnknownAccountException("用户不存在");
		}
		
		//5.根据用户信息的情况,决定是否需要抛出其他的 AuthenticationException 异常
		if ("monster".equals(username)) {
			throw new LockedAccountException("用户被锁定");
		}
		//6.根据用户的情况,来构建 AuthenticationInfo 对象并返回.通常使用的实现类为:SimpleAuthenticationInfo
		//以下信息是从数据库中获取的。
		//1.principal : 认证的实体信息。可以是 username,也可以是数据表对应的用户的实体类对象。
		Object principal = username;
		//2.credentials : 密码
		Object credentials = null;
		if ("admin".equals(username)) {
			credentials = "ce2f6417c7e1d32c1d81a797ee0b499f87c5de06";
		} else if("user".equals(username)){
			credentials = "073d4c3ae812935f23cb3f2a71943f49e082a718";
		}
		//3.realmName : 当前 realm 对象的 name.调用父类的 getName() 方法即可
		String realmName = getName();
		//4.盐值。
		ByteSource credentialsSalt = ByteSource.Util.bytes(username);
		SimpleAuthenticationInfo info = null;
		info = new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName);
		return info;
	}
	
	public static void main(String[] args) {
		String hashAlgorithmName = "SHA1";
		Object credentials = "123456";
		Object salt = ByteSource.Util.bytes("admin");
		int hashIterations = 1024;
		
		Object result = new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
		System.out.println(result);     //生成的密码(SHA1)
	}
}

8.授权

授权:
	1.授权,也叫访问控制,即在应用中控制谁访问那些资源(如访问页面/编辑数据/页面操作等)。在授权中需了解的几个关键对象:主体(Subject)、
			资源(Resource)、权限(Permission)、角色(Role).
    2.主体(Subject) : 访问应用的用户,在 Shiro 中使用 Subject 代表该用户。用户只有授权后才允许访问相应的资源。
    3.资源(Resource) : 在应用中用户可以访问的 URL,比如访问 JSP 页面、查看/编辑某些数据、访问某个业务方法、打印文本等都是资源。用户只有
    		授权后才能访问。
    4.权限(Permission) : 安全策略中的原子授权单位,通过权限我们可以表示在应用中用户有没有操作某个资源的权力。即权限表示在应用中用户不能访问
    		某个资源,如:访问用户列表页面查看/新增/修改/删除用户数据(即很多时候都是 CRUD(增删改查)式权限控制)等。权限代表了用户有没有操作
    		某个资源的权利,即反映在某个资源上的操作允不允许。
    5.Shiro 支持粗粒度权限(如用户模块的所有权限)和细粒度权限(操作某个用户的权限,即实例级别的)
    6.角色(Role) : 权限的集合,一般情况下会赋予用户角色而不是权限,即这样用户可以拥有一组权限,赋予权限时比较方便。典型的如:项目经理、技术总监、
    		CTO、开发工程师等都是角色,不同的角色拥有一组不同的权限。
    		
授权方式:
	Shiro 支持三种方式的授权:
		编程式:通过写 if/else 授权代码块完成
		注解式:通过在执行代码的 Java 方法上放置相应的注解完成,没有权限将抛出相应的异常。
		JSP/GSP 标签:在 JSP/GSP 页面通过相应的标签完成。
		if (subject.hasRole("admin")) {
			//有权限
		} else {
		   //无权限
		}
			
		<shiro:hasRole name="admin">
			<!--有权限-->
        </shiro:hasRole>
        
            
        @RequiresRoles("admin")
        public void hello() {
            //有权限
        }
		
默认拦截器
	1.Shiro 内置了很多默认的拦截器,比如身份验证、授权等相关的。默认拦截器可以参考:
		org.apache.shiro.web.filter.mgt.DefaultFilter 中的枚举拦截器
		
Permissions:
	1.规则:资源标识符:操作:对象实例 ID 即对哪个资源实例可以进行什么操作。其默认支持通配符权限字符串,:标识资源/操作/实例的分割;,表示操作的分割,
			* 表示任意资源/操作/实例。
	2.多层次管理:
	   -	例如:user:query、user:edit
	   -    冒号是一个特殊字符,它用来分割权限字符串的下一部件:第一部分是权限被操作的领域(打印机),第二部分是被执行的操作。
	   -    多个值:每个部件能够保护多个值。因此,除了授权用户 user:query 和 user:edit 权限外,也可以简单的授予他们一个:user:query,edit
	   -    还可以用 * 号代替所有的值,如: user:*, 也可以写 : *:query,表示某个用户在所有的领域都有 query 的权限。
	   
	   
Shiro 的 Permissions :
	实例级访问控制:
		1.这种情况通常会使用三个部件:域、操作、被付诸实施的实例。如 : user:edit:manager
		2.也可以使用通配符来定义,如: user:edit:*、user:*:*、user:*:manager
		3.部分省略通配符:缺少的部件意味着用户可以访问所有与之匹配的值,比如:user:edit 等价于 user:edit:*、user 等价于 user:*:*
		4.注意:通配符只能从字符串的结尾处省略部件,也就是说 user:edit 并不等价于 user:*:edit
		

授权流程:
	流程如下:
		1.首先调用 Subject.isPermiited */hasRole* 接口,其会委托给 SecurityManager,而 SecurityManager 接着会委托给 Authorizer;
		2.Authorizer 是真正的授权者,如果调用如 isPermitted("user:view"),其首先会通过
			PermissionResolver 把字符串转换成相应的 Permission 实例;
		3.在进行授权之前,其会调用相应的 Realm 获取 Subject 相应的角色/权限用于匹配传入的角色/权限;
		4.Authorizer 会判断 Realm 的角色/权限是否和传入的匹配,如果有多个 Realm,会委托给 ModularRealmAuthorizer 进行循环判断,如果匹配如
			isPermitted */hasRole*会返回 true,否则返回 false 表示授权失败。
			

ModularRealmAuthorizer:
	ModularRealmAuthorizer 进行多 Realm 匹配流程:
		1.首先检查相应的 Realm 是否实现了 Authorizer;
		2.如果实现了 Authorizer,那么接着调用其相应的 isPermitted */hasRole* 接口进行匹配;
		3.如果有一个 Realm 匹配那么将返回 true,否则返回 fale.
            
     
            
            
1.授权需要继承 AuthorizingRealm 类,并实现其 doGetAuthorizationInfo 方法。
2. AuthorizingRealm 继承自 AuthenticatingRealm,但没有实现 AuthenticatingRealm 的 doGetAuthenticationInfo 方法,
	所以认证和授权只需要继承 AuthorizingRealm 就可以了。同时实现它的两个抽象方法。
3.

9.Shiro标签

Shiro 标签:
	1.Shiro 提供了 JSTL 标签用于在 JSP 页面进行权限控制,如根据登录用户显示相应的页面按钮。
	2.guest 标签:用户没有身份验证时显示相应信息,即游客访问信息:
		<shiro:guest>
			欢迎游客访问,<a href="login.jsp">登录</a>
        </shiro:guest>
    3.user 标签:用户已经经过认证/记住我登录后显示相应的信息。
    	<shiro:user>
    		欢迎[<shiro:principal/>]登录,<a href="logout">退出</a>
        </shiro:user>
    4.authenticated 标签:用户已经身份验证通过,即 Subject.login 登录成功,不是记住我登录的
    	<shiro:authenticated>
    		用户[<shiro:principal/>]已身份验证通过
        </shiro:authenticated>
    5.notAuthenticated 标签:用户未进行身份验证,即没有调用 Subject.login 进行登录,包括记住我自动登录的也属于未进行身份验证。
    	<shiro:notAuthenticated>
    		未身份验证(包括记住我)
        </shiro:notAuthenticated>
    6.principal 标签:显示用户身份信息,默认调用 Subject.getPrincipal() 获取,即 Primary Principal.
        <shiro:principal property="username"/>
	7.hasRole 标签:如果当前 Subject 有角色将显示 body 体内容:
    	<shiro:hasRole name="admin">
    		用户[<shiro:principal/>]拥有角色admin<br/>
        </shiro:hasRole>
   8.hasAnyRoles 标签:如果当前 Subject 有任意一个角色(或的关系)将显示 body 体内容
   		<shiro:hasAnyRoles name="admin,user">
   			用户[<shiro:principal/>]拥有角色 admin 或 user <br/>
        </shiro:hasAnyRoles>
   9.lacksRole : 如果当前 Subject 没有角色将显示 body 体内容
   		<shiro:lacksRole name="admin">
   			用户[<shiro:principal/]没有角色 admin <br/>
        </shiro:lacksRole>
   10.hasPermission : 如果当前 Subject 有权限将显示 body 体内容
   		<shiro:hasPermission name="user:create">
   			用户[<shiro:principal/>]拥有权限user:create<br/>
        </shiro:hasPermission>
   11.lacksPermission : 如果当前 Subject 没有权限将显示 body 体内容
   		<shiro:lacksPermission name="org:create">
   			用户[<shiro:principal/>]没有权限 org:create<br/>
        </shiro:lacksPermission>

10.权限注解

权限注解:

1.@RequiresAuthentication : 表示当前 Subject 已经通过 login 进行了身份验证;即 Subject.isAuthenticated() 返回 true.
2.@RequiresUser : 表示当前 Subject 已经身份验证或者通过记住我登录的。
3.@RequiresGuest : 表示当前 Subject 没有身份验证或通过记住我登录过,即是游客身份。
4.@RequiresRoles(value={"admin","user"},logical=Logical.AND) : 表示当前 Subject 需要角色 admin 和 user
5.@RequiresPermissions(value={"user:a","user:b"},logical=Logical.OR) :表示当前 Subject 需要权限  user:a 或 user:b.

11.自定义拦截器

自定义拦截器:
	通过自定义拦截器可以扩展功能,: 动态 url-角色/权限访问控制的实现、根据 Subject 身份信息获取用户信息绑定到 
		Requeset(即设置通用数据)、验证码验证、在线用户信息的保存等

12.多 Realm 授权的通过标准


13.会话管理

13.1.概述

概述:
	Shiro 提供了完整的企业级会话管理功能,不依赖于底层容器(如 Web 容器 tomcat),不管 JavaSE 还是 JavaEE 环境都可以使用,提供了会话管理、
	会话事件监听、会话存储/持久化、容器无关的集群、失效/过期支持、对 Web 的透明支持、SSO 单点登录的支持等特性。

13.2.会话相关的 API

1.Subject.getSession() : 
	即可获取会话;其等价于 Subject.getSession(true),即如果当前没有创建 Session 对象会创建一个;Subject.getSession(false),如果当前没有创建 Session 则返回 null.
        
2.session.getId() : 获取当前会话的唯一标识
3.session.getHost() : 获取当前 Subject 的主机地址
4.session.getTimeout() & session.setTimeout(毫秒) : 获取/设置当前 Session 的过期时间。
5.session.touch() & session.stop() :
	更新会话最后访问时间及销毁会话;当 Subject.logout() 时会自动调用 stop 方法来销毁会话。如果在 web 中,调用 HttpSession.invalidate() 也会自动调用
	Shiro Session.stop 方法进行销毁 Shiro 的会话。
6.session.setAttribute(key,val) & session.getAttribute(key) & session.removeAttribute(key) :
	设置/获取/删除会话属性;在整个会话范围内都可以对这些属性进行操作。

13.3.会话监听器

会话监听器用于监听会话的创建、过期及停止事件

13.4.SessionDao

1.AbstractSessionDAO 提供了 SessionDAO 的基础实现,如生成会话 ID 等。
2.CachingSessionDAO 提供了对开发者透明的会话缓存的功能,需要设置相应的 CacheManager
3.MemorySessionDAO 直接在内存中进行会话维护
4.EnterpriseCacheSessionDAO 提供了缓存功能的会话维护,默认情况下使用 MapCache 实现,内部使用 ConcurrentHashMap 保存缓存的会话。
配置实例:


<bean id="sessionIdGenerator"
      class="org.apache.shiro.session.mgt.ejs.JavaUuidSessionIdGenerator"/>


<bean id="sessionDAO"
      class="com.laughing.shiro.realms.MySessionDao">
	<property name="activeSessionCacheName" value="shiro-activeSessionCache"/>
    <property name="sessionIdGenerator" ref="sessionIdGenerator"/>
bean>


<bean id="sessionManager" class="org.apache.shiro.mgt.DefaultSessionManager">
	<property name="globalSessionTimeout" value="1800000"/>
    <property name="deleteInvalidSessions" value="true"/>
    <property name="sessionValidationSchedulerEnabled" value="true"/>
    <property name="sessionDAO" ref="sessionDAO"/>
bean>

13.5.会话认证

1.Shiro 提供了会话验证调度器,用于定期的验证会话是否已过期,如果过期将停止会话。
2.出于性能考虑,一般情况下都是获取会话时来验证会话是否过期并停止会话的;但是如在 Web 环境中,如果用户不主动退出是不知道会话是否过期的,因此
	需要定期的检测会话是否过期,Shiro提供了会话验证调度器。SessionValidationScheduler
3.Shiro 也提供了使用 Quartz 会话验证调度器:QuartzSessionValidationScheduler.

14.缓存

14.1.CacheManagerAware 接口

CacheManagerAware 接口:
1.Shiro 内部相应的组件(DefaultSecurityManager) 会自动检测相应的对象(如 Realm) 是否实现了 CacheManagerAware
	并自动注入相应的 CacheManager。

14.2.Realm 缓存

Realm 缓存:
1.Shiro提供了 CachingRealm ,其实现了CacheManagerAware接口,提供了缓存的一些基础实现;
2.AuthenticatingRealm 及 AuthorizingRealm 也分别提供了对 AuthenticationInfo 和 AutnorizationInfo 信息的缓存。

14.3.Session 缓存

Session 缓存:
1.如 SecurityManager 实现了 SessionSecurityManager,其会判断 SesssionManager 是否实现了 CacheManagerAware 接口,如果
	实现了会把 CacheManager 设置给它。
2.SessionManager 也会判断相应的 SessionDAO(如继承自 CachingSessionDAO) 是否实现了 CacheManagerAware,如果实现了会把 
	CacheManager 设置给它。
3.设置了缓存的 SessionManager,查询时会先查询缓存,如果找不到才查数据库。

15.RememberMe

RememberMe

概述:
Shiro 提供了记住我(RememberMe)的功能,比如访问如淘宝等一些网站时,关闭了浏览器,下次再打开时还是能记住你是谁,下次访问时无需登录即可访问,基本流程如下:
1.首先在登录页面选中 RememberMe 然后登录成功;如果是浏览器登录,一般会把 RememberMe 的 Cookie 写到客户端并保存下来。
2.关闭浏览器再重新打开;会发现浏览器还是记住你的;
3.访问一般的网页服务器端还是知道你是谁,且能正常访问;
4.但是比如我们访问淘宝时,如果要查看我的订单或进行支付时,此时还是需要进行身份验证的,以确保当前用户还是你。

16.认证和记住我

认证和记住我:
1.subject.isAuthenticated() : 表示用户进行了身份验证登录的,即使有 Subject.login 进行了登录。
2.subject.isRemembered() : 表示用户是通过记住我登录的,此时可能并不是真正的你(如你的朋友使用你的电脑,或者你的 cookie 被窃取)在访问的。
3.两者二选一,即 subject.isAuthenticated() == true,则 subject.isRemembered() == false;反之一样。

17.建议

建议:
1.访问一般页面:如个人在主页之类的,我们使用  user 拦截器即可,user 拦截器只要用户登录
2.访问特殊页面:如我的订单,提交订单页面,我们使用 authc 拦截器即可,authc 拦截器会判断用户是否是通过
	Subject.login(isAuthenticated() == true) 登录的,如果是才放行,否则会跳转到登录页面叫你重新登录。

18.RememberMe实现

建议:
1.如果要自己做 RememberMe,需要在登录之前这样创建 Token:UsernamePasswordToken(用户名,密码,是否记住我),且调用 UsernamePasswordToken 的 :
	token.setRememberMe(true) 方法。

19.RememberMe配置

<bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
	<constructor-arg value="rememberMe"/>
    <property name="httpOnly" value="true"/>
    
bean>


<bean id="rememberMeManager"
      class="org.apache.shiro.web.mgt.CookieRememberMeManager">
	<property name="cipherKey" 
              value="#{T(org.apache.shiro.codec.Base64).decode('4AvVhmFLUs0KTA3Kprsdag==')}"/>
    <property name="cookie" ref="rememberMeCookie"/>
bean>

20.代码示例

web.xml


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  
  
	
	<context-param>
		<param-name>contextConfigLocationparam-name>
		<param-value>classpath:applicationContext.xmlparam-value>
	context-param>

	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
	listener>
	
	
	<servlet>
		<servlet-name>springservlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		<load-on-startup>1load-on-startup>
	servlet>

	
	<servlet-mapping>
		<servlet-name>springservlet-name>
		<url-pattern>/url-pattern>
	servlet-mapping>
	
	
	
    <filter>
        <filter-name>shiroFilterfilter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
        <init-param>
            <param-name>targetFilterLifecycleparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>

    <filter-mapping>
        <filter-name>shiroFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
	
web-app>
applicationContext.xxml


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    
    
         
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="cacheManager" ref="cacheManager"/>
        <property name="authenticator" ref="authenticator">property>
        
        <property name="realms">
        	<list>
    			<ref bean="jdbcRealm"/>
    			<ref bean="secondRealm"/>
    		list>
        property>
        
        <property name="rememberMeManager.cookie.maxAge" value="10">property>
    bean>

    
         
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        
        
        <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/> 
    bean>
    
    <bean id="authenticator" 
    	class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
    	<property name="authenticationStrategy">
    		<bean class="org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy">bean>
    	property>
    bean>

    
         
    <bean id="jdbcRealm" class="com.laughing.shiro.realms.ShiroRealm">
    	<property name="credentialsMatcher">
    		<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
    			<property name="hashAlgorithmName" value="MD5">property>
    			<property name="hashIterations" value="1024">property>
    		bean>
    	property>
    bean>
    
    <bean id="secondRealm" class="com.laughing.shiro.realms.SecondRealm">
    	<property name="credentialsMatcher">
    		<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
    			<property name="hashAlgorithmName" value="SHA1">property>
    			<property name="hashIterations" value="1024">property>
    		bean>
    	property>
    bean>

    
    
           
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

    
         
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
          depends-on="lifecycleBeanPostProcessor"/>
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    bean>

    
         
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/login.jsp"/>
        <property name="successUrl" value="/list.jsp"/>
        <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
        
        <property name="filterChainDefinitionMap" ref="filterChainDefinitionMap">property>
        
        
        
    bean>
    
    
    <bean id="filterChainDefinitionMap" 
    	factory-bean="filterChainDefinitionMapBuilder" factory-method="buildFilterChainDefinitionMap">bean>
    
    <bean id="filterChainDefinitionMapBuilder"
    	class="com.laughing.shiro.factory.FilterChainDefinitionMapBuilder">bean>
    
    <bean id="shiroService"
    	class="com.laughing.shiro.services.ShiroService">bean>

beans>

spring-servlet.xml


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	<context:component-scan base-package="com.atguigu.shiro">context:component-scan>
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/">property>
		<property name="suffix" value=".jsp">property>
	bean>
	
	<mvc:annotation-driven>mvc:annotation-driven>
	<mvc:default-servlet-handler/>

beans>

ShiroController.java

package com.laughing.shiro.controller;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.laughing.shiro.services.ShiroService;

@RequestMapping("/shiro")
@Controller
public class ShiroController {
	
	
	@Autowired
	private ShiroService shiroService;
	
	
	@RequestMapping("/testShiroAnnotation")
	public String testShiroAnnotation(HttpSession session) {
		shiroService.testMethod();
		session.setAttribute("key", "123456");
		return "redirect:/list.jsp";
	}
	
	
	@RequestMapping("/login")
	public String login(@RequestParam("username") String username,
			@RequestParam("password") String password) {
		
		//1.获取当前的 Subject
		Subject currentUser = SecurityUtils.getSubject();
		
		//2.测试当前的用户是否已经被认证。即是否已经登录。
		if(!currentUser.isAuthenticated()) {
			//3.若没有被认证,则把用户名和密码封装为 UsernamePasswordToken 对象
			UsernamePasswordToken token = new UsernamePasswordToken(username, password);
			//remember me
			token.setRememberMe(true);
			try {
				System.out.println("1." + token.hashCode());
				//执行登录:调用 Subject 的 login(AuthenticationToken) 方法
				currentUser.login(token);
			} catch (AuthenticationException e) {
				System.out.println("登录失败:" +e.getMessage());
			}
		}
		
		return "redirect:/list.jsp";
	}
}
ShiroRealm.java


package com.laughing.shiro.realms;

import java.util.HashSet;
import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class ShiroRealm extends AuthorizingRealm{

	//用于认证的方法
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		System.out.println("doGetAuthenticationInfo: " +token.hashCode());
		//1.把 AuthenticationToken 转换为 UsernamePasswordToken 
		UsernamePasswordToken upToken = (UsernamePasswordToken)token;
		//2.从 UsernamePasswordToken 中来获取 username
		String username = upToken.getUsername();
		//3.调用数据库的方法,从数据库中查询 username 对应的用户记录。
		System.out.println("从数据库中获取 username: " + username + " 所对应的用户信息.");
		
		//4.若用户不存在,则可以抛出 AuthenticationException 异常
		if ("unknown".equals(username)) {
			throw new UnknownAccountException("用户不存在");
		}
		
		//5.根据用户信息的情况,决定是否需要抛出其他的 AuthenticationException 异常
		if ("monster".equals(username)) {
			throw new LockedAccountException("用户被锁定");
		}
		//6.根据用户的情况,来构建 AuthenticationInfo 对象并返回.通常使用的实现类为:SimpleAuthenticationInfo
		//以下信息是从数据库中获取的。
		//1.principal : 认证的实体信息。可以是 username,也可以是数据表对应的用户的实体类对象。
		Object principal = username;
		//2.credentials : 密码
		Object credentials = null;
		if ("admin".equals(username)) {
			credentials = "038bdaf98f2037b31f1e75b5b4c9b26e";
		} else if("user".equals(username)){
			credentials = "098d2c478e9c11555ce2823231e02ec1";
		}
		//3.realmName : 当前 realm 对象的 name.调用父类的 getName() 方法即可
		String realmName = getName();
		//4.盐值。
		ByteSource credentialsSalt = ByteSource.Util.bytes(username);
		SimpleAuthenticationInfo info = null;
		info = new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName);
		return info;
	}
	
	public static void main(String[] args) {
		String hashAlgorithmName = "MD5";
		Object credentials = "123456";
		Object salt = ByteSource.Util.bytes("user");
		int hashIterations = 1024;
		
		Object result = new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
		System.out.println(result);
	}

	
	//用于授权的方法
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		//1.从 PrincipalCollection 中获取登录用户的信息
		Object principal = principals.getPrimaryPrincipal();
		//2.利用登录的用户的信息来用户当前的角色或权限(可能需要查询数据库)
		Set<String> roles = new HashSet<>();
		roles.add("user");
		if ("admin".equals(principal)) {
			roles.add("admin");
		}
		//3.创建 SimpleAuthorizationInfo,并设置 roles 属性
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
		//4.返回 SimpleAuthorizationInfo 对象
		return info;
	}


}

SecondRealm.java

package com.laughing.shiro.realms;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;

public class SecondRealm extends AuthenticatingRealm{

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		System.out.println("[SecondRelam] doGetAuthenticationInfo: " +token.hashCode());
		//1.把 AuthenticationToken 转换为 UsernamePasswordToken 
		UsernamePasswordToken upToken = (UsernamePasswordToken)token;
		//2.从 UsernamePasswordToken 中来获取 username
		String username = upToken.getUsername();
		//3.调用数据库的方法,从数据库中查询 username 对应的用户记录。
		System.out.println("从数据库中获取 username: " + username + " 所对应的用户信息.");
		
		//4.若用户不存在,则可以抛出 AuthenticationException 异常
		if ("unknown".equals(username)) {
			throw new UnknownAccountException("用户不存在");
		}
		
		//5.根据用户信息的情况,决定是否需要抛出其他的 AuthenticationException 异常
		if ("monster".equals(username)) {
			throw new LockedAccountException("用户被锁定");
		}
		//6.根据用户的情况,来构建 AuthenticationInfo 对象并返回.通常使用的实现类为:SimpleAuthenticationInfo
		//以下信息是从数据库中获取的。
		//1.principal : 认证的实体信息。可以是 username,也可以是数据表对应的用户的实体类对象。
		Object principal = username;
		//2.credentials : 密码
		Object credentials = null;
		if ("admin".equals(username)) {
			credentials = "ce2f6417c7e1d32c1d81a797ee0b499f87c5de06";
		} else if("user".equals(username)){
			credentials = "073d4c3ae812935f23cb3f2a71943f49e082a718";
		}
		//3.realmName : 当前 realm 对象的 name.调用父类的 getName() 方法即可
		String realmName = getName();
		//4.盐值。
		ByteSource credentialsSalt = ByteSource.Util.bytes(username);
		SimpleAuthenticationInfo info = null;
		info = new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName);
		return info;
	}
	
	public static void main(String[] args) {
		String hashAlgorithmName = "SHA1";
		Object credentials = "123456";
		Object salt = ByteSource.Util.bytes("admin");
		int hashIterations = 1024;
		
		Object result = new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
		System.out.println(result);
	}
}

TestRealm.java


package com.laughing.shiro.realms;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class TestRealm extends AuthorizingRealm {

	//用于授权的方法
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	//用于认证的方法
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
		// TODO Auto-generated method stub
		return null;
	}
}
ShiroService.java


package com.laughing.shiro.services;

import java.util.Date;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

public class ShiroService {
	
	public void testMethod() {
		System.out.println("testMethod, time: " + new Date());
		
		
		Session session = SecurityUtils.getSubject().getSession();
		Object val = session.getAttribute("key");
		
		System.out.println("Service SessionValue--->"+val);
	}
}
FilterChainDefinitionMapBuilder.java


package com.laughing.shiro.factory;

import java.util.LinkedHashMap;

public class FilterChainDefinitionMapBuilder {
	
	public LinkedHashMap<String, String> buildFilterChainDefinitionMap() {
		LinkedHashMap<String, String> map = new LinkedHashMap<>();
		
		map.put("/login.jsp", "anon");
		map.put("/shiro/login", "anon");
		map.put("/shiro/logout", "logout");
		map.put("/user.jsp", "authc,roles[user]");
		map.put("/admin.jsp", "authc,roles[admin]");
		map.put("/list.jsp", "user");	
		
		map.put("/**", "authc");
		
		return map;
	}
}
ehcache.xml


<ehcache>

    
    <diskStore path="java.io.tmpdir"/>
    
    <cache name="authorizationCache"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    cache>

    <cache name="authenticationCache"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    cache>

    <cache name="shiro-activeSessionCache"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    cache>

    
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />

    

    
    <cache name="sampleCache1"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
        />

    
    <cache name="sampleCache2"
        maxElementsInMemory="1000"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
        /> -->

    

ehcache>
admin.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title heretitle>
head>
<body>
	
	<h4>Admin Pageh4>
	
body>
html>
list.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>    
    

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title heretitle>
head>
<body>
	
	<h4>List Pageh4>
	
	Welcome: <shiro:principal>shiro:principal>
	
	<shiro:hasRole name="admin">
	<br><br>
	<a href="admin.jsp">Admin Pagea>
	shiro:hasRole>
	
	<shiro:hasRole name="user">
	<br><br>
	<a href="user.jsp">User Pagea>
	shiro:hasRole>
	
	<br><br>
	<a href="shiro/testShiroAnnotation">Test ShiroAnnotationa>
	
	<br><br>
	<a href="shiro/logout">Logouta>
	
body>
html>
login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title heretitle>
head>
<body>
	
	<h4>Login Pageh4>
	
	<form action="shiro/login" method="POST">
		username: <input type="text" name="username"/>
		<br><br>
		
		password: <input type="password" name="password"/>
		<br><br>
		
		<input type="submit" value="Submit"/>
	form>
	
body>
html>
unauthorized.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title heretitle>
head>
<body>
	
	<h4>Unauthorized Pageh4>
	
body>
html>
user.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title heretitle>
head>
<body>
	
	<h4>User Pageh4>
	
body>
html>
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.3.jar
ehcache-core-2.4.3.jar
jackson-all-1.9.11.jar
log4j-1.2.15.jar
shiro-all-1.3.2.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-context-support-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar

你可能感兴趣的:(流行框架)