我們首先了解下什麼是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,得到主體的認證和憑據。
view sourceprint?
|
|
|
|
2,送出認證和憑據給身份驗證系統。
|
|
3,判斷是否允許通路,重試認證或者阻止通路。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
其中Remember Me的功能包括兩個方法,一個是
isRemembered
boolean isRemembered()
非匿名登入的使用者可以記住上次使用的主題的資訊。
isAuthenticated
boolean isAuthenticated()
在此期間需要使用有效的憑據登入系統,否則值為false.
授權操作
授權的例子就是是否可以通路某個頁面,可以操作某個按鈕,是否可以編緝對應的資料等。
如何在shiro中使用授權
1,使用程式設計方式
判斷是否有管理者角色
|
判斷使用者是否有列印的權限
|
If (currentUser.isPermitted(printPermission)) {
//do one thing (show the print button?)
} else {
//don’t show the button?
}
也可以使用字元串的方式驗證
|
|
|
|
|
|
2,使用注釋方式
判斷使用者是否有 建立賬戶權限
|
|
|
|
|
|
|
判斷使用者角色,如果符合角色,可以使用對應方法
|
|
|
|
|
|
|
3,使用jsp taglib
判斷使用者是否有管理權限
|
|
|
|
|
|
|
|
|
|
|
|
|
從高的級别來看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,程式設計方式配置
例如:
|
|
|
|
2,sessionManager對象圖
如果你想使用sessionManager配置自定義的sessionDao資訊,進行自定義會話管理
|
|
|
|
|
3,INI配置
1) 建立一個INI從SecurityManager
可以從多種方式讀取INI配置檔案的資訊,如檔案系統,類路徑等
|
|
|
|
|
|
|
|
2) 通過Ini執行個體讀取
類似于Properties的方式
|
|
|
|
|
|
|
|
|
|
|
|
加載之後就可以操作INI的配置了。
4,INI配置
每一個節點都是單獨的,不可以重複,注釋可以使用#或者;
配置示例
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1) [main]
配置sessionManager的執行個體和它的依賴。
|
|
|
|
|
|
|
|
定義一個對象
|
|
|
簡單的屬性設定
|
|
|
|
配置資訊将轉入到對應的set方法中
|
|
|
|
參考值
你可以使用$符号引用先前定義的一個對象的執行個體
|
|
|
|
|
嵌套屬性
|
|
|
将被注入到下面的程式中
|
引用其它的屬性
|
|
|
|
|
以鍵值的配置方式
|
|
|
|
|
2) [users]
在使用者比較少的情況下這種配置資訊是有效的
|
|
|
|
3) [roles]
如果角色資訊比較少的情況下可以使用這項配置
|
|
|
|
|
|
|
|
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") 關閉權限
根據以上幾個标簽就可以得到對應的權限資訊