6

この問題の解決方法をWebで検索しましたが、実際の解決策は見つかりませんでした。私は、基本的なSpringブートOAuth2認可プロバイダとクライアントをセットアップしようとしています。スプリングブートOAuth2 - トークンからユーザーの詳細を取得できませんでした

私は公式のSpringブートの手順に従い、FacebookとGithubでシングルサインオンを作成しました。その後、私はSecure Spring Boot Webアプリケーションを作成するための指示に従った。

独自のAuthorization Serverを作成して、のように@EnableAuthorizationServerアノテーションをSecure Web Applicationに追加しました。また、リンクに記載されているように、OAuth2クライアントの詳細を追加しました。私はさらに指示に従って、OAuth2クライアントを作成しました。

クライアントを開くには127.0.0.1:9999を、クライアントはlocalhost:8080/loginに私をリダイレクトし、ユーザーの詳細を入力し、認証プロバイダは127.0.0.1:9999/loginにリダイレクトされますエラーメッセージ:

認証が失敗しました:

INFO 2800 --- [NIO-9999:これは、ログに記録する内容であるトークン

からユーザーの詳細情報を取得できませんでした-exec-3] osbasorUserInfoTokenServices:ユーザー情報の取得:http:// localhost:8080/me

DEBUG 2800 --- [nio-9999-exec-3] ossoauth2.client.OAuth2RestTemplate:GET要求を作成しました。

DEBUG 2800 --- [nio-9999-exec-3] ossoauth2.client.OAuth2RestTemplate:[application/json、application/* + jsonにヘッダーを受け入れる]

DEBUG 2800 --- [NIO-9999-EXEC-3] ossoauth2.client.OAuth2RestTemplate:HTTPのリクエストGET:// localhostを:8080 /私は200の結果(OK)

INFO 28 00 - [nio-9999-exec-3] osbasorUserInfoTokenServices:ユーザーの詳細を取得できませんでした:クラスorg.springframework.web.client.RestClientException、レスポンスを抽出できませんでした:適切なHttpMessageConverterがレスポンスタイプに対して見つかりませんでした[interface java.util .MAP]およびコンテンツタイプ[text/htmlの;のcharset = UTF-8]]

これは私のクライアントアプリケーションである:これは、クライアントアプリケーションはYML

@EnableAutoConfiguration 
@Configuration 
@EnableOAuth2Sso 
@RestController 
public class ClientApplication { 

    @RequestMapping("/") 
    public String home(Principal user) { 
     return "Hello " + user.getName(); 
    } 

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

} 

です:

server: 
    port: 9999 
security: 
    oauth2: 
    client: 
     client-id: acme 
     client-secret: acmesecret 
     access-token-uri: http://localhost:8080/oauth/token 
     user-authorization-uri: http://localhost:8080/oauth/authorize 
    resource: 
     user-info-uri: http://localhost:8080/me 

これは私の 認可プロバイダアプリケーション:

@SpringBootApplication 
public class SecurityApp { 

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

} 

@Configuration 
@EnableWebSecurity 
@EnableAuthorizationServer 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/", "/home").permitAll() 
       .anyRequest().authenticated() 
       .and() 
      .formLogin() 
       .loginPage("/login") 
       .permitAll() 
       .and() 
      .logout() 
       .permitAll(); 
    } 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
      .inMemoryAuthentication() 
       .withUser("user").password("password").roles("USER"); 
    } 
} 

@Configuration 
public class MvcConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void addViewControllers(ViewControllerRegistry registry) { 
     registry.addViewController("/home").setViewName("home"); 
     registry.addViewController("/").setViewName("home"); 
     registry.addViewController("/hello").setViewName("hello"); 
     registry.addViewController("/login").setViewName("login"); 
    } 

} 

@RestController 
public class Controller { 

    @RequestMapping({ "/user", "/me" }) 
    public Map<String, String> user(Principal principal) { 
     Map<String, String> map = new LinkedHashMap<>(); 
     map.put("name", principal.getName()); 
     return map; 
    } 
} 

これは、アプリケーションプロバイダーYMLです:

security: 
    oauth2: 
    client: 
     client-id: acme 
     client-secret: acmesecret 
     scope: read,write 
     auto-approve-scopes: '.*' 

答えて

6

私はこの問題を解決!ユーザーエンドポイント(user-info-uri)の要求を処理するリソースサーバーが不足していました。認可プロバイダアプリケーションにこのクラスを追加しました:

@Configuration 
@EnableResourceServer 
public class ResourceServer 
     extends ResourceServerConfigurerAdapter { 
    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http 
      .antMatcher("/me") 
      .authorizeRequests().anyRequest().authenticated(); 
    } 
} 
+2

ここで、認証サーバーに/ me uriのマッピングを配置しましたか? –

+0

https://spring.io/guides/tutorials/spring-boot-oauth2/のこのガイドのように、この必要なステップ(次のステップ)と別のステップを完了する前に試してみるだけです。私も同じ問題がありました。 –

関連する問題