天天看點

SpringBoot -- 整合shiro

1.2 整合shiro

1.2.1 maven坐标

步驟1:在common-parent項目中,添加maven坐标

<!--shiro start-->
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-ehcache</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-spring</artifactId>
        <version>1.3.2</version>
    </dependency>
<!--shiro end-->           

1.2.2 配置過濾器

使用Shiro時,需要配置的相關權限過濾器如下(共10個):

anon: 匿名過濾器,未登陸也可以通路

authc: 認證過濾器, 登陸後通路

perms : 需要xx權限,才能通路

roles: 需要xx角色,才能通路

user: 需要xx使用者,才能通路

port:指定端口才能通路
ssl:必須使用https協定才能通路
logout :登出功能 
rest :根據指定HTTP請求通路才能通路 ,get方式送出 或者 post方式送出才能通路 可
           

1.2.3 配置config類

shiro的配置步驟

1 配置安全管理器SecurityManager

2 realm域配置:由于SecurityManger需要使用realm域,涉及到使用者資訊、權限資訊,處理使用者資訊的時候需要加密

3 密碼比較器:使用者輸入的銘文進行加密,并且與資料庫中的密文進行比較

4 配置生成過濾器的工廠類

/**
 * 在ShiroConfig中做什麼事情呢?
 * 1 配置shiro安全管理器,向安全管理器中注入Realm域
 * 2 配置Realm域:注入密碼比較器
 * 3 配置密碼比較器
 * 4 配置攔截路徑和放行路徑
 */
@Configuration
public class ShiroConfig {
    /**
     * 配置安全管理器,并且注入Realm域
     * @param realm
     * @return
     */
    @Bean
    public SecurityManager securityManager(Realm realm){
        DefaultWebSecurityManager securityManager =  new DefaultWebSecurityManager();
        securityManager.setRealm(realm);
        return securityManager;
    }

    /**
     *  Credentials:憑證/證書 ---
     *
     * 配置Realm域,注入密碼比較器
     * @param credentialsMatcher
     * @return
     */
    @Bean
    public BosRealm realm(CredentialsMatcher credentialsMatcher){
        BosRealm bosRealm = new BosRealm();
        bosRealm.setCredentialsMatcher(credentialsMatcher);
        return bosRealm;
    }

    /**
     * 密碼比較器
     *
     * @return
     */
    @Bean
    public CredentialsMatcher credentialsMatcher(){
//    return new HashedCredentialsMatcher("MD5");
        return new BosCredentialsMatcher();
    }

    /**
     * 配置攔截路徑和放行路徑
     * @param securityManager
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager){
        System.out.println("ShiroConfiguration.shirFilter()");
        // shiro過濾器工廠類
        ShiroFilterFactoryBean shiroFilterFactoryBean  = new ShiroFilterFactoryBean();

        // 必須設定 SecurityManager
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        //攔截器----Map集合
        Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>();

        //配置退出 過濾器,其中的具體的退出代碼Shiro已經替我們實作了
        filterChainDefinitionMap.put("/login*", "anon");
        filterChainDefinitionMap.put("/user/login*", "anon");
        filterChainDefinitionMap.put("/validatecode.jsp*", "anon");
        filterChainDefinitionMap.put("/css/**", "anon");
        filterChainDefinitionMap.put("/js/**", "anon");
        filterChainDefinitionMap.put("/images/**", "anon");
        filterChainDefinitionMap.put("/data/**", "anon");

        //   /** 比對所有的路徑
        //  通過Map集合組成了一個攔截器鍊 ,自頂向下過濾,一旦比對,則不再執行下面的過濾
        //  如果下面的定義與上面沖突,那按照了誰先定義誰說了算
        //  /** 一定要配置在最後
        filterChainDefinitionMap.put("/**", "authc");

        // 将攔截器鍊設定到shiro中
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);


        // 如果不設定預設會自動尋找Web工程根目錄下的"/login.jsp"頁面
        shiroFilterFactoryBean.setLoginUrl("/login.html");
        // 登入成功後要跳轉的連結
        shiroFilterFactoryBean.setSuccessUrl("/index.html");
        //未授權界面;
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");


        return shiroFilterFactoryBean;
    }

    /**
     * 開啟shiro aop注解支援
     * 使用代理方式;是以需要開啟代碼支援
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }


    /**
     * 開啟cglib代理
     * @return
     */
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
        creator.setProxyTargetClass(true);
        return creator;
    }

}           

根據配置檔案報錯顯示,需要建立如下檔案:

1 BosRealm域

2 BosCredentialsMatcher密碼比較器

//自定義Realm ,實作安全資料 連接配接
public class BosRealm extends AuthorizingRealm {

    // 認證...
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
        AuthenticationToken token) {
        System.out.println("shiro 認證管理... ");
        return null;
    }

    @Override
    // 授權...
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) {
        System.out.println("shiro 授權管理...");
        return null;
    }
}           

1.2.5 編寫密碼比較器

public class BosCredentialsMatcher extends SimpleCredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        System.out.println("密碼比較器");
        return false;
    }
}           

授權

//攔截器.
Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>();

//配置退出 過濾器,其中的具體的退出代碼Shiro已經替我們實作了
filterChainDefinitionMap.put("/login*", "anon");
filterChainDefinitionMap.put("/validatecode.jsp*", "anon");
filterChainDefinitionMap.put("/user/login*", "anon");
filterChainDefinitionMap.put("/css/**", "anon");
filterChainDefinitionMap.put("/js/**", "anon");
filterChainDefinitionMap.put("/images/**", "anon");
filterChainDefinitionMap.put("/services/**", "anon");
filterChainDefinitionMap.put("/pages/base/courier**", "perms[courier:list]");
filterChainDefinitionMap.put("/pages/base/area**", "roles[base]");
//<!-- 過濾鍊定義,從上向下順序執行,一般将 /**放在最為下邊 -->:這是一個坑呢,一不小心代碼就不好使了;
//<!-- authc:所有url都必須認證通過才可以通路; anon:所有url都都可以匿名通路-->
filterChainDefinitionMap.put("/**", "authc");

// 如果不設定預設會自動尋找Web工程根目錄下的"/login.jsp"頁面
shiroFilterFactoryBean.setLoginUrl("/login.html");
// 登入成功後要跳轉的連結
shiroFilterFactoryBean.setSuccessUrl("/index.html");
//未授權界面;
shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized.html");```             
下一篇: maven學習