公司原有的認證系統,是基於CAS4.1.8,並已經可以工作,此次的要求是實現支持Spnego,期間繞了很大的圈,主要原因還是對AD域的不熟悉。
在開發時要有三台機器,分別是Windows Server(提供DNS服務和AD域服務),Client(即客戶機),Web Application Server(部署CAS應用的服務器,一般為linux系統,這裡我以windows系統為例)
windows server 2012 r2,JDK1.8,win7(client和web application server)
具體參照:http://jingyan.baidu.com/article/e9fb46e17822727520f76652.html
參照:http://www.cnblogs.com/wanggege/p/4605678.html,假設添加的域為:example.com
這裡假設客戶機用戶名為client,應用服務器用戶名為server。
開始->Active Directory 用戶和計算機->Users->新建->用戶,設置賬戶密碼永不過期。
添加用戶後,要修改用戶賬戶選項:選中要修改用戶右鍵->屬性->賬戶,在賬戶選項中,只勾選“該賬戶支持 Kerberos AES 256位加密”,重置用戶密碼。
參照:http://jingyan.baidu.com/article/358570f67d81b2ce4724fcfe.html
在windows server上,運行cmd.exe,輸入命令 setspn -s HTTP/server.example.com@EXAMPLE.COM server,添加spn賬戶,輸入命令 setspn -l server,檢查添加是否成功。
參照:https://technet.microsoft.com/en-us/library/cc753771.aspx,運行cmd.exe,輸入命令舉例:
ktpass /princ HTTP/server.example.com@EXAMPLE.COM /mapuser server /pass ****** /out server.keytab /crypto all /ptype KRB5_NT_PRINCIPAL /mapop set
生成的keytab文件,在cmd進入目錄下,復制server.keytab文件,粘貼到WEB Application server上,記住文件目錄。
IE:Internet選項->高級,勾選“啟用集成 Windows 驗證”;Internet選項->安全->本地Intranet->站點->高級,添加 http://server.example.com,保存
Firefox:在地址欄上輸入“about:config”,進入配置頁面,搜索“negotiate”,為“network.negotiate-auth.trusted-uris”為 http://server.example.com
Java發布的運行環境包中的加解密有一定的限制。比如默認不允許256位密鑰的AES加解密,解決方法就是修改策略文件。官方網站提供了JCE無限制權限策略文件的下載:
JDK7下載地址:http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
JDK8下載地址:http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
相關配置文檔:http://www.javamex.com/tutorials/cryptography/unrestricted_policy_files.shtml
可參照:https://apereo.github.io/cas/4.2.x/installation/SPNEGO-Authentication.html
login-webflow.xml中, viewLoginForm state前,定義兩個新的action state
<action-state id="startAuthenticate"> <evaluate expression="negociateSpnego" /> <transition on="success" to="spnego" /> </action-state> <action-state id="spnego"> <evaluate expression="spnego" /> <transition on="success" to="sendTicketGrantingTicket" /> <transition on="error" to="viewLoginForm" /> </action-state>
找到generateLoginTicket action,替換 viewLoginForm 為 startAuthenticate
定義兩個 bean 在 cas-servlet.xml中
<bean id="negociateSpnego" class="org.jasig.cas.support.spnego.web.flow.SpnegoNegociateCredentialsAction" />
<bean id="spnego" class="org.jasig.cas.support.spnego.web.flow.SpnegoCredentialsAction"
p:centralAuthenticationService-ref="centralAuthenticationService" />
用以下模板更新deployerConfigContext.xml
<bean id="jcifsConfig"
class="org.jasig.cas.support.spnego.authentication.handler.support.JCIFSConfig"
p:jcifsServicePrincipal="HTTP/server.example.com@EXAMPLE.COM"
p:kerberosDebug="false"
p:kerberosRealm="EXAMPLE.COM"
p:kerberosKdc="172.10.1.10"
p:loginConf="/login.conf" />
<bean id="spnegoAuthentication" class="jcifs.spnego.Authentication" />
<bean id="spnegoHandler"
class="org.jasig.cas.support.spnego.authentication.handler.support.JCIFSSpnegoAuthenticationHandler"
p:authentication-ref="spnegoAuthentication"
p:principalWithDomainName="false"
p:NTLMallowed="true" />
<bean id="spnegoPrincipalResolver"
class="org.jasig.cas.support.spnego.authentication.principal.SpnegoPrincipalResolver" />
<bean id="authenticationManager"
class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager">
<constructor-arg>
<map>
<entry key-ref="spnegoHandler" value-ref="spnegoPrincipalResolver"/>
</map>
</constructor-arg>
<property name="authenticationMetaDataPopulators">
<list>
<bean class="org.jasig.cas.authentication.SuccessfulHandlerMetaDataPopulator" />
</list>
</property>
</bean>
提供login.conf 文件,這裡我把文件放到了類路徑下
jcifs.spnego.initiate {
com.sun.security.auth.module.Krb5LoginModule required storeKey=true useKeyTab=true keyTab="C:/Users/server/server.keytab";
};
jcifs.spnego.accept {
com.sun.security.auth.module.Krb5LoginModule required storeKey=true useKeyTab=true keyTab="C:/Users/server/server.keytab";
};
添加
172.10.1.10 server.example.com
修改屬性casServerLoginUrl和serverName,更改其中的ip為server.psso.com