レルムの4つの用法史郎認証局

一、SimpleAccountRealm 

パブリッククラスAuthenticationTest {
    
    SimpleAccountRealm SARは新しいSimpleAccountRealmを()=;
    
    @前
    ます。public voidアドユーザー(){
        sar.addAccount( "マーク"、 "123456"、 "管理者"、 "ユーザー");
    }
    
    @テスト
    ます。public void testAuthentication(){
        // 1. Seruritymanagerビルド環境
        DefaultSecurityManager DSM =新しいDefaultSecurityManager();
        dsm.setRealm(SAR)。
        
        トピックを提出する// 2.認証要求
        SecurityUtils.setSecurityManager(DSM)。
        対象被写体= SecurityUtils.getSubject()。
        
        UsernamePasswordTokenトークン=新UsernamePasswordToken( "マーク"、 "123456");
        subject.login(トークン)。
        
        System.out.println( "にisAuthenticated:" + subject.isAuthenticated());
        
        subject.checkRoles( "管理者"、 "利用者")。

    }

 

二つ。IniRealm

パブリッククラスIniRealmTest {
 
    
    @テスト
    ます。public void testIniRealm(){
        IniRealm分野=新IniRealm( "クラスパス:user.ini");
        DefaultSecurityManager defaultSerurityManager =新しいDefaultSecurityManager();
        defaultSerurityManager.setRealm(レルム)。
        
        SecurityUtils.setSecurityManager(defaultSerurityManager)。
        対象被写体= SecurityUtils.getSubject()。
        
        UsernamePasswordToken UPT =新UsernamePasswordToken( "マーク"、 "123456");
        
        subject.login(UPT)。
        
        System.out.println( "isAuthentication:" + subject.isAuthenticated());
        
        subject.checkRole( "管理者")。
        
        subject.checkPermission( "ユーザー:アップデート");
        
    }
}

三、JDBCレルム

パブリッククラスJDBCRealmTest {
    
    DruidDataSourceのdataSource =新しいDruidDataSource();
    {
        dataSource.setUrl( "JDBCます。mysql:// localhostを:3306 / XXXX");
        dataSource.setUsername( "ルート");
        dataSource.setPassword( "ルート");
    }
    
    @テスト
    ます。public void testJDBCRealm(){
        JDBCレルムレルム=新しいJDBCレルム();
        realm.setDataSource(データソース)。
        (真)realm.setPermissionsLookupEnabled。
        そうしないと//独自のSQL、データベース名とテーブル名は、デフォルトのクエリステートメントをSHIROしなければならない通常の状況下では、一貫性のある次のように、カスタムSQLを使用しています:
        文字列のSQL =「USER_NAMEは= TEST_USERから選択パスワード?」。
        realm.setAuthenticationQuery(SQL)。
        文字列roleSql = "USER_NAMEは= test_user_rolesからROLE_NAMEを選択?"。
        realm.setUserRolesQuery(roleSql)。
        文字列permissionSql = "ROLE_NAME = test_roles_permissionsからselectパーミッション?"。
        realm.setPermissionsQuery(permissionSql)。
        
        DefaultSecurityManager DSM =新しいDefaultSecurityManager();
        dsm.setRealm(レルム)。
        
        SecurityUtils.setSecurityManager(DSM)。
        対象被写体= SecurityUtils.getSubject()。
        
        UsernamePasswordTokenトークン=新しいUsernamePasswordToken( "XM"、 "123")。
        subject.login(トークン)。
        
        System.out.println( "isAuthencation:" + subject.isAuthenticated());
        
        subject.checkRole( "管理者")。
        subject.checkRoles( "管理者"、 "利用者")。
        subject.checkPermission( "ユーザー:削除");
    }
 
}

第四に、カスタム・レルム

パブリッククラスcustomRealmTest {
    
    @テスト
    ます。public void testCustomRealm(){
        CustomRealmレルム=新しいCustomRealm();
        
        DefaultSecurityManager SDM =新しいDefaultSecurityManager();
        sdm.setRealm(レルム)。
        
        HashedCredentialsMatcher HCM =新しいHashedCredentialsMatcher();
        hcm.setHashAlgorithmName( "MD5");
        hcm.setHashIterations(1)。
        
        realm.setCredentialsMatcher(HCM)。
        
        SecurityUtils.setSecurityManager(大さじ)。
        対象被写体= SecurityUtils.getSubject()。
        
        UsernamePasswordTokenトークン=新UsernamePasswordToken( "マーク"、 "123456");
        subject.login(トークン)。
        System.out.println( "isAuthencation:" + subject.isAuthenticated());
        
        subject.checkRole( "管理者")。
        subject.checkRoles( "管理者"、 "利用者")。
        
        subject.checkPermission( "ユーザー:削除");
    }
 
}
 
パブリッククラスCustomRealmはAuthorizingRealmを{延び
    
    地図<文字列、文字列>ユーザマップ=新しいHashMapの<>();
    
    {
        //シミュレーションのデータベースのデータをチェックしてください
        userMap.put( "マーク"、 "73bea81c6c06bacab41a995495239545");
        super.setName( "customReal");
    }
 
    @オーバーライド
    保護AuthorizationInfo doGetAuthorizationInfo(PrincipalCollectionプリンシパル){
 
        ストリングのuserName =(文字列)principals.getPrimaryPrincipal();
        //データベースやキャッシュのユーザー名の役割を取得します。
        セットの<string> = getRolesByUserNameロール(ユーザ)
        セットの<string> premissions = getpremissionsByUserName(ユーザ名)。
        SimpleAuthorizationInfo情報=新しいSimpleAuthorizationInfo();
        info.setStringPermissions(premissions)。
        info.setRoles(ロール);
        情報を返します。
    }
 
    プライベート設定<文字列> getpremissionsByUserName(文字列のuserName){
        セット<文字列>パーミッション=新しいHashSetの<>();
        permission.add( "ユーザー:削除");
        許可を返します。
    }
 
    プライベート設定<文字列> getRolesByUserName(文字列のユーザー){
        セット<文字列>の役割=新しいHashSetの<>();
        roles.add( "管理者")。
        roles.add( "ユーザー");
        役割を返します。
    }
 
    @オーバーライド
    保護AuthenticationInfo doGetAuthenticationInfo(AuthenticationTokenトークン)を含むAuthenticationException {スロー
        1 //メインパスオーバーを介してユーザ名情報を入手
        ストリングのuserName =(文字列)token.getPrincipal();
        資格を取得する// 2.ユーザ名データベース
        文字列のパスワード= getPassowrdByUserName(ユーザー名);
        もし(パスワード== NULL){
            ヌルを返します。
        }
        
        SimpleAuthenticationInfo情報=新SimpleAuthenticationInfo( "マーク"、パスワード、 "customReal");
        //塩 - 塩の値が設定されるべきデータベースパスワードの暗号文の塩、もし
        info.setCredentialsSalt(ByteSource.Util.bytes( "土地"));
        情報を返します。
    }
 
    プライベート文字列getPassowrdByUserName(文字列のuserName){
        //実際にはデモを容易にするためのデータベースをチェックします
        リターンuserMap.get(ユーザー名);
    }
 
    パブリック静的無効メイン(文字列[] args){
        System.out.println((INT)(1 + Math.random()* 10))。
// Md5Hash HSH =新Md5Hash( "123456"); // MD5暗号化
        Md5Hash HSH =新Md5Hash( "123456"、 "マーク"); // MD5暗号化とより多くの塩を確保
        System.out.println(HSH)。
    }
    
 
}

おすすめ

転載: www.cnblogs.com/wlv1314/p/12115946.html