【问题标题】:spring oauth2, unable to retrieve user infospring oauth2,无法检索用户信息
【发布时间】:2018-08-03 01:43:57
【问题描述】:

找不到说明如何使用 access_token 获取用户信息的文档。

如何制作这样的端点?

当我访问 /user 端点时,我看到 OAuth2AuthenticationProcessingFilter 不包括在内。 我想我把下面的东西搞砸了

@Configuration
@EnableWebSecurity
@Order(2)
// after ResourceServerConfigurerAdapter
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      // https://stackoverflow.com/questions/26387003/how-to-make-spring-boot-never-issue-session-cookie
      .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
      .authorizeRequests()
      .antMatchers("/", "index", "/login**", "/webjars/**")
      .permitAll()
      .and().exceptionHandling()
      .authenticationEntryPoint(authenticationEntryPoint)
      .accessDeniedHandler(oAuth2FailedHandler)
      .and().csrf().disable().authorizeRequests()                                                                                                                                       
      .and().addFilterBefore(new CORSFilter(), BasicAuthenticationFilter.class);                                                                                                    }
}
I also have..

@Order(3)
@Configuration
@EnableResourceServer
@Slf4j
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(final ResourceServerSecurityConfigurer config) {
        config.tokenServices(tokenServices());
    }

   @Override
    public void configure(HttpSecurity http) throws Exception {
     // super.configure(http);

     log.info("hello OAuth2ResourceServerConfig:configure");
     http
        .authorizeRequests().anyRequest().authenticated();

    }

     @Bean
     @Primary
     public DefaultTokenServices tokenServices() {
         final DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
         defaultTokenServices.setTokenStore(tokenStore());
         return defaultTokenServices;
     }

     // JDBC token store configuration

     @Bean
     public DataSource dataSource() {
         final DriverManagerDataSource dataSource = new DriverManagerDataSource();
         dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
         dataSource.setUrl(env.getProperty("spring.datasource.url"));
         dataSource.setUsername(env.getProperty("spring.datasource.username"));
         dataSource.setPassword(env.getProperty("spring.datasource.password"));

         return dataSource;
     }

     @Bean
     public TokenStore tokenStore() {
         return new JdbcTokenStore(dataSource());
     }
}


@Configuration
 //@PropertySource({ "classpath:persistence.properties" })                                                                                                                         @EnableAuthorizationServer
 @Slf4j
 public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    // ....
 }

【问题讨论】:

  • 我也有AuthorizationServerConfigurerAdapter .. 我不知道其中任何一个隐藏了另一个。我有SecurityConfig 设置SessionCreationPolicy.STATELESS 并添加CORSFilter。如果这是您的问题,资源服务器和身份验证服务器是同一台服务器。

标签: spring oauth-2.0 spring-oauth2


【解决方案1】:

添加一个类似这样的 REST 控制器:

@RequestMapping("/user")
public @ResponseBody Principal  user(Principal user) {
    return user;
}

然后你可以调用它,在 Authorization 标头中使用你的访问令牌。

【讨论】:

    猜你喜欢
    • 2017-10-11
    • 2013-09-16
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 2021-03-06
    • 2016-12-31
    • 2011-04-22
    相关资源
    最近更新 更多