Spring OAuth2 «Для доступа к этой ошибке ресурса требуется полная аутентификация» при попытке доступа к URL-адресу входа - PullRequest
0 голосов
/ 21 мая 2018

Я использую Spring Security OAuth2 (Spring Boot 2.0.2 + Spring Cloud Finchley) и пытаюсь инициировать скрытый вход в систему.Браузер перенаправляет меня на URL / login, но я получаю сообщение об ошибке «Требуется полная аутентификация для доступа к этому ресурсу».Как разрешить отображение страницы входа в систему, но при этом обеспечить защиту всех URL-адресов REST?

Моя конфигурация выглядит следующим образом:

App.java

@SpringBootApplication
@RestController
@EnableResourceServer
@EnableAuthorizationServer
public class App {

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

OAuth2Config.java

@Configuration
public class OAuth2Config extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;


    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("XXXXX")
                .secret("XXXXX")
                .authorizedGrantTypes("refresh_token", "password", "client_credentials")
                .scopes("webclient", "mobileclient");
    }

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

WebSecurityConfigurer.java

@Configuration
@Order(-20) // EDIT
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

   @Override
    @Bean
    public UserDetailsService userDetailsServiceBean() throws Exception {
        return super.userDetailsServiceBean();
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()  
                .withUser("XXXXX"). password("XXXXXX").roles("USER");
    }

    // EDIT

    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().permitAll()
        .and().httpBasic().and()
        .requestMatchers()
        //specify urls handled
        .antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
        .antMatchers("/fonts/**", "/js/**", "/css/**")
        .and()
        .authorizeRequests()
        .antMatchers("/fonts/**", "/js/**", "/css/**").permitAll()
        .anyRequest().authenticated();
    }
}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...