Я изучаю реализацию OAUTH2
Когда я попадаю на свою клиентскую часть, http: // локальный: 8082 / пользовательский интерфейс - конечная точка REST для пользовательского интерфейса, которая переведет меня на защищенный URI http: // локальный: 8082 / безопасный после входа на сервер аутентификации http: // localhost: 8081 / auth / логин. Но он терпит неудачу на http: // localhost: 8082 / ui / логин и дает мне ошибку как
org.springframework.security.oauth2.client.resource.UserRedirectRequiredException: для получения одобрения пользователей требуется перенаправление.
Конфигурация моего клиента
OauthConfig.java
@EnableOAuth2Sso
@Configuration
public class OauthConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// TODO Auto-generated method stub
http.antMatcher("/**").
authorizeRequests().antMatchers("/","/login**").permitAll().anyRequest().authenticated();
/*http
.authorizeRequests().anyRequest().authenticated();*/
}
}
и webconfig.java
@SuppressWarnings("deprecation")
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
// TODO Auto-generated method stub
configurer.enable();
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// TODO Auto-generated method stub
super.addViewControllers(registry);
registry.addViewController("/").setViewName("forward:/index");
registry.addViewController("/index");
registry.addViewController("/secure");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// TODO Auto-generated method stub
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer()
{
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public RequestContextListener contextlist()
{
return new RequestContextListener();
}
@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
и мой application.yml
server:
port: 8082
servlet:
context-path: /ui
session:
cookieName: UISESSION
spring:
thymeleaf:
cache: false
oauth2:
client:
client-id: ClientId
clientSecret: secret
accessTokenUri: http://localhost:8081/auth/oauth/token
userAuthorizationUri: http://localhost:8081/auth/oauth/authorize
clientAuthenticationScheme: form
resource:
userInfoUri: http://localhost:8081/auth/rest/hello/principal
preferTokenInfo: false
Нужно ли мне для этого писать собственный oauth2ClientContextFilter? Я уже добавил spring -security-oauth2 в pom.xml. Любая помощь могла бы быть полезна.






У меня был подобный опыт, и путем замены @EnableOAuth2Sso на @EnableOAuth2Client это было решено.
Похоже, что аннотация @EnableOAuth2Sso добавляет дополнительный фильтр цепочки безопасности OAuth2ClientAuthenticationProcessingFilter к springSecurityFilterChain, и это бросает UserRedirectRequiredException.
Обновить SecurityContextHolder
SecurityContextHolder.setStrategyName (SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);