我們首先了解下什么是shiro ,Shiro 是 JAVA 世界中新近出現的權限框架,較之 JAAS 和 Spring Security,Shiro 在保持強大功能的同時,還在簡單性和靈活性方面擁有巨大優勢
Shiro 是一個強大而靈活的開源安全框架,能夠非常清晰的處理認證、授權、管理會話以及密碼加密。如下是它所具有的特點:
- 易於理解的 Java Security API;
- 簡單的身份認證(登錄),支持多種數據源(LDAP,JDBC,Kerberos,ActiveDirectory 等);
- 對角色的簡單的簽權(訪問控制),支持細粒度的簽權;
- 支持一級緩存,以提升應用程序的性能;
- 內置的基於 POJO 企業會話管理,適用於 Web 以及非 Web 的環境;
- 異構客戶端會話訪問;
- 非常簡單的加密 API;
- 不跟任何的框架或者容器捆綁,可以獨立運行。
Shiro 主要有四個組件
- SecurityManager
典型的 Facade,Shiro 通過它對外提供安全管理的各種服務。
- Authenticator
對“Who are you ?”進行核實。通常涉及用戶名和密碼。
這 個組件負責收集 principals 和 credentials,並將它們提交給應用系統。如果提交的 credentials 跟應用系統中提供的 credentials 吻合,就能夠繼續訪問,否則需要重新提交 principals 和 credentials,或者直接終止訪問。
- Authorizer
身 份份驗證通過后,由這個組件對登錄人員進行訪問控制的篩查,比如“who can do what”, 或者“who can do which actions”。Shiro 采用“基於 Realm”的方法,即用戶(又稱 Subject)、用戶組、角色和 permission 的聚合體。
- Session Manager
這個組件保證了異構客戶端的訪問,配置簡單。它是基於 POJO/J2SE 的,不跟任何的客戶端或者協議綁定。
Shiro 的認證和簽權可以通過 JDBC、LDAP 或者 Active Directory 來訪問數據庫、目錄服務器或者 Active Directory 中的人員以及認證 / 簽權信息。SessionManager 通過會話 DAO 可以將會話保存在 cache 中,或者固化到數據庫或文件系統中。
簡介
apache shiro 是一個功能強大和易於使用的Java安全框架,為開發人員提供一個直觀而全面的的解決方案的認證,授權,加密,會話管理。
在實際應用中,它實現了應用程序的安全管理的各個方面。
shiro的功能
apache shiro能做什么?
支持認證跨一個或多個數據源(LDAP,JDBC,kerberos身份等)
執行授權,基於角色的細粒度的權限控制。
增強的緩存的支持。
支持web或者非web環境,可以在任何單點登錄(SSO)或集群分布式會話中使用。
主要功能是:認證,授權,會話管理和加密。
下載並且使用
1,確保系統內安裝JDK1.5+和maven2.2+。
2,到shiro主頁下載shiro.
3,解壓縮
unzip shiro-root-1.1.0-source-release.zip
4,進入到quickstart目錄
cd shiro-root-1.1.0/samples/quickstart
5,運行quickstart
mvn compile exec:java
執行完成如下圖:
Quickstart.java
// get the currently executing user: Subject currentUser = SecurityUtils.getSubject();
使用SecurityUtils.getSubject(),我們可以得到當前正在執行的主題。
得到主題之后,你可以得到他對應的會話信息
// Do some stuff with a Session (no need for a web or EJB container!!!) 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 + "]"); }
你可以得到http的session信息,也可以在非web環境中使用,得到相對應的會話信息。
如果在web應用程序中部署應用,默認情況下,應用將以HttpSession為基礎。在企業級應用中,你在多個應用中可以使用相同的API,無論部署環境。而且使用任何客戶端技術你都可以共享會話數據。
接下來判斷登錄信息
// let's login the current user so we can check against roles and permissions: if (!currentUser.isAuthenticated()) { UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa"); token.setRememberMe(true); try { currentUser.login(token); } catch (UnknownAccountException uae) { log.info("There is no user with username of " + token.getPrincipal()); } catch (IncorrectCredentialsException ice) { log.info("Password for account " + token.getPrincipal() + " was incorrect!"); } 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: if (currentUser.hasRole("schwartz")) { log.info("May the Schwartz be with you!"); } else { log.info("Hello, mere mortal."); }
接着測試是否有特定的權限
//test a typed permission (not instance-level) 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("winnebago:drive:eagle5")) { 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! currentUser.logout();
認證就是用戶確認身份的過程,確認登錄的用戶身份能夠操作的內容。
使用shiro認證分為以下幾個步驟:
1,得到主體的認證和憑據。
// let's login the current user so we can check against roles and permissions: |
if (!currentUser.isAuthenticated()) { |
UsernamePasswordToken token = new UsernamePasswordToken( "lonestarr" , "vespa" ); |
token.setRememberMe( true ); |
2,提交認證和憑據給身份驗證系統。
Subject currentUser = SecurityUtils.getSubject(); |
currentUser.login(token); |
3,判斷是否允許訪問,重試認證或者阻止訪問。
try { |
currentUser.login(token); |
} catch (UnknownAccountException uae) { |
log.info( "There is no user with username of " + token.getPrincipal()); |
} catch (IncorrectCredentialsException ice) { |
log.info( "Password for account " + token.getPrincipal() + " was incorrect!" ); |
} 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? |
} |
其中Remember Me的功能包括兩個方法,一個是
isRemembered
boolean isRemembered()
非匿名登錄的用戶可以記住上次使用的主題的信息。
isAuthenticated
boolean isAuthenticated()
在此期間需要使用有效的憑據登錄系統,否則值為false.
授權操作
授權的例子就是是否可以訪問某個頁面,可以操作某個按鈕,是否可以編緝對應的數據等。
如何在shiro中使用授權
1,使用編程方式
判斷是否有管理員角色
if (currentUser.hasRole( "admin" )) { |
判斷用戶是否有打印的權限
Permission printPermission = new PrinterPermission(“laserjet3000n”,“print”); |
If (currentUser.isPermitted(printPermission)) { //do one thing (show the print button?) } else { //don’t show the button? }
也可以使用字符串的方式驗證
String perm = “printer:print:laserjet4400n”; |
|
if (currentUser.isPermitted(perm)){ |
//show the print button? |
} else { |
//don’t show the button? |
} |
2,使用注釋方式
判斷用戶是否有 創建賬戶權限
//Will throw an AuthorizationException if none |
//of the caller’s roles imply the Account |
//'create' permission\u000B |
@RequiresPermissions (“account:create”) |
public void openAccount( Account acct ) { |
//create the account |
} |
判斷用戶角色,如果符合角色,可以使用對應方法
//Throws an AuthorizationException if the caller |
//doesn’t have the ‘teller’ role: |
|
@RequiresRoles ( “teller” ) |
public void openAccount( Account acct ) { |
//do something in here that only a teller |
//should do |
} |
3,使用jsp taglib
判斷用戶是否有管理權限
<%@ taglib prefix=“shiro” uri=http: //shiro.apache.org/tags %> |
<html> |
<body> |
<shiro:hasPermission name=“users:manage”> |
<a href=“manageUsers.jsp”> |
Click here to manage users |
</a> |
</shiro:hasPermission> |
<shiro:lacksPermission name=“users:manage”> |
No user management for you! |
</shiro:lacksPermission> |
</body> |
</html> |
從高的級別來看shiro:
看一下官方的圖
應用程序調用subject(主題),主題可以是一個用戶也可以是與系統交互的另一個系統,主題綁定shiro的權限管 理,SecurityManager(安全管理),它控制與有與主題相關的安全操作。Realm(橋梁)它是安全與數據之間的橋,它封裝了比如DAO的配 置信息,可以指定連接的數據源,也可使用其它的認證方式,如LDAP等。
然后看一下詳細的架構圖:
Subject (org.apache.shiro.subject.Subject)
主題:與系統交互的第三方如(用戶,cron服務,第三方應用)等。
SecurityManager (org.apache.shiro.mgt.SecurityManager)
shiro系統的核心,協調主題使用的操作,驗證,配置等。
Authenticator (org.apache.shiro.authc.Authenticator)
身份驗證組件,對企圖登錄系統的用戶進行身份的驗證。其中包含一個Authentication Strategy
(org.apache.shiro.authc.pam.AuthenticationStrategy)組件。配置驗證成功與失敗的條件。
Authorizer (org.apache.shiro.authz.Authorizer)
授權組件,指用戶訪問特定應用程序的機制。
SessionManager (org.apache.shiro.session.mgt.SessionManager)
管理會話如何創建生命周期。其中包括的sessiondao是管理會議數據的持久操作:SessionDAO (org.apache.shiro.session.mgt.eis.SessionDAO),代表執行sessionManager的CRUD操作。
CacheManager (org.apache.shiro.cache.CacheManager)
緩存管理模塊。
Cryptography (org.apache.shiro.crypto.*)
加密模塊。
Realms (org.apache.shiro.realm.Realm)
多種方式處理的橋梁。
多種配置方式:
與spring,jboss,guice等進行配置。
1,編程方式配置
例如:
Realm realm = //instantiate or acquire a Realm instance. We'll discuss Realms later. |
|
SecurityManager securityManager = new DefaultSecurityManager(realm); |
//Make the SecurityManager instance available to the entire application via static memory: |
SecurityUtils.setSecurityManager(securityManager); |
2,sessionManager對象圖
如果你想使用sessionManager配置自定義的sessionDao信息,進行自定義會話管理
... |
|
DefaultSecurityManager securityManager = new DefaultSecurityManager(realm); |
SessionDAO sessionDAO = new CustomSessionDAO(); |
|
((DefaultSessionManager)securityManager.getSessionManager()).setSessionDAO(sessionDAO); |
... |
3,INI配置
1) 創建一個INI從SecurityManager
可以從多種方式讀取INI配置文件的信息,如文件系統,類路徑等
import org.apache.shiro.SecurityUtils; |
import org.apache.shiro.util.Factory; |
import org.apache.shiro.mgt.SecurityManager; |
import org.apache.shiro.config.IniSecurityManagerFactory; |
... |
|
Factory<SecurityManager> factory = new IniSecurityManagerFactory( "classpath:shiro.ini" ); |
SecurityManager securityManager = factory.getInstance(); |
SecurityUtils.setSecurityManager(securityManager); |
2) 通過Ini實例讀取
類似於Properties的方式
import org.apache.shiro.SecurityUtils; |
import org.apache.shiro.util.Factory; |
import org.apache.shiro.mgt.SecurityManager; |
import org.apache.shiro.config.Ini; |
import org.apache.shiro.config.IniSecurityManagerFactory; |
... |
|
Ini ini = new Ini(); |
//populate the Ini instance as necessary |
... |
Factory<SecurityManager> factory = new IniSecurityManagerFactory(ini); |
SecurityManager securityManager = factory.getInstance(); |
SecurityUtils.setSecurityManager(securityManager); |
加載之后就可以操作INI的配置了。
4,INI配置
每一個節點都是單獨的,不可以重復,注釋可以使用#或者;
配置示例
# ======================= |
# Shiro INI configuration |
# ======================= |
[main] |
# Objects and their properties are defined here, |
# Such as the securityManager, Realms and anything |
# else needed to build the SecurityManager |
|
[users] |
# The 'users' section is for simple deployments |
# when you only need a small number of statically-defined |
# set of User accounts. |
|
[roles] |
# The 'roles' section is for simple deployments |
# when you only need a small number of statically-defined |
# roles. |
|
[urls] |
# The 'urls' section is used for url-based security |
# in web applications. We'll discuss this section in the |
# Web documentation |
1) [main]
配置sessionManager的實例和它的依賴。
配置示例
[main] |
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher |
|
myRealm = com.company.security.shiro.DatabaseRealm |
myRealm.connectionTimeout = 30000 |
myRealm.username = jsmith |
myRealm.password = secret |
myRealm.credentialsMatcher = $sha256Matcher |
securityManager.sessionManager.globalSessionTimeout = 1800000 |
定義一個對象
[main] |
myRealm = com.company.shiro.realm.MyRealm |
... |
簡單的屬性設置
... |
myRealm.connectionTimeout = 30000 |
myRealm.username = jsmith |
... |
配置信息將轉入到對應的set方法中
... |
myRealm.setConnectionTimeout( 30000 ); |
myRealm.setUsername( "jsmith" ); |
... |
參考值
你可以使用$符號引用先前定義的一個對象的實例
... |
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher |
... |
myRealm.credentialsMatcher = $sha256Matcher |
... |
嵌套屬性
... |
securityManager.sessionManager.globalSessionTimeout = 1800000 |
... |
將被注入到下面的程序中
securityManager.getSessionManager().setGlobalSessionTimeout( 1800000 ); |
引用其它的屬性
sessionListener1 = com.company.my.SessionListenerImplementation |
... |
sessionListener2 = com.company.my.other.SessionListenerImplementation |
... |
securityManager.sessionManager.sessionListeners = $sessionListener1, $sessionListener2 |
以鍵值的配置方式
object1 = com.company.some.Class |
object2 = com.company.another.Class |
... |
anObject = some. class .with.a.Map.property |
|
anObject.mapProperty = key1:$object1, key2:$object2 |
2) [users]
在用戶比較少的情況下這種配置信息是有效的
[users] |
admin = secret |
lonestarr = vespa, goodguy, schwartz |
darkhelmet = ludicrousspeed, badguy, schwartz |
3) [roles]
如果角色信息比較少的情況下可以使用這項配置
[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 'drive' (action) the winnebago (type) with |
# license plate 'eagle5' (instance specific id) |
goodguy = winnebago:drive:eagle5 |
4) [urls]
配置url等可訪問的資源信息。
shiro(3)-shiro核心
身份認證
身份認證分三個步驟
1)提交主題和憑據
2)進行身份認證
3)判斷是通過,重新提交還是不通過
驗證順序
1)調用subject的login方法,提交主體和憑據。
2)得到對應操作的Security Manager
3)通過Sceurity Manager得到對應的Autherticator實例
4)根據配置策略查找對應的橋信息
5)通過橋信息到對應的配置處理進行身份驗證
驗證器
如果你想配置一個自定義的驗證器
可以在配置文件中使用
[main] ... authenticator = com.foo.bar.CustomAuthenticator securityManager.authenticator = $authenticator
配置策略信息
AtLeastOneSuccessfulStrategy 如果一個驗證成功,則驗證結果為成功
FirstSuccessfulStrategy 只有第一個成功,才算成功
AllSuccessfulStrategy 所有的都必須成功
對應的在配置文件中的策略使用如下
shiro.ini [main] ... authcStrategy = org.apache.shiro.authc.pam.FirstSuccessfulStrategy securityManager.authenticator.authenticationStrategy = $authcStrategy ...
執行順序
1)隱式順序
blahRealm = com.company.blah.Realm ... fooRealm = com.company.foo.Realm ... barRealm = com.company.another.Realm
按上下順序執行
2)指定順序
blahRealm = com.company.blah.Realm ... fooRealm = com.company.foo.Realm ... barRealm = com.company.another.Realm securityManager.realms = $fooRealm, $barRealm, $blahRealm ...
按指定的順序執行
授權
控制誰有權限訪問應用程序
授權的幾個要素:權限,角色和用戶。
三種權限的判斷方式
1)編程
角色判斷
Subject currentUser = SecurityUtils.getSubject(); if (currentUser.hasRole("administrator")) { //show the admin button } else { //don't show the button? Grey it out? }
hasRole(String roleName) 主題是否已分配給指定的角色
hasRoles(List<String> roleNames) 是否包含指定的角色
hasAllRoles(Collection<String> roleNames) 是否包含指定的所有角色
角色斷言
Subject currentUser = SecurityUtils.getSubject(); //guarantee that the current user is a bank teller and //therefore allowed to open the account: currentUser.checkRole("bankTeller"); openBankAccount();
checkRole(String roleName) 斷言是否是指定角色
checkRoles(Collection<String> roleNames) 斷言是否包含以下角色
checkRoles(String... roleNames) 斷言是否包含所有角色
如果判斷指定用戶是否有權限訪問指定名稱的打印機
那么就會用到下列幾個方法
Permission printPermission = new PrinterPermission("laserjet4400n", "print"); Subject currentUser = SecurityUtils.getSubject(); if (currentUser.isPermitted(printPermission)) { //show the Print button } else { //don't show the button? Grey it out? }
isPermitted(Permission p) 判斷主題是否允許執行一個動作
isPermitted(List<Permission> perms) 是否允許執行一組動作
isPermittedAll(Collection<Permission> perms) 是否允許執行所有動作
基於字符串的權限檢查
Subject currentUser = SecurityUtils.getSubject(); if (currentUser.isPermitted("printer:print:laserjet4400n")) { //show the Print button } else { //don't show the button? Grey it out? }
也可以如下使用
Subject currentUser = SecurityUtils.getSubject(); Permission p = new WildcardPermission("printer:print:laserjet4400n"); if (currentUser.isPermitted(p) { //show the Print button } else { //don't show the button? Grey it out? }
權限斷言類似於角色斷言。
2)annocation方式
The RequiresAuthentication annotation
@RequiresAuthentication public void updateAccount(Account userAccount) { //this method will only be invoked by a //Subject that is guaranteed authenticated ... }
等同於下述代碼
public void updateAccount(Account userAccount) { if (!SecurityUtils.getSubject().isAuthenticated()) { throw new AuthorizationException(...); } //Subject is guaranteed authenticated here ... }
The RequiresGuest annotation
@RequiresGuest public void signUp(User newUser) { //this method will only be invoked by a //Subject that is unknown/anonymous ... }
等同於
public void signUp(User newUser) { Subject currentUser = SecurityUtils.getSubject(); PrincipalCollection principals = currentUser.getPrincipals(); if (principals != null && !principals.isEmpty()) { //known identity - not a guest: throw new AuthorizationException(...); } //Subject is guaranteed to be a 'guest' here ... }
The RequiresPermissions annotation
@RequiresPermissions("account:create") public void createAccount(Account account) { //this method will only be invoked by a Subject //that is permitted to create an account ... }
等同於
public void createAccount(Account account) { Subject currentUser = SecurityUtils.getSubject(); if (!subject.isPermitted("account:create")) { throw new AuthorizationException(...); } //Subject is guaranteed to be permitted here ... }
The RequiresRoles permission
@RequiresRoles("administrator") public void deleteUser(User user) { //this method will only be invoked by an administrator ... }
等同於
public void deleteUser(User user) { Subject currentUser = SecurityUtils.getSubject(); if (!subject.hasRole("administrator")) { throw new AuthorizationException(...); } //Subject is guaranteed to be an 'administrator' here ... }
The RequiresUser annotation
@RequiresUser public void updateAccount(Account account) { //this method will only be invoked by a 'user' //i.e. a Subject with a known identity ... }
等同於
public void updateAccount(Account account) { Subject currentUser = SecurityUtils.getSubject(); PrincipalCollection principals = currentUser.getPrincipals(); if (principals == null || principals.isEmpty()) { //no identity - they're anonymous, not allowed: throw new AuthorizationException(...); } //Subject is guaranteed to have a known identity here ... }
授權順序
1)應用程序調用主題,判斷hasRole,isPermitted得到角色或者用戶權限的列表。
2)組成對應的授權方法
3)協調如何授權
4)通過橋進行各種方式的授權
web應用
配置web.xml
<listener> <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class> </listener> ... <filter> <filter-name>ShiroFilter</filter-name> <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class> </filter> <filter-mapping> <filter-name>ShiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
如果你願意你可以自定義一個web應用
<context-param> <param-name>shiroEnvironmentClass</param-name> <param-value>com.foo.bar.shiro.MyWebEnvironment</param-value> </context-param>
如果你想改變shiro.ini的位置,那么你可以指定
<context-param> <param-name>shiroConfigLocations</param-name> <param-value>YOUR_RESOURCE_LOCATION_HERE</param-value> </context-param>
shiro.ini中的[urls]配置
例如:
... [urls] /index.html = anon /user/create = anon /user/** = authc /admin/** = authc, roles[administrator] /rest/** = authc, rest /remoting/rpc/** = authc, perms["remote:invoke"]
假如你有如下設置
/account/** = ssl, authc
/account下的任何應用程序都將觸動ssl和authc鏈
在官方的示例中,有一個aspectj的示例,這個是一個銀行的示例,簡單的做了一下修改,演示一下其中幾個方法的使用過程。
看以下幾個類,包括賬戶信息,轉賬信息,以及一些異常處理程序,還包括一個業務操作類
Account賬戶信息類
import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Date; import java.util.List; public class Account { private static long _SEQUENCE; private long _id; private String _ownerName; private volatile boolean _isActive; private double _balance; private final List<AccountTransaction> _transactions; private String _createdBy; private Date _creationDate; public Account(String anOwnerName) { _id = ++_SEQUENCE; _ownerName = anOwnerName; _isActive = true; _balance = 0.0d; _transactions = new ArrayList<AccountTransaction>(); _createdBy = "unknown"; _creationDate = new Date(); } /** * Returns the id attribute. * * @return The id value. */ public long getId() { return _id; } /** * Returns the ownerName attribute. * * @return The ownerName value. */ public String getOwnerName() { return _ownerName; } /** * Returns the isActive attribute. * * @return The isActive value. */ public boolean isActive() { return _isActive; } /** * Changes the value of the attributes isActive. * * @param aIsActive The new value of the isActive attribute. */ public void setActive(boolean aIsActive) { _isActive = aIsActive; } /** * Changes the value of the attributes ownerName. * * @param aOwnerName The new value of the ownerName attribute. */ public void setOwnerName(String aOwnerName) { _ownerName = aOwnerName; } /** * Returns the balance attribute. * * @return The balance value. */ public double getBalance() { return _balance; } /** * Returns the transactions attribute. * * @return The transactions value. */ public List<AccountTransaction> getTransactions() { return _transactions; } protected void applyTransaction(AccountTransaction aTransaction) throws NotEnoughFundsException, InactiveAccountException { if (!_isActive) { throw new InactiveAccountException("Unable to apply " + aTransaction.getType() + " of amount " + aTransaction.getAmount() + " to account " + _id); } synchronized (_transactions) { if (AccountTransaction.TransactionType.DEPOSIT == aTransaction.getType()) { _transactions.add(aTransaction); _balance += aTransaction.getAmount(); } else if (AccountTransaction.TransactionType.WITHDRAWAL == aTransaction.getType()) { if (_balance < aTransaction.getAmount()) { throw new NotEnoughFundsException("Unable to withdraw " + aTransaction.getAmount() + "$ from account " + _id + " - current balance is " + _balance); } _transactions.add(aTransaction); _balance -= aTransaction.getAmount(); } else { throw new IllegalArgumentException("The transaction passed in has an invalid type: " + aTransaction.getType()); } } } /** * Changes the value of the attributes createdBy. * * @param aCreatedBy The new value of the createdBy attribute. */ protected void setCreatedBy(String aCreatedBy) { _createdBy = aCreatedBy; } /** * Returns the createdBy attribute. * * @return The createdBy value. */ public String getCreatedBy() { return _createdBy; } /** * Returns the creationDate attribute. * * @return The creationDate value. */ public Date getCreationDate() { return _creationDate; } /* (non-Javadoc) * @see java.lang.Object#toString() */ public String toString() { return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE). append("id", _id). append("ownerName", _ownerName). append("isActive", _isActive). append("balance", _balance). append("tx.count", _transactions.size()). append("createdBy", _createdBy). append("creationDate", new Timestamp(_creationDate.getTime())). toString(); } }
AccountNotFoundException,賬號不存在異常
package org.apache.shiro.samples.aspectj.bank; public class AccountNotFoundException extends BankServiceException { public AccountNotFoundException(String aMessage) { super(aMessage); } }
AccountTransaction,賬號轉入與轉出
package org.apache.shiro.samples.aspectj.bank; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import java.sql.Timestamp; import java.util.Date; public class AccountTransaction { private static long _SEQUENCE; public enum TransactionType { DEPOSIT, WITHDRAWAL } private long _id; private TransactionType _type; private long _accountId; private double _amount; private String _createdBy; private Date _creationDate; public static AccountTransaction createDepositTx(long anAccountId, double anAmount) { return new AccountTransaction(TransactionType.DEPOSIT, anAccountId, anAmount); } public static AccountTransaction createWithdrawalTx(long anAccountId, double anAmount) { return new AccountTransaction(TransactionType.WITHDRAWAL, anAccountId, anAmount); } private AccountTransaction(TransactionType aType, long anAccountId, double anAmount) { _id = ++_SEQUENCE; _type = aType; _accountId = anAccountId; _amount = anAmount; _createdBy = "unknown"; _creationDate = new Date(); } /** * Returns the id attribute. * * @return The id value. */ public long getId() { return _id; } /** * Returns the type attribute. * * @return The type value. */ public TransactionType getType() { return _type; } /** * Returns the accountId attribute. * * @return The accountId value. */ public long getAccountId() { return _accountId; } /** * Returns the amount attribute. * * @return The amount value. */ public double getAmount() { return _amount; } /** * Changes the value of the attributes createdBy. * * @param aCreatedBy The new value of the createdBy attribute. */ protected void setCreatedBy(String aCreatedBy) { _createdBy = aCreatedBy; } /** * Returns the createdBy attribute. * * @return The createdBy value. */ public String getCreatedBy() { return _createdBy; } /** * Returns the creationDate attribute. * * @return The creationDate value. */ public Date getCreationDate() { return _creationDate; } /* (non-Javadoc) * @see java.lang.Object#toString() */ public String toString() { return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE). append("id", _id). append("type", _type). append("accountId", _accountId). append("amount", _amount). append("createdBy", _createdBy). append("creationDate", new Timestamp(_creationDate.getTime())). toString(); } }
BankServerRunner,銀行服務運行
package org.apache.shiro.samples.aspectj.bank; public class BankServerRunner { private SecureBankService _bankService; public synchronized void start() throws Exception { if (_bankService == null) { _bankService = new SecureBankService(); _bankService.start(); } } public synchronized void stop() { if (_bankService != null) { try { _bankService.dispose(); } finally { _bankService = null; } } } public BankService getBankService() { return _bankService; } public static void main(String[] args) { try { BankServerRunner server = new BankServerRunner(); server.start(); server.stop(); } catch (Exception e) { e.printStackTrace(); } } }
BankService,銀行服務接口
package org.apache.shiro.samples.aspectj.bank; import java.util.Date; public interface BankService { public long[] searchAccountIdsByOwner(String anOwnerName); public long createNewAccount(String anOwnerName); public double getBalanceOf(long anAccountId) throws AccountNotFoundException; public String getOwnerOf(long anAccountId) throws AccountNotFoundException; public double depositInto(long anAccountId, double anAmount) throws AccountNotFoundException, InactiveAccountException; public double withdrawFrom(long anAccountId, double anAmount) throws AccountNotFoundException, NotEnoughFundsException, InactiveAccountException; public TxLog[] getTxHistoryFor(long anAccountId) throws AccountNotFoundException; public double closeAccount(long anAccountId) throws AccountNotFoundException, InactiveAccountException; public boolean isAccountActive(long anAccountId) throws AccountNotFoundException; public static class TxLog { private Date _creationDate; private double _amount; private String _madeBy; public TxLog(Date aCreationDate, double aAmount, String aMadeBy) { super(); _creationDate = aCreationDate; _amount = aAmount; _madeBy = aMadeBy; } /** * Returns the creationDate attribute. * * @return The creationDate value. */ public Date getCreationDate() { return _creationDate; } /** * Returns the amount attribute. * * @return The amount value. */ public double getAmount() { return _amount; } /** * Returns the madeBy attribute. * * @return The madeBy value. */ public String getMadeBy() { return _madeBy; } } }
BankServiceException,銀行服務異常
package org.apache.shiro.samples.aspectj.bank; public class BankServiceException extends Exception { public BankServiceException(String aMessage) { super(aMessage); } public BankServiceException(String aMessage, Throwable aCause) { super(aMessage, aCause); } }
InactiveAccountException,存入賬戶異常
package org.apache.shiro.samples.aspectj.bank; public class InactiveAccountException extends BankServiceException { public InactiveAccountException(String aMessage) { super(aMessage); } }
NotEnoughFundsException,賬戶不足異常
package org.apache.shiro.samples.aspectj.bank; public class NotEnoughFundsException extends BankServiceException { public NotEnoughFundsException(String aMessage) { super(aMessage); } }
SecureBankService,安全銀行的服務類,處理各種銀行的業務
package org.apache.shiro.samples.aspectj.bank; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.samples.aspectj.bank.AccountTransaction.TransactionType; import org.apache.shiro.subject.Subject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class SecureBankService implements BankService { private static final Logger log = LoggerFactory.getLogger(SecureBankService.class); private volatile boolean _isRunning; private final List<Account> _accounts; private Map<Long, Account> _accountsById; /** * Creates a new {@link SecureBankService} instance. */ public SecureBankService() { _accounts = new ArrayList<Account>(); _accountsById = new HashMap<Long, Account>(); } /** * Starts this service */ public void start() throws Exception { _isRunning = true; log.info("銀行服務開始..."); } /** * Stop this service */ public void dispose() { log.info("銀行服務停止..."); _isRunning = false; synchronized (_accounts) { _accountsById.clear(); _accounts.clear(); } log.info("銀行服務停止!"); } /** * Internal utility method that validate the internal state of this service. */ protected void assertServiceState() { if (!_isRunning) { throw new IllegalStateException("銀行的服務沒有開始"); } } public int getAccountCount() { return _accounts.size(); } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#createNewAccount(java.lang.String) */ @RequiresPermissions("bankAccount:create") public long createNewAccount(String anOwnerName) { assertServiceState(); log.info("創建新的賬戶給 " + anOwnerName); synchronized (_accounts) { Account account = new Account(anOwnerName); account.setCreatedBy(getCurrentUsername()); _accounts.add(account); _accountsById.put(account.getId(), account); log.debug("創建新的賬戶: " + account); return account.getId(); } } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#searchAccountIdsByOwner(java.lang.String) */ public long[] searchAccountIdsByOwner(String anOwnerName) { assertServiceState(); log.info("查找已經存在的銀行賬戶為 " + anOwnerName); ArrayList<Account> matchAccounts = new ArrayList<Account>(); synchronized (_accounts) { for (Account a : _accounts) { if (a.getOwnerName().toLowerCase().contains(anOwnerName.toLowerCase())) { matchAccounts.add(a); } } } long[] accountIds = new long[matchAccounts.size()]; int index = 0; for (Account a : matchAccounts) { accountIds[index++] = a.getId(); } log.debug("找到 " + accountIds.length + " 相匹配的賬戶的名稱 " + anOwnerName); return accountIds; } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#getOwnerOf(long) */ @RequiresPermissions("bankAccount:read") public String getOwnerOf(long anAccountId) throws AccountNotFoundException { assertServiceState(); log.info("獲得銀行賬戶的所有者 " + anAccountId); Account a = safellyRetrieveAccountForId(anAccountId); return a.getOwnerName(); } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#getBalanceOf(long) */ @RequiresPermissions("bankAccount:read") public double getBalanceOf(long anAccountId) throws AccountNotFoundException { assertServiceState(); log.info("得到賬戶的余額 " + anAccountId); Account a = safellyRetrieveAccountForId(anAccountId); return a.getBalance(); } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#depositInto(long, double) */ @RequiresPermissions("bankAccount:operate") public double depositInto(long anAccountId, double anAmount) throws AccountNotFoundException, InactiveAccountException { assertServiceState(); log.info("存錢到 " + anAmount + " 這個賬戶 " + anAccountId); try { Account a = safellyRetrieveAccountForId(anAccountId); AccountTransaction tx = AccountTransaction.createDepositTx(anAccountId, anAmount); tx.setCreatedBy(getCurrentUsername()); log.debug("創建一個新的交易 " + tx); a.applyTransaction(tx); log.debug("新的賬戶余額 " + a.getId() + " 存款后 " + a.getBalance()); return a.getBalance(); } catch (NotEnoughFundsException nefe) { throw new IllegalStateException("應該從未發生過", nefe); } } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#withdrawFrom(long, double) */ @RequiresPermissions("bankAccount:operate") public double withdrawFrom(long anAccountId, double anAmount) throws AccountNotFoundException, NotEnoughFundsException, InactiveAccountException { assertServiceState(); log.info("取款 " + anAmount + " 從賬戶 " + anAccountId); Account a = safellyRetrieveAccountForId(anAccountId); AccountTransaction tx = AccountTransaction.createWithdrawalTx(anAccountId, anAmount); tx.setCreatedBy(getCurrentUsername()); log.debug("創建一個新的交易 " + tx); a.applyTransaction(tx); log.debug("新的賬戶余額 " + a.getId() + " 取款后 " + a.getBalance()); return a.getBalance(); } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#getTxHistoryFor(long) */ @RequiresPermissions("bankAccount:read") public TxLog[] getTxHistoryFor(long anAccountId) throws AccountNotFoundException { assertServiceState(); log.info("獲取賬戶交易 " + anAccountId); Account a = safellyRetrieveAccountForId(anAccountId); TxLog[] txs = new TxLog[a.getTransactions().size()]; int index = 0; for (AccountTransaction tx : a.getTransactions()) { log.debug("查過交易 " + tx); if (TransactionType.DEPOSIT == tx.getType()) { txs[index++] = new TxLog(tx.getCreationDate(), tx.getAmount(), tx.getCreatedBy()); } else { txs[index++] = new TxLog(tx.getCreationDate(), -1.0d * tx.getAmount(), tx.getCreatedBy()); } } return txs; } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#closeAccount(long) */ @RequiresPermissions("bankAccount:close") public double closeAccount(long anAccountId) throws AccountNotFoundException, InactiveAccountException { assertServiceState(); log.info("截止賬戶 " + anAccountId); Account a = safellyRetrieveAccountForId(anAccountId); if (!a.isActive()) { throw new InactiveAccountException("這個賬戶 " + anAccountId + " 已經關閉"); } try { AccountTransaction tx = AccountTransaction.createWithdrawalTx(a.getId(), a.getBalance()); tx.setCreatedBy(getCurrentUsername()); log.debug("創建一個新的交易 " + tx); a.applyTransaction(tx); a.setActive(false); log.debug("賬戶 " + a.getId() + " 現在是關閉的 " + tx.getAmount() + " 針對這個業主"); return tx.getAmount(); } catch (NotEnoughFundsException nefe) { throw new IllegalStateException("應該從來不發生", nefe); } } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#isAccountActive(long) */ @RequiresPermissions("bankAccount:read") public boolean isAccountActive(long anAccountId) throws AccountNotFoundException { assertServiceState(); log.info("獲取賬戶的活動狀態 " + anAccountId); Account a = safellyRetrieveAccountForId(anAccountId); return a.isActive(); } /** * Internal method that safelly (concurrency-wise) retrieves an account from the id passed in. * * @param anAccountId The identifier of the account to retrieve. * @return The account instance retrieved. * @throws AccountNotFoundException If no account is found for the provided identifier. */ protected Account safellyRetrieveAccountForId(long anAccountId) throws AccountNotFoundException { Account account = null; synchronized (_accounts) { account = _accountsById.get(anAccountId); } if (account == null) { throw new AccountNotFoundException("沒有找到ID為 " + anAccountId + " 的賬戶"); } log.info("檢查賬戶 " + account); return account; } /** * Internal utility method to retrieve the username of the current authenticated user. * * @return The name. */ protected String getCurrentUsername() { Subject subject = SecurityUtils.getSubject(); if (subject == null || subject.getPrincipal() == null || !subject.isAuthenticated()) { throw new IllegalStateException("無法檢索當前驗證的主題"); } return SecurityUtils.getSubject().getPrincipal().toString(); } }
在配置文件中配置了三組賬戶
[users] root = secret, admin sally = 1234, superviser dan = 123, user
用戶 root 密碼secret 角色admin
用戶 sally 密碼1234 角色superviser
用戶 dan密碼123 角色user
角色信息包括
[roles] admin = bankAccount:* superviser = bankAccount:create, bankAccount:read bankAccount:close user = bankAccount:create, bankAccount:read, bankAccount:operate
包括種種操作的權限分配
使用junit測試
@BeforeClass public static void setUpClass() throws Exception { BasicConfigurator.resetConfiguration(); BasicConfigurator.configure(); logger = Logger.getLogger(SecureBankServiceTest.class.getSimpleName()); Factory<SecurityManager> factory = new IniSecurityManagerFactory( "classpath:shiroBankServiceTest.ini"); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); service = new SecureBankService(); service.start(); }
加載對應的ini中的信息,在每次運行之前
登錄用戶的操作方法
// 作為用戶登錄,不能關閉賬戶 protected void loginAsUser() { if (_subject == null) { _subject = SecurityUtils.getSubject(); } // use dan to run as a normal user (which cannot close an account) _subject.login(new UsernamePasswordToken("dan", "123")); } // 作為超級用戶登錄,不能操作賬戶 protected void loginAsSuperviser() { if (_subject == null) { _subject = SecurityUtils.getSubject(); } // use sally to run as a superviser (which cannot operate an account) _subject.login(new UsernamePasswordToken("sally", "1234")); }
給張三創建賬戶,並且檢查賬戶的情況
@Test public void testCreateAccount() throws Exception { loginAsUser(); createAndValidateAccountFor("張三"); } protected long createAndValidateAccountFor(String anOwner) throws Exception { long createdId = service.createNewAccount(anOwner); assertAccount(anOwner, true, 0.0d, 0, createdId); return createdId; } public static void assertAccount(String eOwnerName, boolean eIsActive, double eBalance, int eTxLogCount, long actualAccountId) throws Exception { Assert.assertEquals(eOwnerName, service.getOwnerOf(actualAccountId)); Assert.assertEquals(eIsActive, service.isAccountActive(actualAccountId)); Assert.assertEquals(eBalance, service.getBalanceOf(actualAccountId)); Assert.assertEquals(eTxLogCount, service.getTxHistoryFor(actualAccountId).length); }
看打印出來的信息
1 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini] 10 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users] 12 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles] 13 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles] 46 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing... 48 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing... 59 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始... 62 [main] INFO SecureBankServiceTest - ######################### ### 開始測試用例 1 120 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String] 120 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison 121 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan] 121 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 121 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 122 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance. 123 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler... 132 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null] 188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 張三 203 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=張三,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:40:26.71] 203 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 203 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=張三,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:40:26.71] 205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=張三,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:40:26.71] 205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=張三,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:40:26.71] 205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=張三,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:40:26.71] 206 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan 206 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [66208001-e91d-4625-938f-1b1c08b2645c] 208 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止... 208 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!
創建張三的用戶信息並且檢查張三的賬戶的情況。
第二個測試
創建賬戶李四並且存入250到賬戶里,用戶有創建和操作的權限,所以可以操作
@Test public void testDepositInto_singleTx() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("李四"); makeDepositAndValidateAccount(accountId, 250.00d, "李四"); }
protected double makeDepositAndValidateAccount(long anAccountId, double anAmount, String eOwnerName) throws Exception { double previousBalance = service.getBalanceOf(anAccountId); int previousTxCount = service.getTxHistoryFor(anAccountId).length; double newBalance = service.depositInto(anAccountId, anAmount); Assert.assertEquals(previousBalance + anAmount, newBalance); assertAccount(eOwnerName, true, newBalance, 1 + previousTxCount, anAccountId); return newBalance; }
運行后的結果
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini] 9 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users] 12 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles] 13 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles] 44 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing... 46 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing... 56 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始... 59 [main] INFO SecureBankServiceTest - ######################### ### 開始測試用例 1 115 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String] 115 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison 115 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan] 115 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 116 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 116 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance. 117 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler... 124 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null] 171 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 李四 188 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991] 188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991] 190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991] 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991] 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991] 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991] 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991] 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 250.0 這個賬戶 1 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991] 196 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=250.0,createdBy=dan,creationDate=2011-09-12 20:44:15.013] 196 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 250.0 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=250.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:44:14.991] 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=250.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:44:14.991] 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=250.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:44:14.991] 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=250.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:44:14.991] 196 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=250.0,createdBy=dan,creationDate=2011-09-12 20:44:15.013] 196 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan 197 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [3b53dc16-67d5-4730-ae8a-872d113c7546] 198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止... 198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!
創建賬戶王五並且存入多筆款項
@Test public void testDepositInto_multiTxs() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("王五"); makeDepositAndValidateAccount(accountId, 50.00d, "王五"); makeDepositAndValidateAccount(accountId, 300.00d, "王五"); makeDepositAndValidateAccount(accountId, 85.00d, "王五"); assertAccount("王五", true, 435.00d, 3, accountId); }
一共存入三筆,最后得到的數據的總和為435
看日志打出來的信息
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini] 10 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users] 12 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles] 13 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles] 46 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing... 49 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing... 59 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始... 62 [main] INFO SecureBankServiceTest - ######################### ### 開始測試用例 1 118 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String] 118 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison 119 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan] 119 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 119 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 120 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance. 121 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler... 129 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null] 189 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 王五 204 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 204 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 204 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 50.0 這個賬戶 1 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 210 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 20:52:54.739] 210 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 50.0 210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 210 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 20:52:54.739] 210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 211 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 20:52:54.739] 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 300.0 這個賬戶 1 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 211 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=2,type=DEPOSIT,accountId=1,amount=300.0,createdBy=dan,creationDate=2011-09-12 20:52:54.741] 211 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 350.0 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=350.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=350.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=350.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=350.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 20:52:54.739] 212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=DEPOSIT,accountId=1,amount=300.0,createdBy=dan,creationDate=2011-09-12 20:52:54.741] 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=350.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=350.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 20:52:54.739] 212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=DEPOSIT,accountId=1,amount=300.0,createdBy=dan,creationDate=2011-09-12 20:52:54.741] 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 85.0 這個賬戶 1 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=350.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=3,type=DEPOSIT,accountId=1,amount=85.0,createdBy=dan,creationDate=2011-09-12 20:52:54.742] 212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 435.0 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 20:52:54.739] 213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=DEPOSIT,accountId=1,amount=300.0,createdBy=dan,creationDate=2011-09-12 20:52:54.741] 213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=3,type=DEPOSIT,accountId=1,amount=85.0,createdBy=dan,creationDate=2011-09-12 20:52:54.742] 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72] 214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 20:52:54.739] 214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=DEPOSIT,accountId=1,amount=300.0,createdBy=dan,creationDate=2011-09-12 20:52:54.741] 214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=3,type=DEPOSIT,accountId=1,amount=85.0,createdBy=dan,creationDate=2011-09-12 20:52:54.742] 214 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan 214 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [1d66c2ec-a668-478a-8f30-e3c65f80a16d] 216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止... 216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!
創建賬戶賈六並且取款,因為賬戶為空所以會拋出異常
@Test(expected = NotEnoughFundsException.class) public void testWithdrawFrom_emptyAccount() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("賈六"); service.withdrawFrom(accountId, 100.00d); }
看執行的結果
1 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini] 11 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users] 13 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles] 15 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles] 46 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing... 50 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing... 60 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始... 63 [main] INFO SecureBankServiceTest - ######################### ### 開始測試用例 1 126 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String] 126 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison 128 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan] 128 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 129 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 130 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance. 132 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler... 145 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null] 190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 賈六 205 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=賈六,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:56:05.029] 205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=賈六,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:56:05.029] 206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=賈六,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:56:05.029] 206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=賈六,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:56:05.029] 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=賈六,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:56:05.029] 208 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 取款 100.0 從賬戶 1 208 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=賈六,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:56:05.029] 210 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 20:56:05.047] 210 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan 210 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [05f3559d-d0c4-458c-a220-31389550576f] 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止... 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!
得到期望的NotEnoughFundsException,運行通過
然后創建賬戶周七,先存入50,然后取100,結果得到的與面相同,余額不足異常
@Test(expected = NotEnoughFundsException.class) public void testWithdrawFrom_notEnoughFunds() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("周七"); makeDepositAndValidateAccount(accountId, 50.00d, "周七"); service.withdrawFrom(accountId, 100.00d); }
看打印出的日志信息
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini] 10 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users] 12 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles] 13 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles] 44 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing... 48 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing... 59 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始... 61 [main] INFO SecureBankServiceTest - ######################### ### 開始測試用例 1 118 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String] 118 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison 119 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan] 119 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 119 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 120 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance. 121 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler... 131 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null] 179 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 周七 196 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936] 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936] 197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936] 197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936] 197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936] 198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936] 198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936] 198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 50.0 這個賬戶 1 199 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936] 200 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 21:01:30.955] 200 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 50.0 200 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:01:30.936] 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:01:30.936] 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:01:30.936] 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:01:30.936] 201 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 21:01:30.955] 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 取款 100.0 從賬戶 1 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:01:30.936] 201 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:01:30.956] 202 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan 202 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [a85a89c7-a805-4086-bd5b-109a0d54086c] 203 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止... 203 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!
再測試先存后取,先存入500,后取100,最后得到的結果為400
@Test public void testWithdrawFrom_singleTx() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("國八"); makeDepositAndValidateAccount(accountId, 500.00d, "國八"); makeWithdrawalAndValidateAccount(accountId, 100.00d, "國八"); assertAccount("國八", true, 400.00d, 2, accountId); }
看打印出的結果
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini] 9 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users] 10 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles] 11 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles] 43 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing... 45 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing... 55 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始... 59 [main] INFO SecureBankServiceTest - ######################### ### 開始測試用例 1 115 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String] 115 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison 116 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan] 116 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 116 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 116 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance. 117 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler... 124 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null] 168 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 國八 185 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 186 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 186 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 187 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 187 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 187 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 187 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 187 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 187 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 500.0 這個賬戶 1 188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 190 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:03:17.103] 190 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 500.0 190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 191 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:03:17.103] 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 191 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:03:17.103] 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 取款 100.0 從賬戶 1 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 192 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:03:17.104] 192 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 取款后 400.0 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 192 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:03:17.103] 192 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:03:17.104] 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085] 193 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:03:17.103] 193 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:03:17.104] 193 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan 193 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [192dddd6-7090-435c-bb65-b3b64a73d667] 195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止... 195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!
存入一筆取多筆
@Test public void testWithdrawFrom_manyTxs() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("Zoe Smith"); makeDepositAndValidateAccount(accountId, 500.00d, "Zoe Smith"); makeWithdrawalAndValidateAccount(accountId, 100.00d, "Zoe Smith"); makeWithdrawalAndValidateAccount(accountId, 75.00d, "Zoe Smith"); makeWithdrawalAndValidateAccount(accountId, 125.00d, "Zoe Smith"); assertAccount("Zoe Smith", true, 200.00d, 4, accountId); }
查看打印的日志信息
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini] 9 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users] 11 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles] 13 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles] 53 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing... 57 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing... 72 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始... 76 [main] INFO SecureBankServiceTest - ######################### ### 開始測試用例 1 132 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String] 132 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison 133 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan] 133 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 133 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 134 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance. 135 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler... 143 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null] 186 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 Zoe Smith 205 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 208 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 500.0 這個賬戶 1 209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337] 212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 500.0 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337] 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337] 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 取款 100.0 從賬戶 1 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:04:28.338] 213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 取款后 400.0 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337] 214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:04:28.338] 214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337] 214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:04:28.338] 214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 取款 75.0 從賬戶 1 214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 215 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=3,type=WITHDRAWAL,accountId=1,amount=75.0,createdBy=dan,creationDate=2011-09-12 21:04:28.339] 215 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 取款后 325.0 215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=325.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=325.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=325.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=325.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 215 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337] 215 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:04:28.338] 215 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=3,type=WITHDRAWAL,accountId=1,amount=75.0,createdBy=dan,creationDate=2011-09-12 21:04:28.339] 215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=325.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=325.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 216 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337] 216 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:04:28.338] 216 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=3,type=WITHDRAWAL,accountId=1,amount=75.0,createdBy=dan,creationDate=2011-09-12 21:04:28.339] 216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 取款 125.0 從賬戶 1 216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=325.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 216 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=4,type=WITHDRAWAL,accountId=1,amount=125.0,createdBy=dan,creationDate=2011-09-12 21:04:28.341] 216 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 取款后 200.0 216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 216 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337] 217 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:04:28.338] 217 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=3,type=WITHDRAWAL,accountId=1,amount=75.0,createdBy=dan,creationDate=2011-09-12 21:04:28.339] 217 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=4,type=WITHDRAWAL,accountId=1,amount=125.0,createdBy=dan,creationDate=2011-09-12 21:04:28.341] 217 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 217 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 217 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 217 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 217 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 220 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 220 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 221 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312] 221 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337] 221 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:04:28.338] 221 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=3,type=WITHDRAWAL,accountId=1,amount=75.0,createdBy=dan,creationDate=2011-09-12 21:04:28.339] 221 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=4,type=WITHDRAWAL,accountId=1,amount=125.0,createdBy=dan,creationDate=2011-09-12 21:04:28.341] 221 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan 221 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [1ecbe8f2-f2f5-468b-af2b-d82d6b1267fa] 223 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止... 223 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!
存多少取多少
@Test public void testWithdrawFrom_upToZero() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("Zoe Smith"); makeDepositAndValidateAccount(accountId, 500.00d, "Zoe Smith"); makeWithdrawalAndValidateAccount(accountId, 500.00d, "Zoe Smith"); assertAccount("Zoe Smith", true, 0.00d, 2, accountId); }
查看打印的日志信息
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini] 9 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users] 11 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles] 12 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles] 43 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing... 45 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing... 55 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始... 58 [main] INFO SecureBankServiceTest - ######################### ### 開始測試用例 1 114 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String] 114 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison 114 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan] 115 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 115 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 115 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance. 116 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler... 125 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null] 168 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 Zoe Smith 186 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 186 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 187 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 189 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 189 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 189 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 189 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 500.0 這個賬戶 1 190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 192 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.804] 192 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 500.0 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 192 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.804] 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 193 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.804] 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 取款 500.0 從賬戶 1 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 193 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.806] 193 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 取款后 0.0 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 193 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.804] 193 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.806] 194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783] 194 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.804] 194 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.806] 194 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan 195 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [12aeb47c-f3c1-46c1-baec-78da03762422] 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止... 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!
關閉賬戶余額為0的賬戶,普通用戶沒有權限,所以需要轉到另外一個角色的賬戶進行操作
@Test public void testCloseAccount_zeroBalance() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("Chris Smith"); logoutCurrentSubject(); loginAsSuperviser(); double closingBalance = service.closeAccount(accountId); Assert.assertEquals(0.00d, closingBalance); assertAccount("Chris Smith", false, 0.00d, 1, accountId); }
查看打印出來的日志信息
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini] 11 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users] 13 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles] 14 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles] 47 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing... 50 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing... 61 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始... 63 [main] INFO SecureBankServiceTest - ######################### ### 開始測試用例 1 121 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String] 121 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison 121 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan] 122 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 122 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 123 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance. 124 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler... 133 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null] 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 Chris Smith 207 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:13:04.496] 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:13:04.496] 208 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:13:04.496] 209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:13:04.496] 209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:13:04.496] 210 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan 210 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [c4adc0a6-987c-4c94-ad38-d13f683c7f1d] 211 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String] 211 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison 211 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - sally, rememberMe=false]. Returned account [sally] 211 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 211 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 211 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null] 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 截止賬戶 1 211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:13:04.496] 213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=WITHDRAWAL,accountId=1,amount=0.0,createdBy=sally,creationDate=2011-09-12 21:13:04.516] 213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 賬戶 1 現在是關閉的 0.0 針對這個業主 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:13:04.496] 213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:13:04.496] 214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:13:04.496] 214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:13:04.496] 214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=WITHDRAWAL,accountId=1,amount=0.0,createdBy=sally,creationDate=2011-09-12 21:13:04.516] 214 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}sally 214 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [f0988257-3441-489a-859c-538043ead6e3] 215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止... 215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!
創建用戶並且存入350,然后對這個用戶進行關閉操作
@Test public void testCloseAccount_withBalance() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("Gerry Smith"); makeDepositAndValidateAccount(accountId, 385.00d, "Gerry Smith"); logoutCurrentSubject(); loginAsSuperviser(); double closingBalance = service.closeAccount(accountId); Assert.assertEquals(385.00d, closingBalance); assertAccount("Gerry Smith", false, 0.00d, 2, accountId); }
查看打印的日志信息
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini] 9 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users] 11 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles] 12 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles] 46 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing... 48 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing... 58 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始... 61 [main] INFO SecureBankServiceTest - ######################### ### 開始測試用例 1 117 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String] 117 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison 118 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan] 118 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 118 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 118 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance. 119 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler... 128 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null] 173 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 Gerry Smith 190 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 385.0 這個賬戶 1 193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 195 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=385.0,createdBy=dan,creationDate=2011-09-12 21:17:58.672] 195 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 385.0 195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=385.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=385.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=385.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=385.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 196 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=385.0,createdBy=dan,creationDate=2011-09-12 21:17:58.672] 196 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan 196 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [b2e689a3-cd4a-4785-962b-0df77758533b] 197 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String] 197 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison 197 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - sally, rememberMe=false]. Returned account [sally] 197 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 197 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 197 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null] 197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 截止賬戶 1 197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=385.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 197 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=385.0,createdBy=sally,creationDate=2011-09-12 21:17:58.674] 197 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 賬戶 1 現在是關閉的 385.0 針對這個業主 197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=false,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=false,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=false,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=false,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:17:58.652] 198 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=385.0,createdBy=dan,creationDate=2011-09-12 21:17:58.672] 198 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=385.0,createdBy=sally,creationDate=2011-09-12 21:17:58.674] 198 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}sally 198 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [6ffa0d67-7510-4205-9fa8-01b6bb9793f5] 199 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止... 199 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!
創建用戶並且關閉正活動的賬戶
@Test(expected = InactiveAccountException.class) public void testCloseAccount_alreadyClosed() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("Chris Smith"); logoutCurrentSubject(); loginAsSuperviser(); double closingBalance = service.closeAccount(accountId); Assert.assertEquals(0.00d, closingBalance); assertAccount("Chris Smith", false, 0.00d, 1, accountId); service.closeAccount(accountId); }
查看打印的日志信息
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini] 9 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users] 12 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles] 13 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles] 44 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing... 47 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing... 57 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始... 60 [main] INFO SecureBankServiceTest - ######################### ### 開始測試用例 1 117 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String] 117 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison 117 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan] 118 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 118 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 119 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance. 120 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler... 127 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null] 178 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 Chris Smith 195 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:19:53.755] 195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:19:53.755] 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:19:53.755] 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:19:53.755] 197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:19:53.755] 198 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan 198 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [8ff8f7c8-5d03-4e4f-b47d-0414cd43111d] 198 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String] 198 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison 199 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - sally, rememberMe=false]. Returned account [sally] 199 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 199 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup. 199 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null] 199 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 截止賬戶 1 199 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:19:53.755] 201 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=WITHDRAWAL,accountId=1,amount=0.0,createdBy=sally,creationDate=2011-09-12 21:19:53.777] 201 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 賬戶 1 現在是關閉的 0.0 針對這個業主 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:19:53.755] 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:19:53.755] 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:19:53.755] 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1 201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:19:53.755] 201 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=WITHDRAWAL,accountId=1,amount=0.0,createdBy=sally,creationDate=2011-09-12 21:19:53.777] 202 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 截止賬戶 1 202 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:19:53.755] 202 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}sally 202 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [53286615-5b71-4642-b3e8-916fb77fba60] 203 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止... 203 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!
其中判斷權限使用的是annocation的方式
@RequiresPermissions("bankAccount:create") 是否有用戶創建權限
@RequiresPermissions("bankAccount:read") 讀權限
@RequiresPermissions("bankAccount:operate") 操作權限
@RequiresPermissions("bankAccount:close") 關閉權限
根據以上幾個標簽就可以得到對應的權限信息
我們首先了解下什么是shiro ,Shiro 是 JAVA 世界中新近出現的權限框架,較之 JAAS 和 Spring Security,Shiro 在保持強大功能的同時,還在簡單性和靈活性方面擁有巨大優勢 Shiro 是一個強大而靈活的開源安全框架,能夠非常清晰的處理認證、授權、管理會話以及密碼加密。如下是它所具有的特點:
易於理解的 Java Security API;
簡單的身份認證(登錄),支持多種數據源(LDAP,JDBC,Kerberos,ActiveDirectory 等);
對角色的簡單的簽權(訪問控制),支持細粒度的簽權;
支持一級緩存,以提升應用程序的性能;
內置的基於 POJO 企業會話管理,適用於 Web 以及非 Web 的環境;
異構客戶端會話訪問;
非常簡單的加密 API;
不跟任何的框架或者容器捆綁,可以獨立運行。
Shiro 主要有四個組件
SecurityManager典型的 Facade,Shiro 通過它對外提供安全管理的各種服務。
Authenticator對“Who are you ?”進行核實。通常涉及用戶名和密碼。這 個組件負責收集 principals 和 credentials,並將它們提交給應用系統。如果提交的 credentials 跟應用系統中提供的 credentials 吻合,就能夠繼續訪問,否則需要重新提交 principals 和 credentials,或者直接終止訪問。
Authorizer身 份份驗證通過后,由這個組件對登錄人員進行訪問控制的篩查,比如“who can do what”, 或者“who can do which actions”。Shiro 采用“基於 Realm”的方法,即用戶(又稱 Subject)、用戶組、角色和 permission 的聚合體。
Session Manager這個組件保證了異構客戶端的訪問,配置簡單。它是基於 POJO/J2SE 的,不跟任何的客戶端或者協議綁定。
Shiro 的認證和簽權可以通過 JDBC、LDAP 或者 Active Directory 來訪問數據庫、目錄服務器或者 Active Directory 中的人員以及認證 / 簽權信息。SessionManager 通過會話 DAO 可以將會話保存在 cache 中,或者固化到數據庫或文件系統中。 簡介apache shiro 是一個功能強大和易於使用的Java安全框架,為開發人員提供一個直觀而全面的的解決方案的認證,授權,加密,會話管理。在實際應用中,它實現了應用程序的安全管理的各個方面。 shiro的功能



