Shiro學習筆記(1)——shiro入門
阿新 • • 發佈:2018-12-21
1.建立一個簡單shiro專案
建立一個java工程
加入shiro需要的jar包
在src下建立log4j配置檔案(非必需步驟,可以跳過)
## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.#log4j.rootLogger=INFO, stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout =org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m %n# General Apache librarieslog4j.logger.org.apache=WARN# Springlog4j.logger.org.springframework=WARN# Default Shiro logginglog4j.logger.org.apache.shiro=TRACE# Disable verbose logginglog4j.logger.org.apache.shiro.util.ThreadContext=WARNlog4j.logger.org.apache.shiro.cache.ehcache.EhCache=WARN
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 在src下建立shiro.ini配置檔案
[users]admin = 123,role1[roles]role1 = printer:print
- 1
- 2
- 3
- 4
使用者名稱:admin 密碼:123 為admin這個賬戶賦予role1角色,多個角色使用逗號隔開 role1這個角色擁有
printer:print
這個許可權,關於這個許可權的寫法,後面會講到
- HelloWorld
package com.shiro.helloworld;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.IncorrectCredentialsException;import org.apache.shiro.authc.LockedAccountException;import org.apache.shiro.authc.UnknownAccountException;import org.apache.shiro.authc.UsernamePasswordToken;import org.apache.shiro.config.IniSecurityManagerFactory;import org.apache.shiro.mgt.SecurityManager;import org.apache.shiro.session.Session;import org.apache.shiro.subject.Subject;import org.apache.shiro.util.Factory;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class HelloWorld { //如果不使用日誌,也可以直接用System.out.println(),也就可以不用配置log4j.properties了 private static final transient Logger log = LoggerFactory.getLogger(HelloWorld.class); public static void main(String[] args) { //獲取SecurityManager的例項 Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); Subject currenUser = SecurityUtils.getSubject(); //session的使用 Session session = currenUser.getSession(); session.setAttribute("key", "value"); String value = (String) session.getAttribute("key"); log.info("value:"+value); //如果還未認證 if(!currenUser.isAuthenticated()){ UsernamePasswordToken token = new UsernamePasswordToken("admin","123"); token.setRememberMe(true); try { currenUser.login(token); } catch (UnknownAccountException uae) { log.info("沒有該使用者: " + token.getPrincipal()); } catch (IncorrectCredentialsException ice) { log.info( token.getPrincipal() + " 的密碼不正確!"); } catch (LockedAccountException lae) { log.info( token.getPrincipal() + " 被鎖定 ,請聯絡管理員"); }catch (AuthenticationException ae) { //其他未知的異常 } } if(currenUser.getPrincipal() != null) log.info("使用者 "+currenUser.getPrincipal() +" 登入成功"); //是否有role1這個角色 if(currenUser.hasRole("role1")){ log.info("有角色role1"); }else{ log.info("沒有角色role1"); } //是否有對印表機進行列印操作的許可權 if(currenUser.isPermitted("printer:print")){ log.info("可以對印表機進行列印操作"); }else { log.info("不可以對印表機進行列印操作"); } //退出登入 currenUser.logout(); System.exit(0); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 其實我們使用shiro就為了做兩件事:1. 驗證使用者的身份,2. 驗證該使用者是否有進行某個操作的許可權
執行結果:
2.Shiro的許可權
2.1 簡單字串
用簡單的字串來表示一個許可權,如:queryPrinter
2.2 多層次管理
printer:print
其中第一部分是許可權被操作的領域(印表機),第二部分是被執行的操作
多個值
多個值使用逗號隔開,如role1 = printer:print,printer:query 不一定要xxx:yyyy的形式,也可以直接使用簡單字串
可以使用*號表示所有
比如printer:* ,表示你可以對印表機進行任何操作, 或者使用 *:query ,表示你在任何領域下,都有查詢操作
2.2 例項級訪問控制
這種情況通常會使用三個部件——第一個是域,第二個是操作,第三個是被付諸 實施的例項。如:printer:query:lp7200
也可以使用萬用字元來定義,如: printer:print:* printer:: printer:*:lp7200 printer:query, print:lp7200
部分省略:缺少的部件意味著使用者可以訪問所有與之匹配的值
printer:print 等價於 printer:print:* printer 等價於 printer:: 但是請記住:只能從字串的結尾處省略部件,也就是說 printer:lp7200 於 並不等價於 printer:*:lp7200