È necessario utilizzare DelegatingAuthenticationEntryPoint
per configurare più punti di ingresso. Ciò significa che puoi avere diversi modi di autenticarsi. Di seguito è riportato il codice di esempio:
dbuser punto di ingresso:
public class DBUserAuthencticationEntryPoint extends BasicAuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request,
HttpServletResponse response, AuthenticationException authException)
throws IOException, ServletException {
super.commence(request, response, authException);
}
}
LDAP punto di ingresso:
public class LDAPAuthencticationEntryPoint extends BasicAuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request,
HttpServletResponse response, AuthenticationException authException)
throws IOException, ServletException {
super.commence(request, response, authException);
}
}
Poi è necessario creare RequestMatcher
s di scegliere il punto di ingresso corretta (in base al nome dell'intestazione/regno):
dbuser richiesta matcher:
RequestMatcher dbUserMatcher = new RequestMatcher() {
@Override
public boolean matches(HttpServletRequest request) {
// Logic to identify a DBUser kind of reqeust
}
};
LDAP utente requset matcher:
RequestMatcher ldapMatcher = new RequestMatcher() {
@Override
public boolean matches(HttpServletRequest request) {
// Logic to identify a LDAP kind of reqeust
}
};
Ora abbiamo bisogno di aggiungere queste matchers e punti di ingresso per DelegatingAuthenticationEntryPoint
. In runtime DelegatingAuthenticationEntryPoint
preleva il punto di ingresso e fa l'autenticazione basata sul matcher che restituisce true
.
DBUserAuthencticationEntryPoint dbUserEntryPoint = new DBUserAuthencticationEntryPoint();
LDAPAuthencticationEntryPoint ldapEntryPoint = new LDAPAuthencticationEntryPoint();
LinkedHashMap<RequestMatcher,AuthenticationEntryPoint> entryPoints = new LinkedHashMap<RequestMatcher,AuthenticationEntryPoint>();
entryPoints.put(ldapMatcher, ldapEntryPoint);
entryPoints.put(dbUserMatcher, dbUserEntryPoint);
DelegatingAuthenticationEntryPoint delegatingAuthenticationEntryPoint = new DelegatingAuthenticationEntryPoint(entryPoints);
ora Mappa del DelegatingAuthenticationEntryPoint
al HttpSecurity
nel metodo configure()
:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.
authorizeRequests().
regexMatchers("/login.*").permitAll().
regexMatchers("/api.*").fullyAuthenticated().
and().
formLogin().loginPage("/login").
and().
exceptionHandling().authenticationEntryPoint(delegatingAuthenticationEntryPoint);
}
}
Configurazione del gestore fornitore:
@Bean
public AuthenticationManager authenticationManager() {
return new ProviderManager(Arrays.asList(provider1, provider2);
}
OK, questo sarà lavoro se riesco a reindirizzare alle mie pagine di autenticazione personalizzate (ad esempio, a seconda dell'intestazione HTTP RealmName). Purtroppo non fornisco pagine di autenticazione, ma solo API OAuth. Quindi, quando arriva la richiesta, devo scegliere il provider di autenticazione appropriato basandosi sull'intestazione RealmName e autenticarsi utilizzando le credenziali dall'intestazione http di autorizzazione. – Konrad
No, funzionerà con qualsiasi tipo di processo di autenticazione. Ho appena effettuato il login basato sulla forma per scopi dimostrativi. Rimuovere il codice di accesso basato su modulo e impostare il provider di autenticazione chiamando 'http.authenticationProvider (authenticationProvider)' – Mithun
OK, ma poi mi piacerebbe utilizzare il flusso Oauth standard e dire org.springframework.security.authentication.ProviderManager al gestore di autenticazione specifico dell'utente . Non riesco a farlo dal punto di ingresso dell'autenticazione. – Konrad