// get the currently executing user: Subject currentUser = SecurityUtils.getSubject();使用SecurityUtils.getSubject(),我們可以得到當前正在執行的主題。得到主題之后,你可以得到他對應的會話信息
// Do some stuff with a Session (no need for a web or EJB container!!!) 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 + "]"); }你可以得到http的session信息,也可以在非web環境中使用,得到相對應的會話信息。如果在web應用程序中部署應用,默認情況下,應用將以HttpSession為基礎。在企業級應用中,你在多個應用中可以使用相同的API,無論部署環境。而且使用任何客戶端技術你都可以共享會話數據。接下來判斷登錄信息
// let's login the current user so we can check against roles and permissions: if (!currentUser.isAuthenticated()) { UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa"); token.setRememberMe(true); try { currentUser.login(token); } catch (UnknownAccountException uae) { log.info("There is no user with username of " + token.getPrincipal()); } catch (IncorrectCredentialsException ice) { log.info("Password for account " + token.getPrincipal() + " was incorrect!"); } 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? } }如果正確可以向下執行,如果不正確,就會對不同的業務進行處理。比如用戶名不正確,密碼不正確,用戶被鎖定的異常,當然也可以使用自定義拋出的異常。如果登錄成功,那么下一步可以做什么呢?提示當前用戶:
/

