2016-08-23 109 views
0

我有一個遺留應用程序,我從用戶密碼轉換爲使用基於證書的認證。我使用的Tomcat 7,和我的應用程序需要支持基於應用程序的URL 4種安全的用戶試圖訪問Spring Security與X509在各級認證

例如: 少數的servlet將使用HTTP 少數的servlet將使用HTTPS沒有任何認證 幾個servlet將使用PKI使用相互認證,但用戶不會事先知道,因此任何具有cert鏈中有效證書的用戶都將被授予訪問權限。 應用程序的重置是管理,因此它將基於預定義的用戶證書CN進行基於角色的訪問。

我有一個連接器運行HTTPS在443 clientAuth =「希望」在我的Tomcat的server.xml

我有具有springSecurityFilterChain,列出我想要求HTTPS

任何URL我的應用程序的web.xml

我的springSecurityContext.xml有一個x509部分,它具有從屬性文件讀取預先註冊的用戶和角色的subject-principal-regex =「CN =(。*?),」user-service-ref =「userDetailsS​​ervice」 。從那裏,我列出了攔截URL模式,我想要哪些模式需要哪些角色,以獲得require-channel =「https」。

我還添加了,我只想通過設置訪問它需要HTTPS的URL =「IS_AUTHENTICATED_ANONYMOUSLY」作爲攔截的URL

這讓我的4只要求我,因爲上述任何意志的網址需要3使用spring安全性自動將HTTP從HTTP重定向到HTTPS,並根據角色強制執行用戶憑證,或根本不檢查客戶端證書。未放入web.xml springSecurityFilterChain的url-patterns將接受HTTP連接。

我無法弄清楚的是最終模式,這是應用程序要求客戶端證書,這將在證書鏈中針對CA進行驗證,而不是針對我已知的具有角色的用戶數據庫。他們將擁有有效的證書,我需要訪問客戶端證書才能發現,但實際上並未在我的系統中知曉,因爲他們只是應用程序的普通用戶。我不太清楚如何完成此操作。

下面是我的3個XML文件中的一些片段,以顯示我在說什麼。我只在springSecurity中包含了httpsandcertlink,以顯示我正在嘗試添加哪一個,但無法工作。

server.xml中

​​
maxThreads="150" SSLEnabled="true" scheme"https" secure="true"
clientAuth="want" sslProtocol"TLS"
keystoreFile="doesntmatter"
truststoreFile="doesntmatter" />

網絡。XML

<login-config>
<auth-method>CLIENT-CERT</auth-method>
<realm-name>MYAPP</realm-name>
</login-config>

<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>*.go</url-pattern>
<url-pattern>/httpsonlylink</url-pattern>
<url-pattern>/httpsandcertlink</url-pattern>
</filter-mapping>

springSecurityContext.xml

<security:http xmlns="http://www.springframework.org/schema/security" >
<security:x509 subject-principal-regex="CN=(.*?)," user-service-ref="userDetailsService" />
<security:intercept-url pattern="/management**" requires-channel="https" access="ROLE_MGMT" />
<security:intercept-url pattern="/httpsonlylink**" requires-channel="https" access="IS_AUTHENTICAED_ANONYMOUSLY" />
<security:intercept-url pattern="/httpsandcertlink**" requires-channel="https" --not sure what to change here-- />
</security:http>

<security:authentication-manager xmlns="http://www.springframework.org/schema/security" alias="authenticationManager">
<security:authentication-provider>
<security:user-service id="userDetailsService" properties="\WEB-INF\users.properties" />
</security:authentication-provider>
</security:authentication-manager>

回答

0

一種方法是通過創建一個額外的 「有效證件」 的角色捎帶的角色要求缺少的必要條件所有用戶a重新自動分配。應該已經需要一個有效的證書鏈,因此應該要求的一切就是設置一個攔截URL模式,以匹配任何需要有效證書並需要「有效證書」角色的URL,確保此規則匹配之後需要特定角色的人。

+0

所以我想到了這一點,但我無法找到一個例子。現在,我有一個身份驗證提供程序,其中包含一行:user-service id =「userDetailsS​​ervice」properties =「\ WEB-INF \ users.properties」。我只是不確定如何爲不在該列表中的任何用戶設置默認角色。我是否需要編寫一個java類來做到這一點,或者可以在.XML中完成嗎? – ffxhokie

+0

這裏將是一個自定義的userDetailsS​​ervice實現的例子:http://howtodoinjava.com/spring/spring-security/custom-userdetailsservice-example-for-spring-3-security/你可能會更簡單,它不會需要任何數據源配置,並且只需要通過'super'重寫'loadUserByUsername()',並通過'super'調用它的父項,並將「valid certificate」角色添加到結果中,創建一個具有該角色的新用戶對象如果'super'返回null或以其他方式指示它找不到用戶。 Spring文檔中應該有一個類似的例子。 –

+0

謝謝,這是我最終做的。我創建了一個自定義的userDetailsS​​ervice實現,該實現爲任何用戶分配角色,發現與否,並最終通過一個簡單的spring安全實現進行工作。謝謝! – ffxhokie