provide test for custom principal extractor config

Signed-off-by: Sola <dev@sola.love>
This commit is contained in:
Sola 2018-09-05 23:35:10 +08:00 committed by Rob Winch
parent 2980f96b55
commit c60fcf263e
2 changed files with 36 additions and 2 deletions

View File

@ -13,7 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.config.annotation.web.configurers;
package org.springframework.security.config.annotation.web.configurers
import sun.security.x509.X500Name
import java.security.cert.CertificateFactory
import java.security.cert.X509Certificate
@ -162,6 +164,38 @@ public class NamespaceHttpX509Tests extends BaseSpringSpec {
}
}
def "http/x509@custom-principal-extractor"() {
setup:
X509Certificate certificate = loadCert("rodatexampledotcom.cer")
loadConfig(CustomPrincipalExtractorConfig)
when:
request.setAttribute("javax.servlet.request.X509Certificate", [certificate] as X509Certificate[] )
springSecurityFilterChain.doFilter(request, response, chain)
then:
response.status == 200
authentication().name == 'rod@example.com'
}
@EnableWebSecurity
public static class CustomPrincipalExtractorConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.
inMemoryAuthentication()
.withUser("rod@example.com").password("password").roles("USER","ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().hasRole("USER")
.and()
.x509()
.x509PrincipalExtractor{ (it.subjectDN as X500Name).commonName }
}
}
def "http/x509@user-service-ref"() {
setup:
X509Certificate certificate = loadCert("rodatexampledotcom.cer")

View File

@ -55,6 +55,6 @@ class X509ConfigurerTests extends BaseSpringSpec {
.and()
.x509()
then:
http.getConfigurer(X509Configurer).subjectPrincipalRegex == ".*"
http.getConfigurer(X509Configurer).x509PrincipalExtractor.subjectDnPattern.toString() == ".*"
}
}