2015-07-24 3 views
3

Как настроить пользовательскую форму входа для защиты конечной точки my/oauth/authorize в приложении Spring Boot, которое является одновременно сервером авторизации и ресурсов? Я хочу достичь того, что пользователь должен войти в систему, чтобы делать запросы/oauth/authorize. Все ресурсы, которые не являются/login и/oauth/**, должны обрабатываться как защищенные ресурсы, защищенные OAuth (требуется действительный токен доступа)Spring OAuth: Пользовательская форма для авторизации конечной точки авторизации

Так что, когда пользователь вызывает localhost: 1234/oauth/authorize? Client_id = client & response_type = token & redirect_uri = http://localhost:5678 он сначала перенаправляется на форму входа в систему и после успешного входа в очередь перенаправляется на конечную точку/oauth/authorize, где продолжается неявный поток OAuth.

Когда я пытаюсь следующий он работает, используя стандартную базовую аутентификацию всплывающее окно

@Configuration 
@EnableAuthorizationServer 
public class OAuthConfig extends AuthorizationServerConfigurerAdapter { 

    @Autowired 
    private AuthenticationManager authenticationManager; 

    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
     endpoints.authenticationManager(authenticationManager); 
    } 

    @Override 
    public void configure(final ClientDetailsServiceConfigurer clients) 
      throws Exception { 
     clients.inMemory() 
       .withClient("client") 
       .authorizedGrantTypes("implicit", "refresh_token") 
       .scopes("read"); 
    } 

} 

Ресурс конфигурации

@Configuration 
@EnableResourceServer 
public class ResourceConfiguration 
     extends ResourceServerConfigurerAdapter 
{ 

    @Override 
    public void configure(final HttpSecurity http) throws Exception { 
     // @formatter:off 
     http.authorizeRequests().antMatchers("/login").permitAll().and() 
       .authorizeRequests().anyRequest().authenticated(); 
     // @formatter:on 
    } 
} 

Конфигурационный Web Security

@Configuration 
public class WebSecurityConfiguration 
     extends WebSecurityConfigurerAdapter 
{ 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests().antMatchers("/oauth/authorize").authenticated().and() 
       .authorizeRequests().anyRequest().permitAll().and().httpBasic(); 
    } 

} 

, но как только поскольку я заменяю httpBasic() следующим образом:

@Configuration 
public class WebSecurityConfiguration 
     extends WebSecurityConfigurerAdapter 
{ 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests().antMatchers("/oauth/authorize").authenticated().and() 
     .authorizeRequests().anyRequest().permitAll().and().formLogin().loginPage("/login").and().csrf() 
     .disable(); 
    } 

} 

и мой пост от входа страницы не перенаправляется, он всегда просто возвращается к/Войти

Выход из консоли выглядит следующим образом

o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/login'; against '/css/**' 
o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/login'; against '/js/**' 
o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/login'; against '/images/**' 
o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/login'; against '/**/favicon.ico' 
o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/login'; against '/error' 
o.s.s.web.util.matcher.OrRequestMatcher : Trying to match using Ant [pattern='/oauth/token'] 
o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/login'; against '/oauth/token' 
o.s.s.web.util.matcher.OrRequestMatcher : Trying to match using Ant [pattern='/oauth/token_key'] 
o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/login'; against '/oauth/token_key' 
o.s.s.web.util.matcher.OrRequestMatcher : Trying to match using Ant [pattern='/oauth/check_token'] 
o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/login'; against '/oauth/check_token' 
o.s.s.web.util.matcher.OrRequestMatcher : No matches found 
o.s.s.web.util.matcher.OrRequestMatcher : Trying to match using org.springframework.security.oauth2.config.annotation.web.c[email protected]5fe3eb5f 
o.s.s.web.util.matcher.OrRequestMatcher : matched 
o.s.security.web.FilterChainProxy  : /login at position 1 of 11 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter' 
o.s.security.web.FilterChainProxy  : /login at position 2 of 11 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter' 
o.s.security.web.FilterChainProxy  : /login at position 3 of 11 in additional filter chain; firing Filter: 'HeaderWriterFilter' 
o.s.s.w.header.writers.HstsHeaderWriter : Not injecting HSTS header since it did not match the requestMatcher org.springframework.se[email protected]51c78264 
o.s.security.web.FilterChainProxy  : /login at position 4 of 11 in additional filter chain; firing Filter: 'LogoutFilter' 
o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/login'; against '/logout' 
o.s.security.web.FilterChainProxy  : /login at position 5 of 11 in additional filter chain; firing Filter: 'OAuth2AuthenticationProcessingFilter' 
o.s.s.o.p.a.BearerTokenExtractor   : Token not found in headers. Trying request parameters. 
o.s.s.o.p.a.BearerTokenExtractor   : Token not found in request parameters. Not an OAuth2 request. 
p.a.OAuth2AuthenticationProcessingFilter : No token in request, will continue chain. 
o.s.security.web.FilterChainProxy  : /login at position 6 of 11 in additional filter chain; firing Filter: 'RequestCacheAwareFilter' 
o.s.security.web.FilterChainProxy  : /login at position 7 of 11 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter' 
o.s.security.web.FilterChainProxy  : /login at position 8 of 11 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter' 
o.s.s.w.a.AnonymousAuthenticationFilter : Populated SecurityContextHolder with anonymous token: 'org.sprin[email protected]90541710: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.sprin[email protected]166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: BDCE2D7EA7252AEA2506633726B8BA19; Granted Authorities: ROLE_ANONYMOUS' 
o.s.security.web.FilterChainProxy  : /login at position 9 of 11 in additional filter chain; firing Filter: 'SessionManagementFilter' 
o.s.security.web.FilterChainProxy  : /login at position 10 of 11 in additional filter chain; firing Filter: 'ExceptionTranslationFilter' 
o.s.security.web.FilterChainProxy  : /login at position 11 of 11 in additional filter chain; firing Filter: 'FilterSecurityInterceptor' 
o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/login'; against '/login' 
o.s.s.w.a.i.FilterSecurityInterceptor : Secure object: FilterInvocation: URL: /login; Attributes: [#oauth2.throwOnError(permitAll)] 
o.s.s.w.a.i.FilterSecurityInterceptor : Previously Authenticated: org.sprin[email protected]90541710: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.sprin[email protected]166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: BDCE2D7EA7252AEA2506633726B8BA19; Granted Authorities: ROLE_ANONYMOUS 
o.s.s.access.vote.AffirmativeBased  : Voter: org.sp[email protected]bba9bfc, returned: 1 
o.s.s.w.a.i.FilterSecurityInterceptor : Authorization successful 
o.s.s.w.a.i.FilterSecurityInterceptor : RunAsManager did not change Authentication object 
o.s.security.web.FilterChainProxy  : /login reached end of additional filter chain; proceeding with original chain 
o.s.s.w.a.ExceptionTranslationFilter  : Chain processed normally 
s.s.w.c.SecurityContextPersistenceFilter : SecurityContextHolder now cleared, as request processing completed 

ответ

4

Добавление @Order(-10) к WebSecurityConfig решает проблема. Я думаю, что это аннотирование гарантирует, что WebSecurityConfig используется до ResourceConfig. Моя окончательная конфигурация выглядит так:

@Configuration 
@Order(-10) 
public class WebSecurityConfig 
     extends WebSecurityConfigurerAdapter 
{ 

    @Autowired 
    private AuthenticationManager authenticationManager; 

    @Override 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.parentAuthenticationManager(authenticationManager); 

    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     // @formatter:off 
     http.authorizeRequests().antMatchers("/oauth/authorize").authenticated() 
       .and() 
       .authorizeRequests().anyRequest().permitAll() 
       .and() 
       .formLogin().loginPage("/login").permitAll() 
       .and() 
       .csrf().disable(); 
     // @formatter:on 
    } 

} 
Смежные вопросы