我有一个使用Spring-security-core 2.0的Grails 2.4应用程序,完全没有问题。我的用户位于一个名为Person的域类中,具有其权限,组...通常。
现在,我必须转向SAML身份验证。我正在使用此在线IDP进行测试:
我终于要使用SAML登录,然后继续我的应用程序。
我现在面临的问题是,尽管登录过程是由SAML IDP完成的,但我仍然需要应用程序的用户来管理应用程序内部的权限和其他特征。
因此,我想做的是获取currentUser的用户名,然后输入:
def user = Person.findByUsername(xxxx)
获取相应的应用程序用户(SAML和应用程序中的用户名相同)。
我不知道是否有某种自动的方式,例如通过某种机制将SAML用户连接到与spring-security-core关联的域类中的用户。如果有,请告诉我。
无论如何,问题是我无法恢复当前用户的用户名。我已经尝试过了:
def user = springSecurityService.currentUser
print user
print user?.username
有结果:
grails.plugin.springsecurity.userdetails.GrailsUser@9722b877: Username: AAdzZWNyZXQxjUG6pddUdMdIAMXFuA9E/rDtTg93QsL1PYrP40B34mLtKeh35b7VKlqQz1nFHYb2GWY/pVTmKvxVA0183NL02GWr7aBkWTwk7jedHK99rZdpAOrdM70OR8qMy39A9z/TAHqN; Password: [PROTECTED]; Enabled: false; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Not granted any authorities
AdzZWNyZXQxjUG6pddUdMdIAMXFuA9E/rDtTg93QsL1PYrP40B34mLtKeh35b7VKlqQz1nFHYb2GWY/pVTmKvxVA0183NL02GWr7aBkWTwk7jedHK99rZdpAOrdM70OR8qMy39A9z/TAHqN
此:
def principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal()
print principal
print principal.getUsername()
结果与以前相同
这:
def credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials()
print credentials.getAttributeAsString("username")
这将导致“没有方法getAttributeAsString的签名...”
我作为用户名收到的文本字符串似乎是随机的,因为每次登录时都会不同,尽管我总是使用同一用户登录。
我猜想这种行为可能会根据IDP发生变化,也许我将需要使用的实际IDP会使用清晰的用户名进行响应,但是如果没有,该怎么办?我认为我需要的是相当典型的,SAML可以管理身份验证,但是您仍然需要知道哪个是您的应用程序用户。如果IDP不告诉您,您怎么知道?
如果有帮助,我将SAML配置留在这里:
BuildConfig.groovy:
dependencies {
compile('org.opensaml:opensaml:2.6.1') {
excludes 'commons-codec', 'commons-collections', 'commons-lang', 'esapi', 'jcip-annotations', 'jcl-over-slf4j', 'joda-time', 'jul-to-slf4j', 'junit', 'log4j-over-slf4j', 'logback-classic', 'openws', 'serializer', 'servlet-api', 'slf4j-api', 'spring-core', 'spring-mock', 'testng', 'velocity', 'xalan', 'xercesImpl', 'xml-apis', 'xml-resolver', 'xmlunit'
}
compile('org.opensaml:xmltooling:1.3.4') {
excludes 'bcprov-jdk15', 'commons-codec', 'jcip-annotations', 'jcl-over-slf4j', 'joda-time', 'jul-to-slf4j', 'junit', 'log4j-over-slf4j', 'logback-classic', 'not-yet-commons-ssl', 'serializer', 'slf4j-api', 'testng', 'xalan', 'xercesImpl', 'xml-apis', 'xml-resolver', 'xmlsec', 'xmlunit'
}
compile('org.opensaml:openws:1.4.4') {
excludes 'commons-codec', 'commons-httpclient', 'jcip-annotations', 'jcl-over-slf4j', 'joda-time', 'jul-to-slf4j', 'junit', 'log4j-over-slf4j', 'logback-classic', 'serializer', 'servlet-api', 'slf4j-api', 'spring-core', 'spring-mock', 'testng', 'xalan', 'xercesImpl', 'xml-apis', 'xml-resolver', 'xmltooling', 'xmlunit'
}
compile 'commons-collections:commons-collections:3.2.1'
compile('org.springframework.security.extensions:spring-security-saml2-core:1.0.0.RC2') {
excludes 'spring-security-core'
excludes 'spring-security-web'
}
}
plugins {
compile ':spring-security-saml:2.0.0'
compile ":spring-security-core:2.0.0"
}
Config.groovy
grails.plugin.springsecurity.saml.active = true
grails.plugin.springsecurity.logout.filterProcessesUrl = "/saml/SingleLogout"
grails.plugin.springsecurity.providerNames = ["samlAuthenticationProvider"]
grails.plugin.springsecurity.useSwitchUserFilter = true
grails.plugin.springsecurity.saml.metadata.sp.defaults = [
alias : 'localhost:dev:MyApp',
entityBaseURL: 'http://localhost:8088/MyApp'
]
sp.xml
<?xml version="1.0" encoding="UTF-8"?>
<md:EntityDescriptor entityID="localhost:dev:MyApp" xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata">
<md:SPSSODescriptor AuthnRequestsSigned="true" WantAssertionsSigned="false" protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
<md:Extensions>
<idpdisco:DiscoveryResponse xmlns:idpdisco="urn:oasis:names:tc:SAML:profiles:SSO:idp-discovery-protocol" Binding="urn:oasis:names:tc:SAML:profiles:SSO:idp-discovery-protocol" Location="http://localhost:8088/MyApp/spring-security-saml/login/auth/"/>
</md:Extensions>
<md:KeyDescriptor use="signing">
<ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:X509Data>
<ds:X509Certificate>
MIIC9jCCArSgAwIBAgIETo67pDALBgcqhkjOOAQDBQAwXjELMAkGA1UEBhMCVUsxEDAOBgNVBAgT
B1Vua25vd24xDzANBgNVBAcTBmxvbmRvbjENMAsGA1UEChMEYnVyYjENMAsGA1UECxMEYnVyYjEO
MAwGA1UEAxMFZmVyb3owHhcNMTExMDA3MDg0MzE2WhcNMTIwMTA1MDg0MzE2WjBeMQswCQYDVQQG
EwJVSzEQMA4GA1UECBMHVW5rbm93bjEPMA0GA1UEBxMGbG9uZG9uMQ0wCwYDVQQKEwRidXJiMQ0w
CwYDVQQLEwRidXJiMQ4wDAYDVQQDEwVmZXJvejCCAbgwggEsBgcqhkjOOAQBMIIBHwKBgQD9f1OB
HXUSKVLfSpwu7OTn9hG3UjzvRADDHj+AtlEmaUVdQCJR+1k9jVj6v8X1ujD2y5tVbNeBO4AdNG/y
ZmC3a5lQpaSfn+gEexAiwk+7qdf+t8Yb+DtX58aophUPBPuD9tPFHsMCNVQTWhaRMvZ1864rYdcq
7/IiAxmd0UgBxwIVAJdgUI8VIwvMspK5gqLrhAvwWBz1AoGBAPfhoIXWmz3ey7yrXDa4V7l5lK+7
+jrqgvlXTAs9B4JnUVlXjrrUWU/mcQcQgYC0SRZxI+hMKBYTt88JMozIpuE8FnqLVHyNKOCjrh4r
s6Z1kW6jfwv6ITVi8ftiegEkO8yk8b6oUZCJqIPf4VrlnwaSi2ZegHtVJWQBTDv+z0kqA4GFAAKB
gQDKBDz1DFPPmmWp9n1FskJOev7CnnVFsKji1NLUDdifvS+uW+cnvnDfD3yPdxzUeknCrPTBRp+B
IvYUvLQ57LMIuLgKQ12RujGl0Oz9JbFMAHuBV2I/7ZykzGQPysSEqKCqG+kDc8VZ4AfIf/S8YnQk
xqdWQ5jLTIzXvcWd0WEYbDALBgcqhkjOOAQDBQADLwAwLAIUGP/oZpi79ZM1793XzZvnmrnmz5gC
FBm4bDN8h/0hAa83jaD8joLr098I
</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
</md:KeyDescriptor>
<md:SingleLogoutService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="http://localhost:8088/MyApp/spring-security-saml/saml/SingleLogout"/>
<md:SingleLogoutService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="http://localhost:8088/MyApp/spring-security-saml/saml/SingleLogout"/>
<md:SingleLogoutService Binding="urn:oasis:names:tc:SAML:2.0:bindings:SOAP" Location="http://localhost:8088/MyApp/spring-security-saml/saml/SingleLogout"/>
<md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress</md:NameIDFormat>
<md:NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:transient</md:NameIDFormat>
<md:NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:persistent</md:NameIDFormat>
<md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified</md:NameIDFormat>
<md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName</md:NameIDFormat>
<md:AssertionConsumerService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="http://localhost:8088/MyApp/spring-security-saml/saml/SSO" index="0" isDefault="true"/>
<md:AssertionConsumerService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact" Location="http://localhost:8088/MyApp/spring-security-saml/saml/SSO" index="1" isDefault="false"/>
<md:AssertionConsumerService Binding="urn:oasis:names:tc:SAML:2.0:bindings:PAOS" Location="http://localhost:8088/MyApp/spring-security-saml/saml/SSO" index="2" isDefault="false"/>
</md:SPSSODescriptor>
</md:EntityDescriptor>