1. 程式人生 > >spring boot 與 shiro的簡單整合使用

spring boot 與 shiro的簡單整合使用

scheduler div turn map 用戶 ttr algorithm pen enc

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

Apache Shiro是一個功能強大且易於使用的Java安全框架,可執行身份驗證,授權,加密和會話管理。借助Shiro易於理解的API,您可以快速輕松地保護任何應用程序 - 從最小的移動應用程序到最大的Web和企業應用程序。spring中也有自帶的安全框架spring security。shrio是通過對其的再封裝,實現了自己的一套全新架構。

正巧spring boot項目中也需要用到用戶的身份驗證以及權限控制,本來想用AOP自己寫一套的,但是最終還是選擇了shiro,通過與前輩的共同戰鬥,最終還是把它實現了出來。

1.直接上配置類:

/**
 * 
 * shiro配置類
 * @author wuzz
 * @Date   2018年4月30日
 *
 */
@Configuration
public class ShiroConfiguration {
    /**
     * LifecycleBeanPostProcessor,這是個DestructionAwareBeanPostProcessor的子類,
     * 負責org.apache.shiro.util.Initializable類型bean的生命周期的,初始化和銷毀。
     * 主要是AuthorizingRealm類的子類,以及EhCacheManager類。
     */
    @Bean(name = "lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    /**
     * HashedCredentialsMatcher,這個類是為了對密碼進行編碼的,
     * 防止密碼在數據庫裏明碼保存,當然在登陸認證的時候,
     * 這個類也負責對form裏輸入的密碼進行編碼。
     */
    @Bean(name = "hashedCredentialsMatcher")
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        credentialsMatcher.setHashAlgorithmName("MD5");
        credentialsMatcher.setHashIterations(1024);
        credentialsMatcher.setStoredCredentialsHexEncoded(true);
        return credentialsMatcher;
    }

    /**ShiroRealm,這是個自定義的認證類,繼承自AuthorizingRealm,
     * 負責用戶的認證和權限的處理,可以參考JdbcRealm的實現。
     */
    @Bean(name = "shiroRealm")
    @DependsOn("lifecycleBeanPostProcessor")
    public PermissionsShiroRealm shiroRealm() {
    	PermissionsShiroRealm realm = new PermissionsShiroRealm();//這個類需要自己編寫 下面會貼出其實現
        realm.setCredentialsMatcher(hashedCredentialsMatcher());
        return realm;
    }

    /**
     * EhCacheManager,緩存管理,用戶登陸成功後,把用戶信息和權限信息緩存起來,
     * 然後每次用戶請求時,放入用戶的session中,如果不設置這個bean,每個請求都會查詢一次數據庫。
     */
//    @Bean(name = "ehCacheManager")
//    @DependsOn("lifecycleBeanPostProcessor")
//    public EhCacheManager getEhCacheManager(){  
//        EhCacheManager ehcacheManager = new EhCacheManager();  
//        ehcacheManager.setCacheManagerConfigFile("classpath:ehcache.xml");  
//        return ehcacheManager;  
//    }  

    /**
     * SecurityManager,權限管理,這個類組合了登陸,登出,權限,session的處理,是個比較重要的類。
//     */
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager securityManager(PermissionsShiroRealm shiroRealm ,SessionManager sessionManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(shiroRealm);
//        securityManager.setCacheManager(getEhCacheManager());
        securityManager.setSessionManager(sessionManager);
        return securityManager;
    }

    /**
     * ShiroFilterFactoryBean,是個factorybean,為了生成ShiroFilter。
     * 它主要保持了三項數據,securityManager,filters,filterChainDefinitionManager。
     */
    @Bean(name = "shiroFilter")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(org.apache.shiro.mgt.SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

//        Map<String, Filter> filters = new LinkedHashMap<>();
//        LogoutFilter logoutFilter = new LogoutFilter();
//        logoutFilter.setRedirectUrl("/api/1.0/loginout");
//        filters.put("logout",null);
//        shiroFilterFactoryBean.setFilters(filters);
        Map<String, String> filterChainDefinitionManager = new LinkedHashMap<String, String>();
        filterChainDefinitionManager.put("/api/1.0/logout", "logout");//登出URL
        filterChainDefinitionManager.put("/api/1.0/login", "anon");//登陸URL
        filterChainDefinitionManager.put("/api/1.0/nologin", "anon");//未登錄跳轉的URL
//      filterChainDefinitionManager.put("/user/edit/**", "authc,perms[user:edit]");// 這裏為了測試,固定寫死的值,也可以從數據庫或其他配置中讀取,此處是用權限控制
        filterChainDefinitionManager.put("/**", "user");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionManager);
        shiroFilterFactoryBean.setLoginUrl("/api/1.0/nologin");
        
//        shiroFilterFactoryBean.setUnauthorizedUrl("/api/1.0/unauth");
        return shiroFilterFactoryBean;
    }

    /**
     * DefaultAdvisorAutoProxyCreator,Spring的一個bean,由Advisor決定對哪些類的方法進行AOP代理。
     */
    @Bean
    @ConditionalOnMissingBean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
        defaultAAP.setProxyTargetClass(true);
        return defaultAAP;
    }

    /**
     * AuthorizationAttributeSourceAdvisor,shiro裏實現的Advisor類,
     * 內部使用AopAllianceAnnotationsAuthorizingMethodInterceptor來攔截用以下註解的方法。
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(org.apache.shiro.mgt.SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor aASA = new AuthorizationAttributeSourceAdvisor();
        aASA.setSecurityManager(securityManager);
        return aASA;
    }
    
    @Bean  
    public DefaultWebSessionManager configWebSessionManager(RedisSessionDao sessionDao) {  
		MySessionManager manager = new MySessionManager();  
		manager.setSessionDAO(sessionDao);// 設置SessionDao  
		manager.setDeleteInvalidSessions(true);// 刪除過期的session  
		manager.setSessionValidationSchedulerEnabled(false);// 是否定時檢查session 
        return manager;
    }
    
    
    public RedisSessionDao configRedisSessionDao() {
    	return new RedisSessionDao() ;
    }


}

  

spring boot 與 shiro的簡單整合使用