魯春利的工做筆記,好記性不如爛筆頭html
SecurityManagerjava
Shiro是從根對象SecurityManager進行身份驗證和受權的;這個對象是線程安全且真個應用只須要一個便可,所以Shiro 提供了SecurityUtils 讓咱們綁定它爲全局的,方便後續操做。mysql
一、純Java 代碼寫法web
@Test public void testProgramSecurityManager () { DefaultSecurityManager securityManager = new DefaultSecurityManager(); // 設置authenticator ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator(); authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy()); securityManager.setAuthenticator(authenticator); // 設置authorizer ModularRealmAuthorizer authorizer = new ModularRealmAuthorizer(); authorizer.setPermissionResolver(new WildcardPermissionResolver()); securityManager.setAuthorizer(authorizer ); // 設置Realm DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/spring_test"); dataSource.setUsername("root"); dataSource.setPassword("Mvtech123!@"); JdbcRealm jdbcRealm = new JdbcRealm(); jdbcRealm.setDataSource(dataSource); // Enables lookup of permissions during authorization. jdbcRealm.setPermissionsLookupEnabled(true); securityManager.setRealm(jdbcRealm); // 將SecurityManager設置到SecurityUtils 方便全局使用 SecurityUtils.setSecurityManager(securityManager); Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken("lucl", "123"); subject.login(token); // 測試經過 Assert.assertTrue(subject.isAuthenticated()); }
二、等價的INI配置spring
shiro-ini-equals-program.inisql
[main] #authenticator authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator authenticationStrategy=org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy authenticator.authenticationStrategy=$authenticationStrategy securityManager.authenticator=$authenticator #authorizer authorizer=org.apache.shiro.authz.ModularRealmAuthorizer permissionResolver=org.apache.shiro.authz.permission.WildcardPermissionResolver authorizer.permissionResolver=$permissionResolver securityManager.authorizer=$authorizer #dataSource dataSource=com.alibaba.druid.pool.DruidDataSource dataSource.driverClassName=com.mysql.jdbc.Driver dataSource.url=jdbc:mysql://localhost:3306/spring_test dataSource.username=root dataSource.password=Mvtech123!@ #realm jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm jdbcRealm.dataSource=$dataSource jdbcRealm.permissionsLookupEnabled=true securityManager.realms=$jdbcRealm
單元測試apache
@Test public void testConfigSecurityManager () { // 一、獲取SecurityManager工廠,此處使用Ini配置文件初始化SecurityManager Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro/ini/shiro-ini-equals-program.ini"); // 二、獲得SecurityManager實例並綁定給SecurityUtils org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); // 三、獲得Subject及建立用戶名/密碼身份驗證Token(即用戶身份/憑證) Subject subject = SecurityUtils.getSubject(); /* * 用戶身份Token 可能不單單是用戶名/密碼,也可能還有其餘的,如登陸時容許用戶名/郵箱/手機號同時登陸。 */ UsernamePasswordToken token = new UsernamePasswordToken("lucl", "123"); try{ // 四、登陸,即身份驗證 subject.login(token); } catch (AuthenticationException e) { // 五、身份驗證失敗 e.printStackTrace(); } // 測試經過 Assert.assertTrue(subject.isAuthenticated()); // 六、退出 subject.logout(); }
說明:安全
即便沒接觸過IoC 容器的知識,如上配置也是很容易理解的:
一、對象名=全限定類名相對於調用public 無參構造器建立對象
二、對象名.屬性名=值至關於調用setter方法設置常量值
三、對象名.屬性名=$對象引用至關於調用setter方法設置對象引用ide
Ini配置文件示例
單元測試
[main] # 提供了對根對象securityManager及其依賴的配置 securityManager=org.apache.shiro.mgt.DefaultSecurityManager ………… securityManager.realms=$jdbcRealm [users] #提供了對用戶/密碼及其角色的配置,用戶名=密碼,角色1,角色2 username=password,role1,role2 [roles] #提供了角色及權限之間關係的配置,角色=權限1,權限2 role1=permission1,permission2 [urls] #用於web,提供了對web url攔截相關的配置,url=攔截器[參數],攔截器 /index.html = anon /admin/** = authc, roles[admin], perms["permission1"]
測試INI配置的身份認證與受權
shiro-ini-config.ini
[users] lucl=123,role1 [roles] role1=system:view:1,system:edit:1,user:update:2
測試代碼
/** * 基於資源的訪問控制(ini配置) */ @Test public void testWhetherHasPermissionInIni () { // 一、獲取SecurityManager工廠,此處使用Ini配置文件初始化SecurityManager Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro/ini/shiro-ini-config.ini"); // 二、獲得SecurityManager實例並綁定給SecurityUtils org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); // 三、獲得Subject及建立用戶名/密碼身份驗證Token(即用戶身份/憑證) Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken("lucl", "123"); try{ // 四、登陸,即身份驗證 subject.login(token); } catch (AuthenticationException e) { // 五、身份驗證失敗 logger.info("用戶身份驗證失敗"); e.printStackTrace(); } // 用戶身份獲得確認 if (subject.isAuthenticated()) { logger.info("用戶登陸成功。"); /** * 進行權限判斷 */ // 判斷是否擁有權限 Assert.assertTrue(subject.isPermitted("system:edit:1")); } else { logger.info("用戶登陸失敗。"); } // 六、退出 subject.logout(); }