打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
cas集成oauth,用新浪微博账号登录示例

cas集成oauth,用新浪微博账号登录示例

分类: JAVA CAS 2820人阅读 评论(25) 收藏 举报

cas官方已经在开发cas与oauth集成的插件,使用的是scribe-up这个项目来获取授权后的用户基本信息。计划在cas3.5.0版本时推出,不过现在已经可以用了,下面将以新浪微博为例,说明如何用新浪微博的账号登录cas,到https://github.com/Jasig/cas/tree/master/cas-server-support-oauth下载源码,把cas-server-support-oauth的依赖添加到你的cas项目中。

先定义两个类,SinaWeiboApi20.java和SinaWeiboProvider.java,SinaWeiboApi20.java主要定义新浪微博的授权链接,SinaWeiboProvider.java主要是获取用户授权后的用户信息。

SinaWeiboApi20.java

  1. public class SinaWeiboApi20 extends DefaultApi20  
  2. {  
  3.   private static final String AUTHORIZE_URL = "https://api.weibo.com/oauth2/authorize?client_id=%s&redirect_uri=%s&response_type=code";  
  4.   private static final String SCOPED_AUTHORIZE_URL = AUTHORIZE_URL + "&scope=%s";  
  5.   
  6.   @Override  
  7.   public Verb getAccessTokenVerb()  
  8.   {  
  9.     return Verb.POST;  
  10.   }  
  11.   
  12.   @Override  
  13.   public AccessTokenExtractor getAccessTokenExtractor()  
  14.   {  
  15.     return new JsonTokenExtractor();  
  16.   }  
  17.   
  18.   @Override  
  19.   public String getAccessTokenEndpoint()  
  20.   {  
  21.     return "https://api.weibo.com/oauth2/access_token?grant_type=authorization_code";  
  22.   }  
  23.   
  24.   @Override  
  25.   public String getAuthorizationUrl(OAuthConfig config)  
  26.   {  
  27.     // Append scope if present  
  28.     if (config.hasScope())  
  29.     {  
  30.       return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()), OAuthEncoder.encode(config.getScope()));  
  31.     }  
  32.     else  
  33.     {  
  34.       return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));  
  35.     }  
  36.   }  
  37. }  

SinaWeiboProvider.java

  1. public class SinaWeiboProvider extends BaseOAuth20Provider {  
  2.     
  3.   @Override  
  4.   protected void internalInit() {  
  5.     if (scope != null) {  
  6.       service = new ServiceBuilder().provider(SinaWeiboApi20.class).apiKey(key)  
  7.           .apiSecret(secret).callback(callbackUrl).scope(scope).build();  
  8.     } else {  
  9.       service = new ServiceBuilder().provider(SinaWeiboApi20.class).apiKey(key)  
  10.           .apiSecret(secret).callback(callbackUrl).build();  
  11.     }  
  12.     String[] names = new String[] {"uid""username"};  
  13.     for (String name : names) {  
  14.       mainAttributes.put(name, null);  
  15.     }  
  16.       
  17.   }  
  18.     
  19.   @Override  
  20.   protected String getProfileUrl() {  
  21.     return "https://api.weibo.com/2/statuses/user_timeline.json";  
  22.   }  
  23.     
  24.   @Override  
  25.   protected UserProfile extractUserProfile(String body) {  
  26.     UserProfile userProfile = new UserProfile();  
  27.     JsonNode json = JsonHelper.getFirstNode(body);  
  28.     ArrayNode statuses = (ArrayNode) json.get("statuses");  
  29.     JsonNode userJson = statuses.get(0).get("user");  
  30.     if (json != null) {  
  31.       UserProfileHelper.addIdentifier(userProfile, userJson, "id");  
  32.       for (String attribute : mainAttributes.keySet()) {  
  33.         UserProfileHelper.addAttribute(userProfile, json, attribute,  
  34.             mainAttributes.get(attribute));  
  35.       }  
  36.     }  
  37.     JsonNode subJson = userJson.get("id");  
  38.     if (subJson != null) {  
  39.       UserProfileHelper  
  40.           .addAttribute(userProfile, "uid", subJson.getIntValue());  
  41.         
  42.     }  
  43.     subJson = userJson.get("domain");  
  44.     if (subJson != null) {  
  45.       UserProfileHelper.addAttribute(userProfile, "username",  
  46.           subJson.getTextValue());       
  47.     }  
  48.   
  49.     return userProfile;  
  50.   }  
  51.   
  52. }  

