Die Kombination von basic-Authentifizierung und login-Formular für den gleichen REST-Api

Ist es ein Weg, um set-up basic-Authentifizierung und login-Formular für den gleichen REST-service? Möchte ich angemeldeten Benutzers auslösen dieser service sowohl über web-browser nach dem loggin in und von der Kommandozeile ausgeführt curl -u username:password hostname.com/api/process
Jetzt habe ich gesehen, dass dieses post: Basic-und Formular-basierte Authentifizierung mit Spring security Javaconfig
aber es ist etwas anderes, was ich versuche zu tun.
Gibt es eine Möglichkeit, um diese Einrichtung mit dem Frühling?
Was ich jetzt habe ist dieses:

package com.my.company.my.app.security;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.JdbcUserDetailsManager;

import javax.sql.DataSource;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    DataSource dataSource;

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SecurityConfig.class);

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/js/**", "/css/**")
                .permitAll();

        http
                .authorizeRequests()
                .antMatchers("/api/**")
                .authenticated()
                .and()
                .httpBasic();

        http
                .authorizeRequests()
                .antMatchers("/","/index")
                .authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/j_spring_security_check")
                .defaultSuccessUrl("/monitor")
                .failureUrl("/login?error")
                .usernameParameter("j_username")
                .passwordParameter("j_password")
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/j_spring_security_logout")
                .logoutSuccessUrl("/login?logout")
                .permitAll()
                .and()
                .csrf()
                .disable();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
                .passwordEncoder(passwordEncoder())
                .usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username=?")
                .authoritiesByUsernameQuery("SELECT username, authority FROM authorities WHERE username=?");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder;
    }
}

Das problem ist nur, dass es nicht umleiten, um mein login-Seite, wenn hostname.com/index oder hostname.com/ aufgerufen wird, statt Fenster pop-ups Fragen, die für die basic-Authentifizierung-Anmeldeinformationen.

  • vielen Dank für die Annahme der Antwort. Könnten Sie mir ein up vote, wenn u don ' T mind!
  • Sichere Sache. Vielen Dank für Ihre Hilfe.
  • Ich habe eine Frage im Zusammenhang mit diesem post also bitte, wenn Sie mir helfen können für, die ? stackoverflow.com/questions/50870308/... @SyntaX
Schreibe einen Kommentar