2017-05-17 3 views
0

Ich habe das Gefühl, dass ich mit Springbootsicherheit für immer kämpfen werde. Was ich will (@least) ist recht einfach: ein REST-Dienst mit 3 verschiedenen Sicherheitsstufen:SpringBoot + REST + Sicherheit - entweder alle Zugriffe offen oder alle geschlossen

  • A "public" RestService zugänglich von allen
  • A "geschützt" RestService für "USER" s
  • A „privates“ RestService für „ADMIN“ s

aber entweder ich Zugriff auf alle habe - oder mit der aktuellen Konfiguration bekam ich Zugang zu NONE, denn ohne BasicAuth immer ich

bekam
  • vollständige Authentifizierung erforderlich, um diese Ressource zuzugreifen

und mit dem "uuu" + "ppp" Ich habe immer

  • Bad Credentials

Durch die Art, wie ich mit Federschuh 1.5.3

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.http.HttpMethod; 
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.builders.WebSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
import org.springframework.security.config.http.SessionCreationPolicy; 
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 

@Configuration 
@EnableWebSecurity 
public class RestSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception { 
     auth.inMemoryAuthentication().withUser("aaa").password("ppp").roles("USER", "ADMIN"); 
     auth.inMemoryAuthentication().withUser("uuu").password("ppp").roles("USER"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.csrf().disable() 
      .authorizeRequests() 
       .antMatchers("/public/**").permitAll() 
       .antMatchers("/protected/**").hasRole("USER") 
       .antMatchers("/private/**").hasRole("ADMIN") 
      .and().httpBasic() 
      .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); // We don't need sessions to be created. 
    } 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web 
      .ignoring() 
       .antMatchers(HttpMethod.OPTIONS, "/**"); /* To allow Pre-flight [OPTIONS] request from browser */ 
    } 
} 
+1

'auth.inMemoryAuthentication(). WithUser ("aaa"). Passwort (“ ppp "). roles (" USER "," ADMIN ") .und(). mitUser (" uuu "). password (" ppp "). roles (" USER ") – pvpkiran

Antwort

0

Der O P hat gefunden, dass ihre Lösung tatsächlich funktioniert. Es gab ein Problem mit der Komponente-Scan, weil meine Ruhe Projekt ein Kind meines DAO und Repository-Projekt ist ...

@SpringBootApplication 
@EnableAutoConfiguration 
@ComponentScan({"com.leycarno.base", "com.leycarno.rest"}) // now we're listen to all the nice components :-) 
@EnableJpaRepositories("com.leycarno.base.repositories") 
@EntityScan("com.leycarno.base.models") 
public class Application { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 
Verwandte Themen