cas单点登录-jdbc认证(三)
生活随笔
收集整理的這篇文章主要介紹了
cas单点登录-jdbc认证(三)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
?本節的內容為JDBC認證,查找數據庫進行驗證,其中包括:
- 密碼加密策略(無密碼,簡單加密,加鹽處理)
- 認證策略(jdbc)
一、業務需求
不同的公司,需求業務需求或者架構不一樣導致我們實現驗證的方式不一樣,那么cas為我們提供了很多認證的模式(當然也可以自定義),其中常用的有:
- JDBC認證
- LDAP認證
- Basic認證
- Shiro認證
- Pac4j認證
- MongoDB認證
- Rest認證
- IP黑白名單
還有可能交給第三方認證,例如:微信、QQ、新浪,github等等
當然也有一些公司或者企業也非常的變態,如:
二、加密方案
cas支持jdbc校驗方案:
- 根據sql給予用戶名進行查詢,根據密碼字段進行鑒定(select * from table_users where username=?)可判斷有效等
- 通過鹽等手段進行編碼加密再進行匹配(推薦)
- 根據sql給予用戶名以及密碼進行查詢(select count(x) from tables_users where username = ? and password=?),不可判斷有效期,若數量大于0則成功
- 根據用戶名密碼連接數據庫,原理是通過jdbc,若連接成功則成功
下文會講述前兩種的配置方法
常用單向加密算法:MD5、SHA、HMAC
一般的加密策略的三種:
- 單項加密算法(密碼)
- 單向加密算法(密碼+動態鹽+私有鹽)*加密次數(推薦)
- 不加密(不推薦)
上述提到的加密方案策略,下面都會一一說明白
三、Jdbc認證
3.1、創建數據庫cas,新增一張用戶表sys_user
說明:
- expired字段表示過期,1表示已過期,需要修改密碼
- disabled表示賬號是否禁用,1表示禁用
3.2、sys_user表數據
| id | username | password | expired | disabled | |
| 1 | admin | 845d5f1153c27beed29f479640445148 | 0 | ? | 1 |
| 2 | jacky | caf1a3dfb505ffed0d024130f58c5cfa | 1 | ? | 0 |
| 4 | zhangsan | 68053af2923e00204c3ca7c6a3150cf7 | 0 | ? | 0 |
說明:
- 如果采用MD5加密,那password就是MD5加密后的密文,sha同樣如此
- admin、jacky、zhangsan明文密碼分別是xiaoxiao、321、789
3.2、修改sso-server/pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.carl.auth</groupId><artifactId>sso</artifactId><version>1.0.0</version><relativePath>../pom.xml</relativePath></parent><artifactId>sso-server</artifactId><packaging>war</packaging><name>sso-server</name><description>CAS認證服務,負責各系統的鑒權的鑒權</description><dependencies><dependency><groupId>org.apereo.cas</groupId><artifactId>cas-server-webapp-tomcat</artifactId><version>${cas.version}</version><type>war</type><scope>runtime</scope></dependency><!--新增支持jdbc驗證--><dependency><groupId>org.apereo.cas</groupId><artifactId>cas-server-support-jdbc</artifactId><version>${cas.version}</version></dependency><!--使用mysql驅動--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.apereo.cas</groupId><artifactId>cas-server-support-bom</artifactId><version>${cas.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>com.rimerosolutions.maven.plugins</groupId><artifactId>wrapper-maven-plugin</artifactId><version>0.0.5</version><configuration><verifyDownload>true</verifyDownload><checksumAlgorithm>MD5</checksumAlgorithm></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${springboot.version}</version><configuration><mainClass>org.springframework.boot.loader.WarLauncher</mainClass><addResources>true</addResources></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.1.0</version><configuration><warName>cas</warName><failOnMissingWebXml>false</failOnMissingWebXml><recompressZippedFiles>false</recompressZippedFiles><archive><compress>false</compress><manifestFile>${project.build.directory}/war/work/org.apereo.cas/cas-server-webapp-tomcat/META-INF/MANIFEST.MF</manifestFile></archive><overlays><overlay><groupId>org.apereo.cas</groupId><artifactId>cas-server-webapp-tomcat</artifactId></overlay></overlays></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.3</version></plugin></plugins><finalName>cas</finalName></build> </project>?3.3、application.properties新增配置
#jdbc驗證配置 #Query Database Authentication 數據庫查詢校驗用戶名開始 #查詢賬號密碼sql,必須包含密碼字段 cas.authn.jdbc.query[0].sql=select * from sys_user where username=? #指定上面的sql查詢字段名(必須) cas.authn.jdbc.query[0].fieldPassword=password #指定過期字段,1為過期,若過期需要修改密碼 cas.authn.jdbc.query[0].fieldExpired=expired #為不可用字段段,1為不可用, cas.authn.jdbc.query[0].fieldDisabled=disabled #數據庫方言hibernate的知識 cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect #數據庫驅動cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
#數據庫連接
cas.authn.jdbc.query[0].url=jdbc:mysql://localhost:53306/cas?useUnicode=true&characterEncoding=UTF-8
#數據庫用戶名
cas.authn.jdbc.query[0].user=root
#數據庫密碼
cas.authn.jdbc.query[0].password=123456
#默認加密策略,通過encodingAlgorithm來指定算法,默認NONE不加密
cas.authn.jdbc.query[0].passwordEncoder.type=DEFAULT
cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=UTF-8
cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=MD5
#Query Database Authentication 數據庫查詢校驗用戶名結束 #jdbc驗證配置
以上配置,如驅動,查詢數據庫等等需要根據不同的場景進行調整
- 若密碼無加密,調整passwordEncoder.type=NONE
- 若密碼加密策略為SHA,調整passwordEncoder.encodingAlgorithm=SHA
- 若算法為自定義,實現org.springframework.security.crypto.password.PasswordEncoder接口,并且把類名配置在passwordEncoder.type
3.4、執行流程
例如:輸入admin/xiaoxiao
?
3.5、對密碼進行鹽值處理再加密,application.properties配置文件修改
#Encode Database Authentication 開始 #加密次數 cas.authn.jdbc.encode[0].numberOfIterations=2 #該列名的值可替代上面的值,但對密碼加密時必須取該值進行處理 cas.authn.jdbc.encode[0].numberOfIterationsFieldName= # 鹽值固定列 cas.authn.jdbc.encode[0].saltFieldName=username #靜態鹽值 cas.authn.jdbc.encode[0].staticSalt=. cas.authn.jdbc.encode[0].sql=select * from sys_user_encode where username=? #對處理鹽值后的算法 cas.authn.jdbc.encode[0].algorithmName=MD5 cas.authn.jdbc.encode[0].passwordFieldName=password cas.authn.jdbc.encode[0].expiredFieldName=expired cas.authn.jdbc.encode[0].disabledFieldName=disabled cas.authn.jdbc.encode[0].url=jdbc:hsqldb:mem:cas-hsql-database cas.authn.jdbc.encode[0].dialect=jdbc:mysql://localhost:53306/cas?useUnicode=true&characterEncoding=UTF-8 cas.authn.jdbc.encode[0].user=rootcas.authn.jdbc.encode[0].password=123456
cas.authn.jdbc.encode[0].driverClass=com.mysql.jdbc.Driver
#Encode Database Authentication 結束
4、驗證
4.1、輸入admin/xiaoxiao
?
?4.2、輸入、jacky/321
?
4.3、輸入zhangsan/789
5、總結?
- pom.xm配置引入jdbc支持包,和?數據庫驅動包?
- application.properties增加數據連接配置和加密方式
總結
以上是生活随笔為你收集整理的cas单点登录-jdbc认证(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux系统 qt调试,Linux下Q
- 下一篇: [JMX一步步来] 9、基于JBoss来