2016-09-19 4 views
0

Ich verwende Spring Security, um Benutzer basierend auf der Rolle zu authentifizieren. Authentifizieren für /** ist zu geben:Ich kann Authentifizierung in Spring Security vornehmen?

Page load failed with error: too many HTTP redirects

Fehler und Login-Seite wird nicht angezeigt.

protected void configure(HttpSecurity http) throws Exception { 
      http.authorizeRequests() 
      .antMatchers("/login*").authenticated() 
      .antMatchers("/**").authenticated() 
      .and() 
      .formLogin().loginPage("/login").failureUrl("/login?error").defaultSuccessUrl("/welcome") 
      .usernameParameter("username").passwordParameter("password") 
      .and() 
      .logout().logoutSuccessUrl("/login?logout").logoutUrl("/login?logout") 
      .and() 
      .exceptionHandling().accessDeniedPage("/accessDenied") 
      .and() 
      .csrf(); 
     } 

Aber wenn ich so tun:

protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests() 
     .antMatchers("/login").authenticated() 
     .antMatchers("/").authenticated() 
     .and() 
     .formLogin().loginPage("/login").failureUrl("/login?error").defaultSuccessUrl("/welcome") 
     .usernameParameter("username").passwordParameter("password") 
     .and() 
     .logout().logoutSuccessUrl("/login?logout").logoutUrl("/login?logout") 
     .and() 
     .exceptionHandling().accessDeniedPage("/accessDenied") 
     .and() 
     .csrf(); 
    } 

Was ist falsch in diesem Code für /** URL zu authentifizieren?

Antwort

1

Ihre Login-Seite ist für nicht authentifizierte Benutzer nicht zugänglich:

.antMatchers("/login*").authenticated() 

so Spring Security zu Ihrem Login-Seite umleitet, die auf Ihre loging Seite umleitet, ...

Sie haben nicht authentifizierten Benutzern zu ermöglichen, um Ihre Login-Seite zu erhalten, sehen Spring Security Reference:

While the automatically generated log in page is convenient to get up and running quickly, most applications will want to provide their own log in page. To do so we can update our configuration as seen below:

protected void configure(HttpSecurity http) throws Exception { 
    http 
     .authorizeRequests() 
      .anyRequest().authenticated() 
      .and() 
     .formLogin() 
      .loginPage("/login") 1 
      .permitAll();  2 
} 

1 The updated configuration specifies the location of the log in page.

2 We must grant all users (i.e. unauthenticated users) access to our log in page. The formLogin().permitAll() method allows granting access to all users for all URLs associated with form based log in.

Wenn Sie die Platzhalter (*) Alle Seiten entfernen ar Für nicht authentifizierte Benutzer zugänglich, außer login und /.

Verwandte Themen