添加SinaWeiboProvider bean声明到applicationContext.xml

  1. <bean id="sinaWeibo" class="com.xxx.oauth.provider.SinaWeiboProvider">  
  2.         <property name="key" value="sinaweibo_key" />  
  3.         <property name="secret" value="sinaweibo_secret" />  
  4.         <property name="callbackUrl" value="https://sso.xxx.com:9443/login" />  
  5.     </bean>  

其中callbackUrl为你cas的登录地址。

cas-servlet.xml 中定义OAuthAction bean

  1. <bean id="oauthAction" class="org.jasig.cas.support.oauth.web.flow.OAuthAction"  
  2.         p:centralAuthenticationService-ref="centralAuthenticationService"  >  
  3.         <property name="providers">  
  4.             <list>  
  5.                 <ref bean="sinaWeibo" />                
  6.             </list>  
  7.         </property>  
  8.     </bean>  

添加oauthAction到cas的login-webflow.xml中,其主要功能是拦截oauth服务商返回的信息。

  1. <action-state id="oauthAction">   
  2.         <evaluate expression="oauthAction" />   
  3.         <transition on="success" to="sendTicketGrantingTicket" />   
  4.         <transition on="error" to="ticketGrantingTicketExistsCheck" />  
  5.     </action-state>  


添加OAuthAuthenticationHandler到deployerConfigContext.xml 中的authenticationHandlers处,使其支持oauth验证

  1. <property name="authenticationHandlers">  
  2.             <list>          
  3.                 <bean class="org.jasig.cas.support.oauth.authentication.handler.support.OAuthAuthenticationHandler">   
  4.                     <property name="providers">         
  5.                         <list>           
  6.                             <ref bean="sinaWeibo" />                          
  7.                         </list>       
  8.                     </property>     
  9.                 </bean>                 
  10.             </list>  
  11.         </property>  

添加OAuthCredentialsToPrincipalResolverdeployerConfigContext.xml中的credentialsToPrincipalResolvers处。

  1. <property name="credentialsToPrincipalResolvers">  
  2.             <list>          
  3.                 <bean class="org.jasig.cas.support.oauth.authentication.principal.OAuthCredentialsToPrincipalResolver" >    
  4.                 </bean>  
  5.             </list>  
  6.         </property>  

如果想获取从oauth返回的用户信息,就必须添加OAuthAuthenticationMetaDataPopulator到deployerConfigContext.xml中authenticationMetaDataPopulators处。

  1. <property name="authenticationMetaDataPopulators">   
  2.             <list>   
  3.                 <bean class="org.jasig.cas.support.oauth.authentication.OAuthAuthenticationMetaDataPopulator" />   
  4.             </list>   
  5.         </property>  

最后一步就添加用新浪微博账号登录的链接到登录页面

  1. <a href="${SinaWeiboProviderUrl}">用新浪微博登录</a>   

大功告成!

参考资料:https://wiki.jasig.org/display/CASUM/OAuth+client+support

本文地址:http://blog.csdn.net/laigood12345/article/details/7567247
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ASP.NET万能JSON解析器
SpringCloud SpringBoot OAuth2 Spring Security Redi...
[C#] 自己开发实现OAuth做webapi认证 | 教程大全
kafka connector 使用总结以及自定义connector开发
mybatis3 空字段null字段不返回
delphi 数据集与JSON对象互转
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服