1. 程式人生 > >shiro架構之三------在獨立應用中使用shiro

shiro架構之三------在獨立應用中使用shiro

官網:https://shiro.apache.org/

 

1. 下載
在非Web環境的獨立應用中使用Shiro時,只需要shiro-core元件。
在Maven專案中的依賴配置如下:

複製程式碼

<dependency>
  <groupId>org.apache.shiro</groupId>
  <artifactId>shiro-core</artifactId>
  <version>1.3.2</version>
</dependency>

<!-- Shiro uses SLF4J for logging. We'll use the 'simple' binding in this example app. See http://www.slf4j.org for more info. -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.6.1</version>
</dependency>

複製程式碼

特別地!Shiro使用了日誌框架slf4j,因此需要對應配置指定的日誌實現元件,如:log4j,logback等。
在此,使用slf4j的簡單日誌實現slf4j-simple。


2. 資料來源配置
在Shiro中,Realm定義了訪問資料的方式,用來連線不同的資料來源,如:LDAP,關係資料庫,配置檔案等等。
Realm類圖:

也就是說,可以根據實際需求及應用的許可權管理複雜度靈活選擇指定資料來源。
在此,以org.apache.shiro.realm.text.IniRealm為例,具體配置如下:

shiro.ini:

複製程式碼

# =============================================================================
# Tutorial INI configuration
#
# Usernames/passwords are based on the classic Mel Brooks' film "Spaceballs" :)
# =============================================================================

# -----------------------------------------------------------------------------
# Users and their (optional) assigned roles
# username = password, role1, role2, ..., roleN
# -----------------------------------------------------------------------------
[users]
root = secret, admin
guest = guest, guest
presidentskroob = 12345, president
darkhelmet = ludicrousspeed, darklord, schwartz
lonestarr = vespa, goodguy, schwartz

# -----------------------------------------------------------------------------
# Roles with assigned permissions
# roleName = perm1, perm2, ..., permN
# -----------------------------------------------------------------------------
[roles]
admin = *
schwartz = lightsaber:*
goodguy = winnebago:drive:eagle5

複製程式碼

選擇了資料來源,現在開始使用Shiro進行認證和訪問授權控制。

 

3. 認證
在Shiro中,認證即執行使用者登入,讀取指定Realm連線的資料來源,以驗證使用者身份的有效性與合法性。

複製程式碼

String name = "lonestarr";
String pass = "vespa";
Subject currentUser = SecurityUtils.getSubject();
if(!currentUser.isAuthenticated()) {
  UsernamePasswordToken token = new UsernamePasswordToken(name, pass);
  token.setRememberMe(true);
  try {
    currentUser.login(token);
  } catch (UnknownAccountException e) {
    logger.error(String.format("user not found: %s", name), e);
  } catch(IncorrectCredentialsException e) {
    logger.error(String.format("user: %s pwd: %s error", name, pass), e);
  } catch (ConcurrentAccessException e) {
    logger.error(String.format("user has been authenticated: %s", name), e);
  } catch (AuthenticationException e) {
    logger.error(String.format("account except: %s", name), e);
  }
}
logger.info( "User [" + currentUser.getPrincipal() + "] logged in successfully." );

複製程式碼

 

4. 訪問授權
在Shiro中,訪問授權即驗證使用者是否具備執行指定操作的許可權(角色或許可權驗證)。
特別地!在執行訪問授權驗證之前,必須執行使用者認證。

複製程式碼

String role = "schwartz";
if(currentUser.hasRole(role)) {
  logger.info(String.format("使用者: %s 屬於角色:%s", name, role));
}else{
  logger.error(String.format("使用者: %s 不屬於角色:%s", name, role));
}

String perm = "lightsaber:cc";
if(currentUser.isPermitted(perm)) {
  logger.info(String.format("使用者: %s 擁有許可權:%s", name, perm));
}else {
  logger.error(String.format("使用者:%s 沒有許可權:%s", name, perm));
}

複製程式碼

 

5. 完整示例
詳見:https://git.oschina.net/cchanghui/test-shirocli.git