Spring Boot + Vaadin API Security

OnDemand

Top Contributor
Hallo zusammen,

habe folgende Situation:

auf verschiedenen Servern laufen unsere Anwendungen, wir haben einen zentralen "AdminService".
Nun soll sich UserService per API Daten vom AdminService holen, mittels BasicAuth.

Ich bekomme es einfach nicht hin, alle Aufrufe an "/api**" NICHT über die DB-Loginroutine laufen zu lassen, sondern über BasicAuth
Kann mir jemand helfen?

Die Sicherheitskonfig grätscht immer dazischen. Ich möchte quasi alles was an /api** geht über http BasicAuth leiten. Geht das überhaupt?

Java:
@Override
    protected void configure(HttpSecurity http) throws Exception {
        // Not using Spring CSRF here to be able to use plain HTML for the login page
        http.csrf().disable()
                .requestCache().requestCache(new CustomRequestCache())
                .and().authorizeRequests()
                .requestMatchers(SecurityUtils::isFrameworkInternalRequest).permitAll()
                .anyRequest().authenticated()
                .and().formLogin()
                .loginPage(LOGIN_URL).permitAll()
                .loginProcessingUrl(LOGIN_PROCESSING_URL)
                .failureUrl(LOGIN_FAILURE_URL)
                .and().logout().logoutSuccessUrl(LOGOUT_SUCCESS_URL);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authProvider());
    }

    @Bean
    public DaoAuthenticationProvider authProvider() {
        DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(userDetailsService);
        authProvider.setPasswordEncoder(encoder());
        return authProvider;
    }

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


    /**
     * Allows access to static resources, bypassing Spring security.
     */
    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers(
                // Client-side JS
                "/VAADIN/**",

                // the standard favicon URI
                "/favicon.ico",

                //Alles unter Public ohne Passwort erreichbar machen
                "/register",

                // the robots exclusion standard
                "/robots.txt",

                // web application manifest
                "/manifest.webmanifest",
                "/sw.js",
                "/offline.html",

                // icons and images
                "/icons/**",
                "/images/**",
                "/styles/**",

                // (development mode) H2 debugging console
                "/h2-console/**");
    }
 
Hab es wie folgt gelöst. Eine weitere SecuritryConfig nur für Rest, welche mit @Order(1) zuerst "gilt"

Java:
@EnableWebSecurity
@Configuration
public class SecurityConfigurationRest extends WebSecurityConfigurerAdapter {

    @Configuration
    @Order(1)
    public static class RestSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.cors().and().csrf().disable()
                    // make sure to only make rules for /api/**  - everything else will be defined in WebSecurityConfigurationAdapter
                    .antMatcher("/api/**").authorizeRequests().anyRequest().authenticated().and()
                    .httpBasic();
        }

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
                    .withUser("user").password("{noop}pass").roles("USER");
        }

        @Bean
        public PasswordEncoder encoderRest() {
            return new BCryptPasswordEncoder(5, new SecureRandom());
        }
    }
 

Zurück
Oben