2017-08-10 3 views
1

Ich möchte diese URL "/ realtime/updates" einschränken, aber wenn ich "/ anyurl" anfordere, leitet es mich zur Anmeldeseite um. Ich möchte zur Anmeldeseite nur für "/ realtime/updates" umgeleitet werden.Spring boot Security

@Configuration 
@EnableWebSecurity 
    @EnableGlobalMethodSecurity(prePostEnabled = true) 
    public class WebSecConfig1 extends WebSecurityConfigurerAdapter { 
    @Override 
    public void configure(WebSecurity web) throws Exception { 
    } 
    @Bean 
    @Override 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
     return super.authenticationManagerBean(); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     super.configure(http); 

     http.authorizeRequests().antMatchers("/realtime/updates").hasRole("USER").anyRequest().authenticated().and().formLogin(); 



    } 
    @Autowired 
    public void ConfigGlobal(AuthenticationManagerBuilder auth) throws Exception{ 
     auth.inMemoryAuthentication().withUser("abc").password("123").roles("USER"); 
    } 
} 

Antwort

1

Sie müssen ein Schnipsel in der protected void configure Methode ändern. Versuchen Sie folgendes:

@Override 
protected void configure(HttpSecurity httpSecurity) throws Exception { 
    httpSecurity 
     .authorizeRequests() 
      .antMatchers("/**").permitAll() 
      .antMatchers("/realtime/updates").hasRole("USER") 
     .and().formLogin().permitAll(); 
} 
+0

Bitte geben Sie genau das, was die Änderung ist, da dies unklar ist, was der Code Teil – dhdavvie

+0

@Schipunov geändert werden muss in jeden url Ich möchte beschränken tut zu restricte "/ realtime/updates", aber es ist nicht einschränkend –

+0

@Shchipunov immer noch dies keine URL einschließlich "realtime/updates" –

0

Sie benötigen Einschränkungen in configure method.Your Methode hinzufügen wie folgt:

@Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .csrf().disable() 
       .authorizeRequests() 
       .antMatchers("/").permitAll() 
       .antMatchers("/realtime/updates/**").authenticated() 
       .antMatchers("/otherURL/**").hasRole("ADMIN") 
       .and() 
       .exceptionHandling().accessDeniedPage("/login") 
       .and() 
       .formLogin().loginPage("/index") 
       .usernameParameter("username") 
       .passwordParameter("password") 
       .loginProcessingUrl("/j_spring_security_check") 
       .failureUrl("/loginControl?error=true") 
       .defaultSuccessUrl("/loginControl?error=false") 
       .and() 
       .logout().logoutUrl("/logout").logoutSuccessUrl("/index") 
       ; 

    } 
0

Ich rief super.configure (http); Innerhalb der Konfigurationsmethode habe ich entfernt, dass es funktioniert hat.

This is was previous code: 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
     super.configure(http); 


     http.authorizeRequests().antMatchers("/realtime/updates") 
    .hasRole("USER") 
    .anyRequest() 
    .authenticated() 
    .and() 
    .formLogin(); 
} 

Dies ist aktuellen Code, gearbeitet:

@Override 
protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests().antMatchers("/realtime/updates") 
    .hasRole("USER") 
    .anyRequest() 
    .authenticated() 
    .and() 
    .formLogin(); 
} 
Verwandte Themen