CASシングルサインオン認証の暗号化MD5暗号化塩の学習(II)データベース

CASシングルサインオン学習(II)データベースを認証

公式サイトCAS説明

ポンポンファイル

必要な認証データベースパッケージの追加
ドライバパッケージを追加mysqlのように、私はMySQLを使用していますが

    <dependencies>
<!--        cas database 认证支持-->
        <dependency>
            <groupId>org.apereo.cas</groupId>
            <artifactId>cas-server-support-jdbc</artifactId>
            <version>${cas.version}</version>
        </dependency>
<!--mysql driver-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>
    </dependencies>

YAML

#SSL配置 证书
server:
ssl:
 enabled: true
 key-store: classpath:thekeystore
 key-store-password: changeit
 key-password: changeit

cas:
authn:
 jdbc:
   query:
      #数据库连接
     - url: jdbc:mysql://127.0.0.1:3306/blog_cas?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL
        =false
       # 用户名
       user: blog-cas
       # 密码
       password: blog-cas
       # 方言
       dialect: org.hibernate.dialect.MySQLDialect
       # 数据库驱动
       driverClass: com.mysql.jdbc.Driver
       #        idleTimeout: 5000
       #查询账号密码SQL,必须包含密码字段
       sql: select  username,password from user where username=?
       # 指定密码字段
       fieldPassword: password

データベースユーザーにテーブルを作成して
ここに画像を挿入説明
起動した後に再パッケージ化、あなたはA / 1がログインしたユーザを使用することができます

MD5暗号化

MD5は不可逆的な、文字列の固定された平文パスワードの長さ(32)のように計算することができます。

cas:
  authn:
    jdbc:
      query:
      	-   passwordEncoder:
            type: DEFAULT
            encodingAlgorithm: MD5

1のmysqlのクエリMD5値、userテーブルのコピー

MD5を選択します( '1')

MD5暗号化塩

CASの公式ウェブサイトは、エンコードデータベース認証を説明
しても、同じ平文パスワードで、あなたは別のパスワード値を持つことができ、あなたは人にパスワードを達成することができ、MD5値のMD5暗号化は、特に安全ではない、二つの同一の平文のパスワードがこの問題に同じ、塩の暗号化ソリューションです。
付加塩は、人がパスワードを達成するためにそうすることを、プライベートとパブリックの塩塩MD5計算値にパスワードを暗号化した後、

YML

# 盐加密
cas:
  authn:
    jdbc:
      encode:
        # jdbc
        -   url: jdbc:mysql://127.0.0.1:3306/blog_cas?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
            # 驱动
            driverClass: com.mysql.jdbc.Driver
            # 方言
            dialect: org.hibernate.dialect.MySQL5Dialect
            # 数据库用户名
            user: blog-cas
            # 数据库密码
            password: blog-cas
            # 加密方式
            algorithmName: MD5
            # 加密迭代次数
            numberOfIterations: 2
            # 密码字段
            passwordFieldName: password
            # 动态盐值
            saltFieldName: username
            # 静态盐值
            staticSalt: 123
            sql: SELECT username,password FROM user WHERE username =?

ソルト値の生成
に必要なパッケージを

	        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.4.0</version>
<!--            <scope>test</scope>-->
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    package jsong;

    import org.apache.shiro.crypto.hash.ConfigurableHashService;
    import org.apache.shiro.crypto.hash.DefaultHashService;
    import org.apache.shiro.crypto.hash.HashRequest;
    import org.apache.shiro.util.ByteSource;
    import org.junit.Test;

    public class CreateSaltPassword {
        // 静态盐值
        private String staticSalt = "123";
        // 加密算法
        private String algorithmName = "MD5";
        // 密码
        private String encodedPassword = "1";
        // 用户名 动态盐值
        private String dynaSalt = "test";
        // 加密迭代次数
        private int numberOfIterations = 2;

        @Test
        public void test() throws Exception {
            ConfigurableHashService hashService = new DefaultHashService();
            hashService.setPrivateSalt(ByteSource.Util.bytes(this.staticSalt));
            hashService.setHashAlgorithmName(this.algorithmName);
            hashService.setHashIterations(this.numberOfIterations);
            HashRequest request = new HashRequest.Builder()
                    .setSalt(dynaSalt)
                    .setSource(encodedPassword)
                    .build();
            String res = hashService.computeHash(request).toHex();
            System.out.println(res);
        }
    }

データベースとユーザ名テスト生成塩値の有無、ユーザ名ので、パスワード同じ、異なるユーザが異なる値塩を有することになる場合であっても
ログインすることができ、テスト/ 1ユーザを使用して
ここに画像を挿入説明

参考記事

公開された83元の記事 ウォン称賛21 ビュー50000 +

おすすめ

転載: blog.csdn.net/JsongNeu/article/details/104227262