2016-10-19 1 views
1

Ich habe wieder ein Problem mit Spring Oauth2. Ich weiß, dass dieses Thema nicht leicht ist, etw zu empfehlen oder die Codes zu überprüfen, weil wir zu viel Konfiguration haben. Mein Projekt hat 3 verschiedene Server, Authentifizierungsserver, Ressourcenserver und Front-End-Server. Ich möchte register.html in Front-End-Projekt registrieren (unter Angularjs-Dateien), aber wenn ich Anfrage an die zugehörige URL (http://localhost:7080/app/#register) seine Umleitung auf die Login-Seite (http://localhost:9080/auth-service/login) nur für eine Sekunde kann ich mein Register sehen .html Inhalt, aber danach geht es auf Login-Seite. Die Frage ist, wo sollte ich dieses Register.html setzen, sollte es unter Front-End-Projekt oder Authentifizierungsserver sein?Spring Oauth2 + Benutzerregistrierung

Mein Authentifizierungsserver und Front-End-Servercodes sind;

@Configuration 
    public class AuthServerSecurityConfiguration extends WebSecurityConfigurerAdapter { 

@Autowired 
private AuthenticationManager authenticationManager; 


@Override 
protected void configure(AuthenticationManagerBuilder auth) 
     throws Exception { 
    auth.parentAuthenticationManager(authenticationManager); 
    auth.authenticationProvider(userAuthProviderService()); 
} 
private CsrfMatcher csrfRequestMatcher = new CsrfMatcher(); 
@Override 
protected void configure(final HttpSecurity http) throws Exception { 
    /*http.csrf().disable();*/ 
    http.csrf().requireCsrfProtectionMatcher(csrfRequestMatcher); 
    http 
      .formLogin().loginPage("/login").defaultSuccessUrl("/") 
      /*.failureUrl("")*/.successHandler(new AuthSuccessHandler()).permitAll() 
      .and() 
      .requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access","/register") 
      .and() 
      .authorizeRequests().anyRequest().authenticated(); 

} 

@Bean 
public UserAuthProviderService userAuthProviderService(){ 
    return new UserAuthProviderService(); 
} 

private class CsrfMatcher implements RequestMatcher { 
    @Override 
    public boolean matches(HttpServletRequest request) { 
     return false; 
    } 
} 

}

@Configuration 
@EnableAutoConfiguration 
@RestController 
@EnableZuulProxy 
@EnableOAuth2Sso 
@EnableOAuth2Client 
public class UIServiceMain { 

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

@Configuration 
protected static class SecurityConfiguration extends OAuth2SsoConfigurerAdapter { 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http.logout().and() 
       .antMatcher("/**").authorizeRequests() 
       .antMatchers("/index.html", "/home.html", "/", "/login","/register.html").permitAll().anyRequest() 
       .authenticated().and().csrf().disable(); 
     http.headers().frameOptions().disable(); //FOR EMBED MAP 
    } 

    //unused 
    private Filter csrfHeaderFilter() { 
     return new OncePerRequestFilter() { 
      @Override 
      protected void doFilterInternal(HttpServletRequest request, 
        HttpServletResponse response, FilterChain filterChain) 
        throws ServletException, IOException { 
       CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class 
         .getName()); 
       if (csrf != null) { 
        Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN"); 
        String token = csrf.getToken(); 
        if (cookie == null || token != null 
          && !token.equals(cookie.getValue())) { 
         cookie = new Cookie("XSRF-TOKEN", token); 
         cookie.setPath("/"); 
         response.addCookie(cookie); 
        } 
       } 
       filterChain.doFilter(request, response); 
      } 
     }; 
    } 

    //unused 
    private CsrfTokenRepository csrfTokenRepository() { 
     HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); 
     repository.setHeaderName("X-XSRF-TOKEN"); 
     return repository; 
    } 
} 

}

Antwort

0

in Ihrem UI-Server versuchen WebSecurity erstellen mit /register.hml aktiviert ist, so etwas wie dieses

@Configuration 
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter{ 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
        .requestMatchers() 
        .antMatchers("/register.html") 
        .and() 
        .authorizeRequests() 
        .anyRequest().authenticated(); 
     } 
} 

edit: oder vielleicht In Ihrer aktuellen Konfiguration entfernen Sie .antMatcher("/**").authorizeRequests() und fügen Siehinzu

so schließlich könnte es so etwas wie dieses:

@Override 
public void configure(HttpSecurity http) throws Exception { 
     http.logout().and() 
       .antMatchers("/index.html", "/home.html", "/", "/login","/register.html").permitAll().anyRequest() 
       .authenticated() 
       .and().csrf().disable(); 
     http.headers().frameOptions().disable() //FOR EMBED MAP 
       .and() 
       .authorizeRequests() 
       .anyRequest().authenticated(); 

    } 
0

Paar Dinge:

  • ich nicht aus einem guten Grund denken kann, nicht Ihre * .html zu setzen irgendwo anders als Front-End-Server.

  • Auch in der Regel sollten Sie den Zugriff auf Ihre statischen UI-Komponenten erlauben wie @bilak öffentlich erwähnt:

.antMatchers("/index.html", "/home.html", "/", "/login","/register.html").permitAll()

  • Wenn Sie in der Lage sind register.html Seite zu sehen, bei alle (vorausgesetzt, nicht authentifizierter Benutzer), dann ist es bereits öffentlich

  • Vielleicht gibt es einen Webservice-Aufruf auf register.html Das Ladeereignis, das hinter der Spring - Sicherheit steht, die den Authentifizierungsablauf auslöst.

Verwandte Themen