//test a role: if (currentUser.hasRole("schwartz")) { log.info("May the Schwartz be with you!"); } else { log.info("Hello, mere mortal."); }接着測試是否有特定的權限
//test a typed permission (not instance-level) 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("winnebago:drive:eagle5")) { 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! currentUser.logout();認證就是用戶確認身份的過程,確認登錄的用戶身份能夠操作的內容。使用shiro認證分為以下幾個步驟:1,得到主體的認證和憑據。
view sourceprint?
// let's login the current user so we can check against roles and permissions:
if(!currentUser.isAuthenticated()) {
UsernamePasswordToken token =newUsernamePasswordToken("lonestarr","vespa");
token.setRememberMe(true);
2,提交認證和憑據給身份驗證系統。
view sourceprint?
Subject currentUser = SecurityUtils.getSubject();
currentUser.login(token);
3,判斷是否允許訪問,重試認證或者阻止訪問。
view sourceprint?
try{
currentUser.login(token);
}catch(UnknownAccountException uae) {
log.info("There is no user with username of "+ token.getPrincipal());
}catch(IncorrectCredentialsException ice) {
log.info("Password for account "+ token.getPrincipal() +" was incorrect!");
}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?
}
其中Remember Me的功能包括兩個方法,一個是isRememberedbooleanisRemembered()非匿名登錄的用戶可以記住上次使用的主題的信息。isAuthenticatedbooleanisAuthenticated()在此期間需要使用有效的憑據登錄系統,否則值為false. 授權操作授權的例子就是是否可以訪問某個頁面,可以操作某個按鈕,是否可以編緝對應的數據等。如何在shiro中使用授權1,使用編程方式判斷是否有管理員角色
view sourceprint?
if(currentUser.hasRole("admin")) {
判斷用戶是否有打印的權限
view sourceprint?
Permission printPermission =newPrinterPermission(“laserjet3000n”,“print”);
If (currentUser.isPermitted(printPermission)) {//doone thing (show the print button?)}else{//don’t show the button?}也可以使用字符串的方式驗證
view sourceprint?
String perm = “printer:print:laserjet4400n”;
if(currentUser.isPermitted(perm)){
//show the print button?
}else{
//don’t show the button?
}
2,使用注釋方式 判斷用戶是否有 創建賬戶權限
view sourceprint?
//Will throw an AuthorizationException if none
//of the caller’s roles imply the Account
//'create' permission\u000B
@RequiresPermissions(“account:create”)
publicvoidopenAccount( Account acct ) {
//create the account
}
判斷用戶角色,如果符合角色,可以使用對應方法
view sourceprint?
//Throws an AuthorizationException if the caller
//doesn’t have the ‘teller’ role:
@RequiresRoles( “teller” )
publicvoidopenAccount( Account acct ) {
//do something in here that only a teller
//should do
}
3,使用jsp taglib 判斷用戶是否有管理權限
view sourceprint?
<%@ taglib prefix=“shiro” uri=http://shiro.apache.org/tags %>
<html>
<body>
<shiro:hasPermission name=“users:manage”>
<a href=“manageUsers.jsp”>
Click here to manage users
</a>
</shiro:hasPermission>
<shiro:lacksPermission name=“users:manage”>
No user managementforyou!
</shiro:lacksPermission>
</body>
<

從高的級別來看shiro:看一下官方的圖


view sourceprint?
Realm realm =//instantiate or acquire a Realm instance. We'll discuss Realms later.
SecurityManager securityManager =newDefaultSecurityManager(realm);
//Make the SecurityManager instance available to the entire application via static memory:
SecurityUtils.setSecurityManager(securityManager);
2,sessionManager對象圖如果你想使用sessionManager配置自定義的sessionDao信息,進行自定義會話管理
view sourceprint?
...
DefaultSecurityManager securityManager =newDefaultSecurityManager(realm);
SessionDAO sessionDAO =newCustomSessionDAO();
((DefaultSessionManager)securityManager.getSessionManager()).setSessionDAO(sessionDAO);
...
3,INI配置1) 創建一個INI從SecurityManager可以從多種方式讀取INI配置文件的信息,如文件系統,類路徑等
view sourceprint?
importorg.apache.shiro.SecurityUtils;
importorg.apache.shiro.util.Factory;
importorg.apache.shiro.mgt.SecurityManager;
importorg.apache.shiro.config.IniSecurityManagerFactory;
...
Factory<SecurityManager> factory =newIniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
2) 通過Ini實例讀取類似於Properties的方式
view sourceprint?
importorg.apache.shiro.SecurityUtils;
importorg.apache.shiro.util.Factory;
importorg.apache.shiro.mgt.SecurityManager;
importorg.apache.shiro.config.Ini;
importorg.apache.shiro.config.IniSecurityManagerFactory;
...
Ini ini =newIni();
//populate the Ini instance as necessary
...
Factory<SecurityManager> factory =newIniSecurityManagerFactory(ini);
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
加載之后就可以操作INI的配置了。4,INI配置每一個節點都是單獨的,不可以重復,注釋可以使用#或者;配置示例
view sourceprint?
# =======================
# Shiro INI configuration
# =======================
[main]
# Objects and their properties are defined here,
# Such as the securityManager, Realms and anything
#elseneeded to build the SecurityManager
[users]
# The'users'section isforsimple deployments
# when you only need a small number of statically-defined
# set of User accounts.
[roles]
# The'roles'section isforsimple deployments
# when you only need a small number of statically-defined
# roles.
[urls]
# The'urls'section is usedforurl-based security
# in web applications. We'll discussthissection in the
# Web documentation
1) [main]配置sessionManager的實例和它的依賴。配置示例
view sourceprint?
[main]
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
myRealm = com.company.security.shiro.DatabaseRealm
myRealm.connectionTimeout =30000
myRealm.username = jsmith
myRealm.password = secret
myRealm.credentialsMatcher = $sha256Matcher
securityManager.sessionManager.globalSessionTimeout =1800000
定義一個對象
view sourceprint?
[main]
myRealm = com.company.shiro.realm.MyRealm
...
簡單的屬性設置
view sourceprint?
...
myRealm.connectionTimeout =30000
myRealm.username = jsmith
...
配置信息將轉入到對應的set方法中
view sourceprint?
...
myRealm.setConnectionTimeout(30000);
myRealm.setUsername("jsmith");
...
參考值你可以使用$符號引用先前定義的一個對象的實例
view sourceprint?
...
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
...
myRealm.credentialsMatcher = $sha256Matcher
...
嵌套屬性
view sourceprint?
...
securityManager.sessionManager.globalSessionTimeout =1800000
...
將被注入到下面的程序中
view sourceprint?
securityManager.getSessionManager().setGlobalSessionTimeout(1800000);
引用其它的屬性
view sourceprint?
sessionListener1 = com.company.my.SessionListenerImplementation
...
sessionListener2 = com.company.my.other.SessionListenerImplementation
...
securityManager.sessionManager.sessionListeners = $sessionListener1, $sessionListener2
以鍵值的配置方式
view sourceprint?
object1 = com.company.some.Class
object2 = com.company.another.Class
...
anObject = some.class.with.a.Map.property
anObject.mapProperty = key1:$object1, key2:$object2
2) [users]在用戶比較少的情況下這種配置信息是有效的
view sourceprint?
[users]
admin = secret
lonestarr = vespa, goodguy, schwartz
darkhelmet = ludicrousspeed, badguy, schwartz
3) [roles]如果角色信息比較少的情況下可以使用這項配置
view sourceprint?
[roles]
#'admin'role has all permissions, indicated by the wildcard'*'
admin = *
# The'schwartz'role candoanything (*) with any lightsaber:
schwartz = lightsaber:*
# The'goodguy'role is allowed to'drive'(action) the winnebago (type) with
# license plate'eagle5'(instance specific id)
goodguy = winnebago:drive:eagle5
4) [urls]配置url等可訪問的資源信息。shiro(3)-shiro核心
身份認證


