2013-11-23 3 views
2

내 웹 사이트의 사용자를 facebook/twitter 계정으로 인증하고 싶습니다. 그러나 봄의 예에서는 spring이 먼저 로그인하도록 요청하고, 그 후에 만 ​​사용자가 이미 역할을 맡으면 페이스 북에 연결하여 데이터를 얻을 수 있습니다. facebookData 컨트롤러 (intercept-url pattern="/**" access="permitAll")에서 권한이 없어도 spring은 나를 로그인 페이지로 리디렉션합니다.Spring oAuth2.0 인증

  • OpenAuth는 인증에 사용할 수 없습니다. anonymouse user?

내 봄 보안 :

<http use-expressions="true"> 
     <form-login login-page="/login" /> 
     <intercept-url pattern="/**" access="permitAll" /> 
     <logout /> 
     <custom-filter ref="oauth2ClientFilter" after="EXCEPTION_TRANSLATION_FILTER" /> 
    </http> 
    <oauth:client id="oauth2ClientFilter" /> 

<oauth:resource id="facebook" type="authorization_code" client-id="..." client-secret="..." authentication-scheme="query" 
    access-token-uri="https://graph.facebook.com/oauth/access_token" user-authorization-uri="https://www.facebook.com/dialog/oauth" token-name="oauth_token" client-authentication-scheme="form" scope="email"/> 

<bean id="facebookController" class="org.springframework.security.oauth.examples.tonr.mvc.FacebookController" /> 

<oauth:rest-template resource="facebook" id="fasebookRestTemplate"> 
    <property name="messageConverters"> 
     <list> 
      <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
       <property name="supportedMediaTypes"> 
        <list> 
         <bean class="org.springframework.http.MediaType"> 
          <!--facebook sends its json as text/javascript for some reason --> 
          <constructor-arg value="text" /> 
          <constructor-arg value="javascript" /> 
         </bean> 
         <bean class="org.springframework.http.MediaType"> 
          <constructor-arg value="application" /> 
          <constructor-arg value="json" /> 
         </bean> 
        </list> 
       </property> 
      </bean> 
     </list> 
    </property> 
</oauth:rest-template> 

와 페이스 북 컨트롤러 :의 https://github.com/socialsignin/spring-social-security

내가 성공적으로 만들었 사용 :

@Autowired 
private RestOperations facebookRestTemplate; 

@RequestMapping("/facebook/info") 
public String photos(Model model) throws Exception { 

    ObjectNode resultNode = facebookRestTemplate.getForObject("https://graph.facebook.com/me/friends", ObjectNode.class); 
    ArrayNode data = (ArrayNode) resultNode.get("data"); 
    ArrayList<String> friends = new ArrayList<String>(); 
    for (JsonNode dataNode : data) { 
     friends.add(dataNode.get("name").getTextValue()); 
    } 
    model.addAttribute("friends", friends); 
    return "facebook"; 
} 

답변

1

정확히이하는 멋진 도서관이 있습니다 그것은 - 모든 사용자가 할 일은 인증을 위해 Facebook에 연결하는 것입니다. 첫 번째 연결시 새 사용자를 만들려면 SpringSocialSecurityConnectionSignUp을 확장하여 execute 메서드를 재정의하면됩니다. Github 페이지는 당신이해야 할 일을 멋지게 설명합니다. 또한 실제로 유용하게 사용할 수있는 샘플 프로젝트도 있습니다.

프로젝트가 Maven에 있습니다. 재미있어!

+0

고마워요.하지만 페이스 북과 트위터뿐만 아니라 여기에서 답을 찾았을뿐 아니라 http://forum.spring.io/forum/spring-projects/security/oauth/121682-authentication-is-required도 연결하려고합니다. 획득 토큰 - 익명 - 허용되지 않음 – user1406196