Ich habe LDAP-Authentifizierung mit Spring Security in einer Spring Boot-Anwendung implementiert. Benutzeranmeldeinformationen werden über ein HTTP-Anmeldeformular übermittelt. Benutzername wird als {0}
in .userDnPatterns("cn={0},ou=institution,ou=people")
genommen. Allerdings, wenn ich Rest Api mit Curl-Befehl aufrufen wollte, wie sollte ich den Benutzernamen und das Passwort von Curl (oder benutzerdefinierte HTML-Seite)? Mit anderen Worten, wie kann man eine Variable von der POST-Anfrage an die Config-Klasse übergeben?So führen Sie den curl-Befehl aus, um rest api mit der Spring-Sicherheit aufzurufen
Meine websecurityconfig Klasse ist wie folgt:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin();
}
@Configuration
protected static class AuthenticationConfiguration extends
GlobalAuthenticationConfigurerAdapter {
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource("ldap://ldap.xxx.edu:389/dc=mdanderson,dc=edu");
contextSource.setUserDn("cn=ris_flow,ou=service accounts,ou=institution,ou=service accounts,dc=mdanderson,dc=edu");
contextSource.setPassword("xxxyyyzzz");
contextSource.afterPropertiesSet();
LdapAuthenticationProviderConfigurer<AuthenticationManagerBuilder> ldapAuthenticationProviderConfigurer = auth.ldapAuthentication();
ldapAuthenticationProviderConfigurer
.userDnPatterns("cn={0},ou=institution,ou=people")
.userSearchBase("")
.contextSource(contextSource);
}
}
}
EDIT:
Als ich
curl -d "username=djiao&password=xxxyyyzzz" http://localhost:8080/ristore/foundation/TRF134936
führen Sie es nichts zurückgibt. Die gleiche URL funktioniert jedoch in einem Browser. Durch das Aktivieren von verbose mit -v
, hier ist die Ausgabe des Curl-Befehls.
* Hostname was NOT found in DNS cache
* Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> POST /ristore/foundation/TRF134936 HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:8080
> Accept: */*
> Content-Length: 35
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 35 out of 35 bytes
< HTTP/1.1 302 Found
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Set-Cookie: JSESSIONID=FE8C94FC8B445A2C8AA1FCFBB5F2826D; Path=/; HttpOnly
< Location: http://localhost:8080/login
< Content-Length: 0
< Date: Wed, 27 Apr 2016 02:19:43 GMT
<
* Connection #0 to host localhost left intact
Vielleicht nur einen Kommentar hier: Wenn Sie für eine Rest-API gehen möchte, würde ich nicht Formular Login aber entweder Basic-Authentifizierung oder OAuth verwenden. –
@ daniel.eichten Einverstanden. Formular-Login ist nur etwas, das ich ldap-Authentifizierung testen wollte. Wird definitiv html Login-Seite für Rest apis – Nasreddin