![](/img/trans.png)
[英]Library for a flexible authentication in Spring MVC with Oauth2.0
[英]Authentication with Spring oAuth2.0
我想通過facebook / twitter帳戶對我的網站上的用戶進行身份驗證。 但是在春季示例中,春季要求我先登錄,然后再登錄,只有當用戶已經具有角色時,我才能連接到Facebook並獲取一些數據。 即使facebookData控制器上沒有權限( intercept-url pattern="/**" access="permitAll"
),spring intercept-url pattern="/**" access="permitAll"
將我重定向到登錄頁面。
我的春季安全:
<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>
和facebook控制器:
@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";
}
有一個很酷的庫可以做到這一點: https : //github.com/socialsignin/spring-social-security
我已經成功利用了它-用戶要做的就是連接到Facebook以進行身份驗證。 要在首次連接時創建新用戶,只需擴展SpringSocialSecurityConnectionSignUp
,以覆蓋execute
方法。 Github頁面很好地解釋了您需要做什么。 它還有一些非常有用的示例項目。
該項目在Maven中。 玩得開心!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.