Spring Boot UserRedirectRequiredException - 需要重定向才能获得用户批准 [英] Spring Boot UserRedirectRequiredException- A redirect is required to get the users approval

查看:44
本文介绍了Spring Boot UserRedirectRequiredException - 需要重定向才能获得用户批准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 OAUTH2 实现

当我访问我的客户端时 http://localhost:8082/ui - UI 的 REST 端点登录到身份验证服务器后,这将带我到安全 URI http://localhost:8082/secure http://localhost:8081/auth/login.但它在 http://localhost:8082/ui/login 失败并给我错误>

org.springframework.security.oauth2.client.resource.UserRedirectRequiredException:需要重定向才能获得用户批准

我的客户端配置是

OauthConfig.java

@EnableOAuth2Sso@配置公共类 OauthConfig 扩展了 WebSecurityConfigurerAdapter {@覆盖protected void configure(HttpSecurity http) 抛出异常 {//TODO 自动生成的方法存根http.antMatcher("/**").authorizeRequests().antMatchers("/","/login**").permitAll().anyRequest().authenticated();/*http.authorizeRequests().anyRequest().authenticated();*/}}

和 webconfig.java

@SuppressWarnings("deprecation")@EnableWebMvc@配置公共类 WebConfig 扩展了 WebMvcConfigurerAdapter {@覆盖public void configureDefaultServletHandling(DefaultServletHandlerConfigurer 配置器) {//TODO 自动生成的方法存根配置器.启用();}@覆盖public void addViewControllers(ViewControllerRegistry 注册表) {//TODO 自动生成的方法存根super.addViewControllers(注册表);registry.addViewController("/").setViewName("forward:/index");registry.addViewController("/index");registry.addViewController("/secure");}@覆盖public void addResourceHandlers(ResourceHandlerRegistry registry) {//TODO 自动生成的方法存根registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");}@豆角,扁豆公共静态 PropertySourcesPlaceholderConfigurer placeHolderConfigurer(){返回新的 PropertySourcesPlaceholderConfigurer();}@豆角,扁豆公共 RequestContextListener 上下文列表(){返回新的 RequestContextListener();}@豆角,扁豆公共静态 PropertySourcesPlaceholderConfigurer placeholderConfigurer() {返回新的 PropertySourcesPlaceholderConfigurer();}}

我的 application.yml 是

 服务器:端口:8082小服务程序:上下文路径:/ui会议:cookie 名称:UISESSION春天:百里香:缓存:假oauth2:客户:客户端 ID:客户端 IDclientSecret:秘密accessTokenUri: http://localhost:8081/auth/oauth/tokenuserAuthorizationUri:http://localhost:8081/auth/oauth/authorizeclientAuthenticationScheme:表单资源:userInfoUri:http://localhost:8081/auth/rest/hello/principal首选令牌信息:假

我需要为此编写自定义 oauth2ClientContextFilter 吗?我已经在 pom.xml 中添加了 spring-security-oauth2.任何帮助将不胜感激.

解决方案

更新 SecurityContextHolder

SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);

I am learning OAUTH2 implementation

When i hit my client end http://localhost:8082/ui - REST end point for UI which will take me to the secure URI http://localhost:8082/secure after logging into the auth server http://localhost:8081/auth/login. But it fails at http://localhost:8082/ui/login and give me error as

org.springframework.security.oauth2.client.resource.UserRedirectRequiredException: A redirect is required to get the users approval

My client configuration is

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();*/
    }
}

and 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();
    }

}

and my application.yml is

 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

Do i need to write custom oauth2ClientContextFilter for this? i already have added spring-security-oauth2 to the pom.xml. Any help would be really appreciated.

解决方案

Update SecurityContextHolder

SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);

这篇关于Spring Boot UserRedirectRequiredException - 需要重定向才能获得用户批准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