log4j log4j log4j.rootLogger=INFO, stdout log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.app ...
package com.cun; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.*; import org.apache.shiro.config.IniSecurityManagerFactory; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.session.Session; import org.apache.shiro.subject.Subject; import org.apache.shiro.util.Factory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Quickstart { private static final Logger log = LoggerFactory.getLogger(Quickstart.class); public static void main(String[] args) { //創建配置的Shiro SecurityManager的最簡單方法 //領域,用戶,角色和許可權是使用簡單的INI配置。 //我們將通過使用可以提取.ini文件的工廠來實現這一點 //返回一個SecurityManager實例 //使用類路徑根目錄下的shiro.ini文件 //(文件:和url:首碼分別從文件和網址載入): @SuppressWarnings("deprecation") Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); //對於這個簡單的示例快速入門,請創建SecurityManager可作為JVM單例訪問。 大多數應用程式不會這樣做 //而是依賴於他們的容器配置或web.xml webapps。 SecurityUtils.setSecurityManager(securityManager); //現在設置了一個簡單的Shiro環境,讓我們看看你能做些什麼: //獲取當前執行的用戶: Subject currentUser = SecurityUtils.getSubject(); //用Session做一些事情(不需要web或EJB容器!!!) Session session = currentUser.getSession(); session.setAttribute("sessionKey", "aValue"); String value = (String) session.getAttribute("sessionKey"); if (value!=null&&value.equals("aValue")) { log.info("檢索正確的值: [" + value + "]"); }else{ log.info("檢索錯誤的值: [" + value + "]"); } //讓我們登錄當前用戶,以便我們檢查角色和許可權: if (!currentUser.isAuthenticated()) { UsernamePasswordToken token = new UsernamePasswordToken("admin", "123456"); token.setRememberMe(true); try { currentUser.login(token); } catch (UnknownAccountException uae) { log.info("捕獲未知用戶名異常:" + token.getPrincipal()); } catch (IncorrectCredentialsException ice) { log.info("捕獲密碼錯誤異常: " + token.getPrincipal()); } catch (LockedAccountException lae) { log.info("賬號鎖住: " + token.getPrincipal()); }catch (ExcessiveAttemptsException eae) { log.info("賬號錯誤登錄過多異常:: " + token.getPrincipal()); } // ...在這裡捕獲更多例外(也許是特定於您的應用程式的自定義? catch (AuthenticationException ae) { //unexpected condition? error? } } //判斷是否登錄成功: if(currentUser.getPrincipal()!=null) log.info("用戶: [" + currentUser.getPrincipal() + "] 登錄成功."); else log.warn("登錄失敗!"); //測試角色: if (currentUser.hasRole("schwartz")) { log.info("你有Schwartz角色!"); } else { log.info("sorry,你只是普通用戶沒有Schwartz角色"); } //測試有沒該領域的許可權 if (currentUser.isPermitted("lightsaber:wield")) { log.info("你被許可:lightsaber:wield。明智地使用它."); } else { log.info("Sorry, lightsaber:wield訪問域僅僅屬於 schwartz角色."); } // (非常強大)實例級別許可權: if (currentUser.isPermitted("winnebago:drive:eagle5")) { log.info("你被允許(id) 'eagle5'的'drive' winnebago " + "這是鑰匙 - 玩得開心!"); } else { log.info("Sorry,你不被允許'eagle5'winnebago!"); } //退出 currentUser.logout(); System.exit(0); } }
shiro.ini [users] root = 123, admin guest = 123456, guest presidentskroob = 123456, president darkhelmet = 123456, darklord, schwartz lonestarr = 123456, goodguy, schwartz [roles] admin = * schwartz = lightsaber:* goodguy = winnebago:drive:eagle5
log4j
-
log4j log4j.rootLogger=INFO, stdout log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n # General Apache libraries log4j.logger.org.apache=WARN # Spring log4j.logger.org.springframework=WARN # Default Shiro logging log4j.logger.org.apache.shiro=TRACE # Disable verbose logging log4j.logger.org.apache.shiro.util.ThreadContext=WARN log4j.logger.org.apache.shiro.cache.ehcache.EhCache=WARN
View Code
相對通用的maven
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.11.RELEASE</version> <relativePath /> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- SpringBoot中使用 Shiro 做用戶、角色、許可權管理 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.0</version> </dependency> <!-- swagger生成介面API --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <!-- 介面API生成html文檔 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> </dependencies>View Code