[main]...authenticator = com.foo.bar.CustomAuthenticatorsecurityManager.authenticator = $authenticator配置策略信息AtLeastOneSuccessfulStrategy 如果一個驗證成功,則驗證結果為成功FirstSuccessfulStrategy 只有第一個成功,才算成功AllSuccessfulStrategy 所有的都必須成功對應的在配置文件中的策略使用如下
shiro.ini[main]...authcStrategy = org.apache.shiro.authc.pam.FirstSuccessfulStrategysecurityManager.authenticator.authenticationStrategy = $authcStrategy...執行順序1)隱式順序
blahRealm = com.company.blah.Realm...fooRealm = com.company.foo.Realm...barRealm = com.company.another.Realm按上下順序執行2)指定順序
blahRealm = com.company.blah.Realm...fooRealm = com.company.foo.Realm...barRealm = com.company.another.RealmsecurityManager.realms = $fooRealm, $barRealm, $blahRealm...按指定的順序執行授權

Subject currentUser = SecurityUtils.getSubject();if (currentUser.hasRole("administrator")) { //show the admin button} else { //don't show the button? Grey it out?}hasRole(String roleName) 主題是否已分配給指定的角色hasRoles(List<String> roleNames) 是否包含指定的角色hasAllRoles(Collection<String> roleNames) 是否包含指定的所有角色角色斷言
Subject currentUser = SecurityUtils.getSubject();//guarantee that the current user is a bank teller and//therefore allowed to open the account:currentUser.checkRole("bankTeller");openBankAccount();checkRole(String roleName) 斷言是否是指定角色checkRoles(Collection<String> roleNames) 斷言是否包含以下角色checkRoles(String... roleNames) 斷言是否包含所有角色如果判斷指定用戶是否有權限訪問指定名稱的打印機那么就會用到下列幾個方法
Permission printPermission = new PrinterPermission("laserjet4400n", "print");Subject currentUser = SecurityUtils.getSubject();if (currentUser.isPermitted(printPermission)) { //show the Print button} else { //don't show the button? Grey it out?}isPermitted(Permission p) 判斷主題是否允許執行一個動作isPermitted(List<Permission> perms) 是否允許執行一組動作isPermittedAll(Collection<Permission> perms) 是否允許執行所有動作基於字符串的權限檢查
Subject currentUser = SecurityUtils.getSubject();if (currentUser.isPermitted("printer:print:laserjet4400n")) { //show the Print button} else { //don't show the button? Grey it out?}也可以如下使用
Subject currentUser = SecurityUtils.getSubject();Permission p = new WildcardPermission("printer:print:laserjet4400n");if (currentUser.isPermitted(p) { //show the Print button} else { //don't show the button? Grey it out?}權限斷言類似於角色斷言。2)annocation方式TheRequiresAuthenticationannotation
@RequiresAuthenticationpublic void updateAccount(Account userAccount) { //this method will only be invoked by a //Subject that is guaranteed authenticated ...}等同於下述代碼
public void updateAccount(Account userAccount) { if (!SecurityUtils.getSubject().isAuthenticated()) { throw new AuthorizationException(...); } //Subject is guaranteed authenticated here ...}TheRequiresGuestannotation
@RequiresGuestpublic void signUp(User newUser) { //this method will only be invoked by a //Subject that is unknown/anonymous ...}等同於
public void signUp(User newUser) { Subject currentUser = SecurityUtils.getSubject(); PrincipalCollection principals = currentUser.getPrincipals(); if (principals != null && !principals.isEmpty()) { /

@RequiresPermissions("account:create")public void createAccount(Account account) { //this method will only be invoked by a Subject //that is permitted to create an account ...}等同於
public void createAccount(Account account) { Subject currentUser = SecurityUtils.getSubject(); if (!subject.isPermitted("account:create")) { throw new AuthorizationException(...); } //Subject is guaranteed to be permitted here ...}TheRequiresRolespermission
@RequiresRoles("administrator")public void deleteUser(User user) { //this method will only be invoked by an administrator ...}等同於
public void deleteUser(User user) { Subject currentUser = SecurityUtils.getSubject(); if (!subject.hasRole("administrator")) { throw new AuthorizationException(...); } //Subject is guaranteed to be an 'administrator' here ...}TheRequiresUserannotation
@RequiresUserpublic void updateAccount(Account account) { //this method will only be invoked by a 'user' //i.e. a Subject with a known identity ...}等同於
public void updateAccount(Account account) { Subject currentUser = SecurityUtils.getSubject(); PrincipalCollection principals = currentUser.getPrincipals(); if (principals == null || principals.isEmpty()) { /


<listener> <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener<


<context-param> <param-name>shiroEnvironmentClass</param-name> <param-value>com.foo.bar.shiro.MyWebEnvironment</param-value></context-param>如果你想改變shiro.ini的位置,那么你可以指定
<context-param> <param-name>shiroConfigLocations</param-name> <param-value>YOUR_RESOURCE_LOCATION_HERE</param-value></context-param>shiro.ini中的[urls]配置例如:
...[urls]/index.html = anon/user/create = anon/user/** = authc/admin/** = authc, roles[administrator]/rest/** = authc, rest/remoting/rpc/** = authc, perms["remote:invoke"]假如你有如下設置
/account/** = ssl, authc/account下的任何應用程序都將觸動ssl和authc鏈在官方的示例中,有一個aspectj的示例,這個是一個銀行的示例,簡單的做了一下修改,演示一下其中幾個方法的使用過程。看以下幾個類,包括賬戶信息,轉賬信息,以及一些異常處理程序,還包括一個業務操作類Account賬戶信息類
import org.apache.commons.lang.builder.ToStringBuilder;import org.apache.commons.lang.builder.ToStringStyle;import java.sql.Timestamp;import java.util.ArrayList;import java.util.Date;import java.util.List;public class Account { private static long _SEQUENCE; private long _id; private String _ownerName; private volatile boolean _isActive; private double _balance; private final List<AccountTransaction> _transactions; private String _createdBy; private Date _creationDate; public Account(String anOwnerName) { _id = ++_SEQUENCE; _ownerName = anOwnerName; _isActive = true; _balance = 0.0d; _transactions = new ArrayList<AccountTransaction>(); _createdBy = "unknown"; _creationDate = new Date(); } /** * Returns the id attribute. * * @return The id value. */ public long getId() { return _id; } /** * Returns the ownerName attribute. * * @return The ownerName value. */ public String getOwnerName() { return _ownerName; } /** * Returns the isActive attribute. * * @return The isActive value. */ public boolean isActive() { return _isActive; } /** * Changes the value of the attributes isActive. * * @param aIsActive The new value of the isActive attribute. */ public void setActive(boolean aIsActive) { _isActive = aIsActive; } /** * Changes the value of the attributes ownerName. * * @param aOwnerName The new value of the ownerName attribute. */ public void setOwnerName(String aOwnerName) { _ownerName = aOwnerName; } /** * Returns the balance attribute. * * @return The balance value. */ public double getBalance() { return _balance; } /** * Returns the transactions attribute. * * @return The transactions value. */ public List<AccountTransaction> getTransactions() { return _transactions; } protected void applyTransaction(AccountTransaction aTransaction) throws NotEnoughFundsException, InactiveAccountException { if (!_isActive) { throw new InactiveAccountException("Unable to apply " + aTransaction.getType() + " of amount " + aTransaction.getAmount() + " to account " + _id); } synchronized (_transactions) { if (AccountTransaction.TransactionType.DEPOSIT == aTransaction.getType()) { _transactions.add(aTransaction); _balance += aTransaction.getAmount(); } else if (AccountTransaction.TransactionType.WITHDRAWAL == aTransaction.getType()) { if (_balance < aTransaction.getAmount()) { throw new NotEnoughFundsException("Unable to withdraw " + aTransaction.getAmount() + "$ from account " + _id + " - current balance is " + _balance); } _transactions.add(aTransaction); _balance -= aTransaction.getAmount(); } else { throw new IllegalArgumentException("The transaction passed in has an invalid type: " + aTransaction.getType()); } } } /** * Changes the value of the attributes createdBy. * * @param aCreatedBy The new value of the createdBy attribute. */ protected void setCreatedBy(String aCreatedBy) { _createdBy = aCreatedBy; } /** * Returns the createdBy attribute. * * @return The createdBy value. */ public String getCreatedBy() { return _createdBy; } /** * Returns the creationDate attribute. * * @return The creationDate value. */ public Date getCreationDate() { return _creationDate; } /* (non-Javadoc) * @see java.lang.Object#toString() */ public String toString() { return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE). append("id", _id). append("ownerName", _ownerName). append("isActive", _isActive). append("balance", _balance). append("tx.count", _transactions.size()). append("createdBy", _createdBy). append("creationDate", new Timestamp(_creationDate.getTime())). toString(); }}AccountNotFoundException,賬號不存在異常
package org.apache.shiro.samples.aspectj.bank;public class AccountNotFoundException extends BankServiceException { public AccountNotFoundException(String aMessage) { super(aMessage); }}AccountTransaction,賬號轉入與轉出
package org.apache.shiro.samples.aspectj.bank;import org.apache.commons.lang.builder.ToStringBuilder;import org.apache.commons.lang.builder.ToStringStyle;import java.sql.Timestamp;import java.util.Date;public class AccountTransaction { private static long _SEQUENCE; public enum TransactionType { DEPOSIT, WITHDRAWAL } private long _id; private TransactionType _type; private long _accountId; private double _amount; private String _createdBy; private Date _creationDate; public static AccountTransaction createDepositTx(long anAccountId, double anAmount) { return new AccountTransaction(TransactionType.DEPOSIT, anAccountId, anAmount); } public static AccountTransaction createWithdrawalTx(long anAccountId, double anAmount) { return new AccountTransaction(TransactionType.WITHDRAWAL, anAccountId, anAmount); } private AccountTransaction(TransactionType aType, long anAccountId, double anAmount) { _id = ++_SEQUENCE; _type = aType; _accountId = anAccountId; _amount = anAmount; _createdBy = "unknown"; _creationDate = new Date(); } /** * Returns the id attribute. * * @return The id value. */ public long getId() { return _id; } /** * Returns the type attribute. * * @return The type value. */ public TransactionType getType() { return _type; } /** * Returns the accountId attribute. * * @return The accountId value. */ public long getAccountId() { return _accountId; } /** * Returns the amount attribute. * * @return The amount value. */ public double getAmount() { return _amount; } /** * Changes the value of the attributes createdBy. * * @param aCreatedBy The new value of the createdBy attribute. */ protected void setCreatedBy(String aCreatedBy) { _createdBy = aCreatedBy; } /** * Returns the createdBy attribute. * * @return The createdBy value. */ public String getCreatedBy() { return _createdBy; } /** * Returns the creationDate attribute. * * @return The creationDate value. */ public Date getCreationDate() { return _creationDate; } /* (non-Javadoc) * @see java.lang.Object#toString() */ public String toString() { return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE). append("id", _id). append("type", _type). append("accountId", _accountId). append("amount", _amount). append("createdBy", _createdBy). append("creationDate", new Timestamp(_creationDate.getTime())). toString(); }}BankServerRunner,銀行服務運行
package org.apache.shiro.samples.aspectj.bank;public class BankServerRunner { private SecureBankService _bankService; public synchronized void start() throws Exception { if (_bankService == null) { _bankService = new SecureBankService(); _bankService.start(); } } public synchronized void stop() { if (_bankService != null) { try { _bankService.dispose(); } finally { _bankService = null; } } } public BankService getBankService() { return _bankService; } public static void main(String[] args) { try { BankServerRunner server = new BankServerRunner(); server.start(); server.stop(); } catch (Exception e) { e.printStackTrace(); } }} BankService,銀行服務接口
package org.apache.shiro.samples.aspectj.bank;import java.util.Date;public interface BankService { public long[] searchAccountIdsByOwner(String anOwnerName); public long createNewAccount(String anOwnerName); public double getBalanceOf(long anAccountId) throws AccountNotFoundException; public String getOwnerOf(long anAccountId) throws AccountNotFoundException; public double depositInto(long anAccountId, double anAmount) throws AccountNotFoundException, InactiveAccountException; public double withdrawFrom(long anAccountId, double anAmount) throws AccountNotFoundException, NotEnoughFundsException, InactiveAccountException; public TxLog[] getTxHistoryFor(long anAccountId) throws AccountNotFoundException; public double closeAccount(long anAccountId) throws AccountNotFoundException, InactiveAccountException; public boolean isAccountActive(long anAccountId) throws AccountNotFoundException; public static class TxLog { private Date _creationDate; private double _amount; private String _madeBy; public TxLog(Date aCreationDate, double aAmount, String aMadeBy) { super(); _creationDate = aCreationDate; _amount = aAmount; _madeBy = aMadeBy; } /** * Returns the creationDate attribute. * * @return The creationDate value. */ public Date getCreationDate() { return _creationDate; } /** * Returns the amount attribute. * * @return The amount value. */ public double getAmount() { return _amount; } /** * Returns the madeBy attribute. * * @return The madeBy value. */ public String getMadeBy() { return _madeBy; } }}BankServiceException,銀行服務異常
package org.apache.shiro.samples.aspectj.bank;public class BankServiceException extends Exception { public BankServiceException(String aMessage) { super(aMessage); } public BankServiceException(String aMessage, Throwable aCause) { super(aMessage, aCause); }}InactiveAccountException,存入賬戶異常
package org.apache.shiro.samples.aspectj.bank;public class InactiveAccountException extends BankServiceException { public InactiveAccountException(String aMessage) { super(aMessage); }}NotEnoughFundsException,賬戶不足異常
package org.apache.shiro.samples.aspectj.bank;public class NotEnoughFundsException extends BankServiceException { public NotEnoughFundsException(String aMessage) { super(aMessage); }}SecureBankService,安全銀行的服務類,處理各種銀行的業務
package org.apache.shiro.samples.aspectj.bank;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authz.annotation.RequiresPermissions;import org.apache.shiro.samples.aspectj.bank.AccountTransaction.TransactionType;import org.apache.shiro.subject.Subject;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class SecureBankService implements BankService { private static final Logger log = LoggerFactory.getLogger(SecureBankService.class); private volatile boolean _isRunning; private final List<Account> _accounts; private Map<Long, Account> _accountsById; /** * Creates a new {@link SecureBankService} instance. */ public SecureBankService() { _accounts = new ArrayList<Account>(); _accountsById = new HashMap<Long, Account>(); } /** * Starts this service */ public void start() throws Exception { _isRunning = true; log.info("銀行服務開始..."); } /** * Stop this service */ public void dispose() { log.info("銀行服務停止..."); _isRunning = false; synchronized (_accounts) { _accountsById.clear(); _accounts.clear(); } log.info("銀行服務停止!"); } /** * Internal utility method that validate the internal state of this service. */ protected void assertServiceState() { if (!_isRunning) { throw new IllegalStateException("銀行的服務沒有開始"); } } public int getAccountCount() { return _accounts.size(); } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#createNewAccount(java.lang.String) */ @RequiresPermissions("bankAccount:create") public long createNewAccount(String anOwnerName) { assertServiceState(); log.info("創建新的賬戶給 " + anOwnerName); synchronized (_accounts) { Account account = new Account(anOwnerName); account.setCreatedBy(getCurrentUsername()); _accounts.add(account); _accountsById.put(account.getId(), account); log.debug("創建新的賬戶: " + account); return account.getId(); } } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#searchAccountIdsByOwner(java.lang.String) */ public long[] searchAccountIdsByOwner(String anOwnerName) { assertServiceState(); log.info("查找已經存在的銀行賬戶為 " + anOwnerName); ArrayList<Account> matchAccounts = new ArrayList<Account>(); synchronized (_accounts) { for (Account a : _accounts) { if (a.getOwnerName().toLowerCase().contains(anOwnerName.toLowerCase())) { matchAccounts.add(a); } } } long[] accountIds = new long[matchAccounts.size()]; int index = 0; for (Account a : matchAccounts) { accountIds[index++] = a.getId(); } log.debug("找到 " + accountIds.length + " 相匹配的賬戶的名稱 " + anOwnerName); return accountIds; } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#getOwnerOf(long) */ @RequiresPermissions("bankAccount:read") public String getOwnerOf(long anAccountId) throws AccountNotFoundException { assertServiceState(); log.info("獲得銀行賬戶的所有者 " + anAccountId); Account a = safellyRetrieveAccountForId(anAccountId); return a.getOwnerName(); } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#getBalanceOf(long) */ @RequiresPermissions("bankAccount:read") public double getBalanceOf(long anAccountId) throws AccountNotFoundException { assertServiceState(); log.info("得到賬戶的余額 " + anAccountId); Account a = safellyRetrieveAccountForId(anAccountId); return a.getBalance(); } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#depositInto(long, double) */ @RequiresPermissions("bankAccount:operate") public double depositInto(long anAccountId, double anAmount) throws AccountNotFoundException, InactiveAccountException { assertServiceState(); log.info("存錢到 " + anAmount + " 這個賬戶 " + anAccountId); try { Account a = safellyRetrieveAccountForId(anAccountId); AccountTransaction tx = AccountTransaction.createDepositTx(anAccountId, anAmount); tx.setCreatedBy(getCurrentUsername()); log.debug("創建一個新的交易 " + tx); a.applyTransaction(tx); log.debug("新的賬戶余額 " + a.getId() + " 存款后 " + a.getBalance()); return a.getBalance(); } catch (NotEnoughFundsException nefe) { throw new IllegalStateException("應該從未發生過", nefe); } } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#withdrawFrom(long, double) */ @RequiresPermissions("bankAccount:operate") public double withdrawFrom(long anAccountId, double anAmount) throws AccountNotFoundException, NotEnoughFundsException, InactiveAccountException { assertServiceState(); log.info("取款 " + anAmount + " 從賬戶 " + anAccountId); Account a = safellyRetrieveAccountForId(anAccountId); AccountTransaction tx = AccountTransaction.createWithdrawalTx(anAccountId, anAmount); tx.setCreatedBy(getCurrentUsername()); log.debug("創建一個新的交易 " + tx); a.applyTransaction(tx); log.debug("新的賬戶余額 " + a.getId() + " 取款后 " + a.getBalance()); return a.getBalance(); } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#getTxHistoryFor(long) */ @RequiresPermissions("bankAccount:read") public TxLog[] getTxHistoryFor(long anAccountId) throws AccountNotFoundException { assertServiceState(); log.info("獲取賬戶交易 " + anAccountId); Account a = safellyRetrieveAccountForId(anAccountId); TxLog[] txs = new TxLog[a.getTransactions().size()]; int index = 0; for (AccountTransaction tx : a.getTransactions()) { log.debug("查過交易 " + tx); if (TransactionType.DEPOSIT == tx.getType()) { txs[index++] = new TxLog(tx.getCreationDate(), tx.getAmount(), tx.getCreatedBy()); } else { txs[index++] = new TxLog(tx.getCreationDate(), -1.0d * tx.getAmount(), tx.getCreatedBy()); } } return txs; } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#closeAccount(long) */ @RequiresPermissions("bankAccount:close") public double closeAccount(long anAccountId) throws AccountNotFoundException, InactiveAccountException { assertServiceState(); log.info("截止賬戶 " + anAccountId); Account a = safellyRetrieveAccountForId(anAccountId); if (!a.isActive()) { throw new InactiveAccountException("這個賬戶 " + anAccountId + " 已經關閉"); } try { AccountTransaction tx = AccountTransaction.createWithdrawalTx(a.getId(), a.getBalance()); tx.setCreatedBy(getCurrentUsername()); log.debug("創建一個新的交易 " + tx); a.applyTransaction(tx); a.setActive(false); log.debug("賬戶 " + a.getId() + " 現在是關閉的 " + tx.getAmount() + " 針對這個業主"); return tx.getAmount(); } catch (NotEnoughFundsException nefe) { throw new IllegalStateException("應該從來不發生", nefe); } } /* (non-Javadoc) * @see com.connectif.trilogy.root.security.BankService#isAccountActive(long) */ @RequiresPermissions("bankAccount:read") public boolean isAccountActive(long anAccountId) throws AccountNotFoundException { assertServiceState(); log.info("獲取賬戶的活動狀態 " + anAccountId); Account a = safellyRetrieveAccountForId(anAccountId); return a.isActive(); } /** * Internal method that safelly (concurrency-wise) retrieves an account from the id passed in. * * @param anAccountId The identifier of the account to retrieve. * @return The account instance retrieved. * @throws AccountNotFoundException If no account is found for the provided identifier. */ protected Account safellyRetrieveAccountForId(long anAccountId) throws AccountNotFoundException { Account account = null; synchronized (_accounts) { account = _accountsById.get(anAccountId); } if (account == null) { throw new AccountNotFoundException("沒有找到ID為 " + anAccountId + " 的賬戶"); } log.info("檢查賬戶 " + account); return account; } /** * Internal utility method to retrieve the username of the current authenticated user. * * @return The name. */ protected String getCurrentUsername() { Subject subject = SecurityUtils.getSubject(); if (subject == null || subject.getPrincipal() == null || !subject.isAuthenticated()) { throw new IllegalStateException("無法檢索當前驗證的主題"); } return SecurityUtils.getSubject().getPrincipal().toString(); }}在配置文件中配置了三組賬戶
[users]root = secret, adminsally = 1234, superviserdan = 123, user用戶 root 密碼secret 角色admin用戶 sally 密碼1234 角色superviser用戶 dan密碼123 角色user角色信息包括
[roles]admin = bankAccount:*superviser = bankAccount:create, bankAccount:read bankAccount:closeuser = bankAccount:create, bankAccount:read, bankAccount:operate包括種種操作的權限分配使用junit測試
@BeforeClass public static void setUpClass() throws Exception { BasicConfigurator.resetConfiguration(); BasicConfigurator.configure(); logger = Logger.getLogger(SecureBankServiceTest.class.getSimpleName()); Factory<SecurityManager> factory = new IniSecurityManagerFactory( "classpath:shiroBankServiceTest.ini"); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); service = new SecureBankService(); service.start(); }加載對應的ini中的信息,在每次運行之前登錄用戶的操作方法
// 作為用戶登錄,不能關閉賬戶 protected void loginAsUser() { if (_subject == null) { _subject = SecurityUtils.getSubject(); } // use dan to run as a normal user (which cannot close an account) _subject.login(new UsernamePasswordToken("dan", "123")); } // 作為超級用戶登錄,不能操作賬戶 protected void loginAsSuperviser() { if (_subject == null) { _subject = SecurityUtils.getSubject(); } // use sally to run as a superviser (which cannot operate an account) _subject.login(new UsernamePasswordToken("sally", "1234")); }給張三創建賬戶,並且檢查賬戶的情況
@Test public void testCreateAccount() throws Exception { loginAsUser(); createAndValidateAccountFor("張三"); }protected long createAndValidateAccountFor(String anOwner) throws Exception { long createdId = service.createNewAccount(anOwner); assertAccount(anOwner, true, 0.0d, 0, createdId); return createdId; }public static void assertAccount(String eOwnerName, boolean eIsActive, double eBalance, int eTxLogCount, long actualAccountId) throws Exception { Assert.assertEquals(eOwnerName, service.getOwnerOf(actualAccountId)); Assert.assertEquals(eIsActive, service.isAccountActive(actualAccountId)); Assert.assertEquals(eBalance, service.getBalanceOf(actualAccountId)); Assert.assertEquals(eTxLogCount, service.getTxHistoryFor(actualAccountId).length); }看打印出來的信息
1 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini]10 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users]12 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles]13 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles]46 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing...48 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing...59 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始...62 [main] INFO SecureBankServiceTest - ############################ 開始測試用例 1120 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String]120 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison121 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan]121 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.121 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.122 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance.123 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler...132 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null]188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 張三203 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=張三,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:40:26.71]203 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1203 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=張三,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:40:26.71]205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=張三,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:40:26.71]205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=張三,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:40:26.71]205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=張三,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:40:26.71]206 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan206 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [66208001-e91d-4625-938f-1b1c08b2645c]208 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止...208 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!創建張三的用戶信息並且檢查張三的賬戶的情況。第二個測試創建賬戶李四並且存入250到賬戶里,用戶有創建和操作的權限,所以可以操作
@Test public void testDepositInto_singleTx() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("李四"); makeDepositAndValidateAccount(accountId, 250.00d, "李四"); }protected double makeDepositAndValidateAccount(long anAccountId, double anAmount, String eOwnerName) throws Exception { double previousBalance = service.getBalanceOf(anAccountId); int previousTxCount = service.getTxHistoryFor(anAccountId).length; double newBalance = service.depositInto(anAccountId, anAmount); Assert.assertEquals(previousBalance + anAmount, newBalance); assertAccount(eOwnerName, true, newBalance, 1 + previousTxCount, anAccountId); return newBalance; }運行后的結果
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini]9 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users]12 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles]13 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles]44 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing...46 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing...56 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始...59 [main] INFO SecureBankServiceTest - ############################ 開始測試用例 1115 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String]115 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison115 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan]115 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.116 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.116 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance.117 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler...124 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null]171 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 李四188 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991]188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991]190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991]191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991]191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991]192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991]193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991]193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 250.0 這個賬戶 1193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:44:14.991]196 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=250.0,createdBy=dan,creationDate=2011-09-12 20:44:15.013]196 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 250.0196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=250.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:44:14.991]196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=250.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:44:14.991]196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=250.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:44:14.991]196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=李四,isActive=true,balance=250.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:44:14.991]196 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=250.0,createdBy=dan,creationDate=2011-09-12 20:44:15.013]196 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan197 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [3b53dc16-67d5-4730-ae8a-872d113c7546]198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止...198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!創建賬戶王五並且存入多筆款項
@Test public void testDepositInto_multiTxs() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("王五"); makeDepositAndValidateAccount(accountId, 50.00d, "王五"); makeDepositAndValidateAccount(accountId, 300.00d, "王五"); makeDepositAndValidateAccount(accountId, 85.00d, "王五"); assertAccount("王五", true, 435.00d, 3, accountId); }一共存入三筆,最后得到的數據的總和為435看日志打出來的信息
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini]10 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users]12 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles]13 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles]46 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing...49 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing...59 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始...62 [main] INFO SecureBankServiceTest - ############################ 開始測試用例 1118 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String]118 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison119 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan]119 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.119 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.120 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance.121 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler...129 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null]189 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 王五204 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72]204 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1204 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72]205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72]206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72]206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72]207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72]207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72]207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 50.0 這個賬戶 1207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:52:54.72]210 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 20:52:54.739]210 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 50.0210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:52:54.72]210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:52:54.72]210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:52:54.72]210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:52:54.72]210 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 20:52:54.739]210 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:52:54.72]211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:52:54.72]211 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 20:52:54.739]211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 300.0 這個賬戶 1211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 20:52:54.72]211 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=2,type=DEPOSIT,accountId=1,amount=300.0,createdBy=dan,creationDate=2011-09-12 20:52:54.741]211 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 350.0211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=350.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 20:52:54.72]211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=350.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 20:52:54.72]211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=350.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 20:52:54.72]211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=350.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 20:52:54.72]212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 20:52:54.739]212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=DEPOSIT,accountId=1,amount=300.0,createdBy=dan,creationDate=2011-09-12 20:52:54.741]212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=350.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 20:52:54.72]212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=350.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 20:52:54.72]212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 20:52:54.739]212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=DEPOSIT,accountId=1,amount=300.0,createdBy=dan,creationDate=2011-09-12 20:52:54.741]212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 85.0 這個賬戶 1212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=350.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 20:52:54.72]212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=3,type=DEPOSIT,accountId=1,amount=85.0,createdBy=dan,creationDate=2011-09-12 20:52:54.742]212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 435.0212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72]212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72]213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72]213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72]213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 20:52:54.739]213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=DEPOSIT,accountId=1,amount=300.0,createdBy=dan,creationDate=2011-09-12 20:52:54.741]213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=3,type=DEPOSIT,accountId=1,amount=85.0,createdBy=dan,creationDate=2011-09-12 20:52:54.742]213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72]213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72]213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72]213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=王五,isActive=true,balance=435.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 20:52:54.72]214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 20:52:54.739]214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=DEPOSIT,accountId=1,amount=300.0,createdBy=dan,creationDate=2011-09-12 20:52:54.741]214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=3,type=DEPOSIT,accountId=1,amount=85.0,createdBy=dan,creationDate=2011-09-12 20:52:54.742]214 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan214 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [1d66c2ec-a668-478a-8f30-e3c65f80a16d]216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止...216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!創建賬戶賈六並且取款,因為賬戶為空所以會拋出異常
@Test(expected = NotEnoughFundsException.class) public void testWithdrawFrom_emptyAccount() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("賈六"); service.withdrawFrom(accountId, 100.00d); }看執行的結果
1 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini]11 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users]13 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles]15 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles]46 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing...50 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing...60 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始...63 [main] INFO SecureBankServiceTest - ############################ 開始測試用例 1126 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String]126 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison128 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan]128 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.129 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.130 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance.132 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler...145 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null]190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 賈六205 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=賈六,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:56:05.029]205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=賈六,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:56:05.029]206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=賈六,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:56:05.029]206 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=賈六,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:56:05.029]207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=賈六,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:56:05.029]208 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 取款 100.0 從賬戶 1208 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=賈六,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 20:56:05.029]210 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 20:56:05.047]210 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan210 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [05f3559d-d0c4-458c-a220-31389550576f]211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止...211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!得到期望的NotEnoughFundsException,運行通過然后創建賬戶周七,先存入50,然后取100,結果得到的與面相同,余額不足異常
@Test(expected = NotEnoughFundsException.class) public void testWithdrawFrom_notEnoughFunds() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("周七"); makeDepositAndValidateAccount(accountId, 50.00d, "周七"); service.withdrawFrom(accountId, 100.00d); }看打印出的日志信息
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini]10 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users]12 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles]13 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles]44 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing...48 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing...59 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始...61 [main] INFO SecureBankServiceTest - ############################ 開始測試用例 1118 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String]118 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison119 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan]119 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.119 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.120 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance.121 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler...131 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null]179 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 周七196 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936]196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936]197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936]197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936]197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936]198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936]198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936]198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 50.0 這個賬戶 1199 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:01:30.936]200 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 21:01:30.955]200 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 50.0200 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:01:30.936]201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:01:30.936]201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:01:30.936]201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:01:30.936]201 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=50.0,createdBy=dan,creationDate=2011-09-12 21:01:30.955]201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 取款 100.0 從賬戶 1201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=周七,isActive=true,balance=50.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:01:30.936]201 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:01:30.956]202 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan202 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [a85a89c7-a805-4086-bd5b-109a0d54086c]203 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止...203 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!再測試先存后取,先存入500,后取100,最后得到的結果為400
@Test public void testWithdrawFrom_singleTx() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("國八"); makeDepositAndValidateAccount(accountId, 500.00d, "國八"); makeWithdrawalAndValidateAccount(accountId, 100.00d, "國八"); assertAccount("國八", true, 400.00d, 2, accountId); }看打印出的結果
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini]9 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users]10 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles]11 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles]43 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing...45 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing...55 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始...59 [main] INFO SecureBankServiceTest - ############################ 開始測試用例 1115 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String]115 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison116 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan]116 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.116 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.116 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance.117 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler...124 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null]168 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 國八185 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085]186 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1186 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085]187 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1187 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085]187 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1187 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085]187 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1187 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085]188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085]188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085]188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 500.0 這個賬戶 1188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:03:17.085]190 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:03:17.103]190 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 500.0190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:03:17.085]190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:03:17.085]191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:03:17.085]191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:03:17.085]191 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:03:17.103]191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:03:17.085]191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:03:17.085]191 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:03:17.103]191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 取款 100.0 從賬戶 1191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:03:17.085]192 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:03:17.104]192 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 取款后 400.0192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085]192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085]192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085]192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085]192 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:03:17.103]192 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:03:17.104]192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085]192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085]193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085]193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=國八,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:03:17.085]193 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:03:17.103]193 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:03:17.104]193 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan193 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [192dddd6-7090-435c-bb65-b3b64a73d667]195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止...195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!存入一筆取多筆
@Test public void testWithdrawFrom_manyTxs() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("Zoe Smith"); makeDepositAndValidateAccount(accountId, 500.00d, "Zoe Smith"); makeWithdrawalAndValidateAccount(accountId, 100.00d, "Zoe Smith"); makeWithdrawalAndValidateAccount(accountId, 75.00d, "Zoe Smith"); makeWithdrawalAndValidateAccount(accountId, 125.00d, "Zoe Smith"); assertAccount("Zoe Smith", true, 200.00d, 4, accountId); }查看打印的日志信息
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini]9 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users]11 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles]13 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles]53 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing...57 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing...72 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始...76 [main] INFO SecureBankServiceTest - ############################ 開始測試用例 1132 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String]132 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison133 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan]133 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.133 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.134 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance.135 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler...143 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null]186 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 Zoe Smith205 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312]205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1205 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312]207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312]207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312]207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1208 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312]209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312]209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312]209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 500.0 這個賬戶 1209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:04:28.312]212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337]212 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 500.0212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:04:28.312]212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:04:28.312]212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:04:28.312]212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1212 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:04:28.312]213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337]213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:04:28.312]213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:04:28.312]213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337]213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 取款 100.0 從賬戶 1213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:04:28.312]213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:04:28.338]213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 取款后 400.0213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:04:28.312]213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:04:28.312]213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:04:28.312]214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:04:28.312]214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337]214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:04:28.338]214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:04:28.312]214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:04:28.312]214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337]214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:04:28.338]214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 取款 75.0 從賬戶 1214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=400.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:04:28.312]215 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=3,type=WITHDRAWAL,accountId=1,amount=75.0,createdBy=dan,creationDate=2011-09-12 21:04:28.339]215 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 取款后 325.0215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=325.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 21:04:28.312]215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=325.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 21:04:28.312]215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=325.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 21:04:28.312]215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=325.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 21:04:28.312]215 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337]215 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:04:28.338]215 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=3,type=WITHDRAWAL,accountId=1,amount=75.0,createdBy=dan,creationDate=2011-09-12 21:04:28.339]215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=325.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 21:04:28.312]215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=325.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 21:04:28.312]216 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337]216 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:04:28.338]216 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=3,type=WITHDRAWAL,accountId=1,amount=75.0,createdBy=dan,creationDate=2011-09-12 21:04:28.339]216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 取款 125.0 從賬戶 1216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=325.0,tx.count=3,createdBy=dan,creationDate=2011-09-12 21:04:28.312]216 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=4,type=WITHDRAWAL,accountId=1,amount=125.0,createdBy=dan,creationDate=2011-09-12 21:04:28.341]216 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 取款后 200.0216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312]216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312]216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312]216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1216 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312]216 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337]217 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:04:28.338]217 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=3,type=WITHDRAWAL,accountId=1,amount=75.0,createdBy=dan,creationDate=2011-09-12 21:04:28.339]217 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=4,type=WITHDRAWAL,accountId=1,amount=125.0,createdBy=dan,creationDate=2011-09-12 21:04:28.341]217 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1217 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312]217 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1217 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312]217 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1220 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312]220 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1221 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=200.0,tx.count=4,createdBy=dan,creationDate=2011-09-12 21:04:28.312]221 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:04:28.337]221 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=100.0,createdBy=dan,creationDate=2011-09-12 21:04:28.338]221 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=3,type=WITHDRAWAL,accountId=1,amount=75.0,createdBy=dan,creationDate=2011-09-12 21:04:28.339]221 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=4,type=WITHDRAWAL,accountId=1,amount=125.0,createdBy=dan,creationDate=2011-09-12 21:04:28.341]221 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan221 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [1ecbe8f2-f2f5-468b-af2b-d82d6b1267fa]223 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止...223 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!存多少取多少
@Test public void testWithdrawFrom_upToZero() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("Zoe Smith"); makeDepositAndValidateAccount(accountId, 500.00d, "Zoe Smith"); makeWithdrawalAndValidateAccount(accountId, 500.00d, "Zoe Smith"); assertAccount("Zoe Smith", true, 0.00d, 2, accountId); }查看打印的日志信息
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini]9 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users]11 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles]12 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles]43 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing...45 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing...55 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始...58 [main] INFO SecureBankServiceTest - ############################ 開始測試用例 1114 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String]114 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison114 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan]115 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.115 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.115 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance.116 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler...125 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null]168 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 Zoe Smith186 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783]186 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1187 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783]188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783]188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783]188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1188 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783]189 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1189 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783]189 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1189 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783]190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 500.0 這個賬戶 1190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:05:23.783]192 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.804]192 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 500.0192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:05:23.783]192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:05:23.783]192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:05:23.783]192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:05:23.783]192 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.804]192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:05:23.783]192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:05:23.783]193 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.804]193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 取款 500.0 從賬戶 1193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=500.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:05:23.783]193 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.806]193 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 取款后 0.0193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783]193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783]193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783]193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783]193 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.804]193 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.806]194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783]194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783]194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783]194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1194 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Zoe Smith,isActive=true,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:05:23.783]194 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.804]194 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=500.0,createdBy=dan,creationDate=2011-09-12 21:05:23.806]194 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan195 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [12aeb47c-f3c1-46c1-baec-78da03762422]196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止...196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!關閉賬戶余額為0的賬戶,普通用戶沒有權限,所以需要轉到另外一個角色的賬戶進行操作
@Test public void testCloseAccount_zeroBalance() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("Chris Smith"); logoutCurrentSubject(); loginAsSuperviser(); double closingBalance = service.closeAccount(accountId); Assert.assertEquals(0.00d, closingBalance); assertAccount("Chris Smith", false, 0.00d, 1, accountId); }查看打印出來的日志信息
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini]11 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users]13 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles]14 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles]47 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing...50 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing...61 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始...63 [main] INFO SecureBankServiceTest - ############################ 開始測試用例 1121 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String]121 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison121 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan]122 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.122 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.123 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance.124 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler...133 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null]191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 Chris Smith207 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:13:04.496]207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1207 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:13:04.496]208 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:13:04.496]209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:13:04.496]209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1209 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:13:04.496]210 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan210 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [c4adc0a6-987c-4c94-ad38-d13f683c7f1d]211 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String]211 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison211 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - sally, rememberMe=false]. Returned account [sally]211 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.211 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.211 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null]211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 截止賬戶 1211 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:13:04.496]213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=WITHDRAWAL,accountId=1,amount=0.0,createdBy=sally,creationDate=2011-09-12 21:13:04.516]213 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 賬戶 1 現在是關閉的 0.0 針對這個業主213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:13:04.496]213 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:13:04.496]214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:13:04.496]214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1214 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:13:04.496]214 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=WITHDRAWAL,accountId=1,amount=0.0,createdBy=sally,creationDate=2011-09-12 21:13:04.516]214 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}sally214 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [f0988257-3441-489a-859c-538043ead6e3]215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止...215 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!創建用戶並且存入350,然后對這個用戶進行關閉操作
@Test public void testCloseAccount_withBalance() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("Gerry Smith"); makeDepositAndValidateAccount(accountId, 385.00d, "Gerry Smith"); logoutCurrentSubject(); loginAsSuperviser(); double closingBalance = service.closeAccount(accountId); Assert.assertEquals(385.00d, closingBalance); assertAccount("Gerry Smith", false, 0.00d, 2, accountId); }查看打印的日志信息
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini]9 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users]11 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles]12 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles]46 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing...48 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing...58 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始...61 [main] INFO SecureBankServiceTest - ############################ 開始測試用例 1117 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String]117 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison118 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan]118 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.118 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.118 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance.119 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler...128 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null]173 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 Gerry Smith190 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652]190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1190 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652]191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1191 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652]192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652]192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1192 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652]193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652]193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652]193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 存錢到 385.0 這個賬戶 1193 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:17:58.652]195 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=385.0,createdBy=dan,creationDate=2011-09-12 21:17:58.672]195 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 新的賬戶余額 1 存款后 385.0195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=385.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:17:58.652]195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=385.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:17:58.652]195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=385.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:17:58.652]196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=385.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:17:58.652]196 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=385.0,createdBy=dan,creationDate=2011-09-12 21:17:58.672]196 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan196 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [b2e689a3-cd4a-4785-962b-0df77758533b]197 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String]197 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison197 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - sally, rememberMe=false]. Returned account [sally]197 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.197 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.197 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null]197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 截止賬戶 1197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=true,balance=385.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:17:58.652]197 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=385.0,createdBy=sally,creationDate=2011-09-12 21:17:58.674]197 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 賬戶 1 現在是關閉的 385.0 針對這個業主197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=false,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:17:58.652]197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=false,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:17:58.652]197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=false,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:17:58.652]198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1198 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Gerry Smith,isActive=false,balance=0.0,tx.count=2,createdBy=dan,creationDate=2011-09-12 21:17:58.652]198 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=DEPOSIT,accountId=1,amount=385.0,createdBy=dan,creationDate=2011-09-12 21:17:58.672]198 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=2,type=WITHDRAWAL,accountId=1,amount=385.0,createdBy=sally,creationDate=2011-09-12 21:17:58.674]198 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}sally198 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [6ffa0d67-7510-4205-9fa8-01b6bb9793f5]199 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止...199 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!創建用戶並且關閉正活動的賬戶
@Test(expected = InactiveAccountException.class) public void testCloseAccount_alreadyClosed() throws Exception { loginAsUser(); long accountId = createAndValidateAccountFor("Chris Smith"); logoutCurrentSubject(); loginAsSuperviser(); double closingBalance = service.closeAccount(accountId); Assert.assertEquals(0.00d, closingBalance); assertAccount("Chris Smith", false, 0.00d, 1, accountId); service.closeAccount(accountId); }查看打印的日志信息
0 [main] DEBUG org.apache.shiro.io.ResourceUtils - Opening resource from class path [shiroBankServiceTest.ini]9 [main] DEBUG org.apache.shiro.config.Ini - Parsing [users]12 [main] DEBUG org.apache.shiro.config.Ini - Parsing [roles]13 [main] DEBUG org.apache.shiro.config.IniFactorySupport - Creating instance from Ini [sections=users,roles]44 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [roles] section. Processing...47 [main] DEBUG org.apache.shiro.realm.text.IniRealm - Discovered the [users] section. Processing...57 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務開始...60 [main] INFO SecureBankServiceTest - ############################ 開始測試用例 1117 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String]117 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison117 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - dan, rememberMe=false]. Returned account [dan]118 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.118 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.119 [main] DEBUG org.apache.shiro.session.mgt.AbstractValidatingSessionManager - No sessionValidationScheduler set. Attempting to create default instance.120 [main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler...127 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null]178 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶給 Chris Smith195 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建新的賬戶: Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:19:53.755]195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1195 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:19:53.755]196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:19:53.755]196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1196 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:19:53.755]197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1197 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:19:53.755]198 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}dan198 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [8ff8f7c8-5d03-4e4f-b47d-0414cd43111d]198 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Performing credentials equality check for tokenCredentials of type [[C and accountCredentials of type [java.lang.String]198 [main] DEBUG org.apache.shiro.authc.credential.SimpleCredentialsMatcher - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison199 [main] DEBUG org.apache.shiro.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - sally, rememberMe=false]. Returned account [sally]199 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.199 [main] DEBUG org.apache.shiro.subject.support.DefaultSubjectContext - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.199 [main] DEBUG org.apache.shiro.session.mgt.DefaultSessionManager - Creating new EIS record for new session instance [org.apache.shiro.session.mgt.SimpleSession,id=null]199 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 截止賬戶 1199 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=true,balance=0.0,tx.count=0,createdBy=dan,creationDate=2011-09-12 21:19:53.755]201 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 創建一個新的交易 AccountTransaction[id=1,type=WITHDRAWAL,accountId=1,amount=0.0,createdBy=sally,creationDate=2011-09-12 21:19:53.777]201 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 賬戶 1 現在是關閉的 0.0 針對這個業主201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲得銀行賬戶的所有者 1201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:19:53.755]201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶的活動狀態 1201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:19:53.755]201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 得到賬戶的余額 1201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:19:53.755]201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 獲取賬戶交易 1201 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:19:53.755]201 [main] DEBUG org.apache.shiro.samples.aspectj.bank.SecureBankService - 查過交易 AccountTransaction[id=1,type=WITHDRAWAL,accountId=1,amount=0.0,createdBy=sally,creationDate=2011-09-12 21:19:53.777]202 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 截止賬戶 1202 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 檢查賬戶 Account[id=1,ownerName=Chris Smith,isActive=false,balance=0.0,tx.count=1,createdBy=dan,creationDate=2011-09-12 21:19:53.755]202 [main] DEBUG org.apache.shiro.mgt.DefaultSecurityManager - Logging out subject with primary principal {}sally202 [main] DEBUG org.apache.shiro.session.mgt.AbstractSessionManager - Stopping session with id [53286615-5b71-4642-b3e8-916fb77fba60]203 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止...203 [main] INFO org.apache.shiro.samples.aspectj.bank.SecureBankService - 銀行服務停止!其中判斷權限使用的是annocation的方式@RequiresPermissions("bankAccount:create") 是否有用戶創建權限@RequiresPermissions("bankAccount:read") 讀權限@RequiresPermissions("bankAccount:operate") 操作權限@RequiresPermissions("bankAccount:close") 關閉權限根據以上幾個標簽就可以得到對應的權限信息