博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring-mvc 与 openid4java
阅读量:7060 次
发布时间:2019-06-28

本文共 6779 字,大约阅读时间需要 22 分钟。

以GoogleOpenID 为例,试验了OAuth单点登录的用法:

1 
2
org.openid4java
3
openid4java
4
0.9.8
5

 

1 import java.util.List;  2   3 import javax.servlet.http.HttpServletRequest;  4 import javax.servlet.http.HttpServletResponse;  5   6 import org.openid4java.OpenIDException;  7 import org.openid4java.consumer.ConsumerManager;  8 import org.openid4java.consumer.VerificationResult;  9 import org.openid4java.discovery.DiscoveryInformation; 10 import org.openid4java.discovery.Identifier; 11 import org.openid4java.message.AuthRequest; 12 import org.openid4java.message.AuthSuccess; 13 import org.openid4java.message.ParameterList; 14 import org.openid4java.message.ax.AxMessage; 15 import org.openid4java.message.ax.FetchRequest; 16 import org.openid4java.message.ax.FetchResponse; 17 import org.slf4j.Logger; 18 import org.slf4j.LoggerFactory; 19 import org.springframework.stereotype.Controller; 20 import org.springframework.web.bind.annotation.RequestMapping; 21 import org.springframework.web.util.UriComponentsBuilder; 22  23 import com.google.common.base.Throwables; 24  25 @Controller 26 @RequestMapping("/openid") 27 @SuppressWarnings("rawtypes") 28 public class SecurityOpenIDController { 29  30     public static final String GOOGLE_ENDPOINT = "https://www.google.com/accounts/o8/id"; 31     private static final Logger LOGGER = LoggerFactory.getLogger(SecurityOpenIDController.class); 32  33     public final ConsumerManager manager = new ConsumerManager(); 34  35     @RequestMapping("/login") 36     public void login( 37             UriComponentsBuilder builder, 38             HttpServletRequest request, 39             HttpServletResponse response 40         ) throws Exception 41     { 42         // configure the return_to URL where your application will receive 43         // the authentication responses from the OpenID provider 44         String returnUrl = builder.path("/openid/return").build().toUriString(); 45          46         // --- Forward proxy setup (only if needed) --- 47         // ProxyProperties proxyProps = new ProxyProperties(); 48         // proxyProps.setProxyName("proxy.example.com"); 49         // proxyProps.setProxyPort(8080); 50         // HttpClientFactory.setProxyProperties(proxyProps); 51  52         // perform discovery on the user-supplied identifier 53         List discoveries = manager.discover(GOOGLE_ENDPOINT); 54  55         // attempt to associate with the OpenID provider 56         // and retrieve one service endpoint for authentication 57         DiscoveryInformation discovered = manager.associate(discoveries); 58          59         // store the discovery information in the user's session 60         request.getSession().setAttribute("openid-disc", discovered); 61  62         // obtain a AuthRequest message to be sent to the OpenID provider 63         AuthRequest authReq = manager.authenticate(discovered, returnUrl); 64  65         // attribute Exchange 66         FetchRequest fetch = FetchRequest.createFetchRequest(); 67         fetch.addAttribute("email", "http://axschema.org/contact/email", true); 68         fetch.addAttribute("firstName", "http://axschema.org/namePerson/first", true); 69         fetch.addAttribute("lastName", "http://axschema.org/namePerson/last", true); 70          71         // attach the extension to the authentication request 72         authReq.addExtension(fetch); 73  74         if (!discovered.isVersion2()) { 75             // Option 1: GET HTTP-redirect to the OpenID Provider endpoint 76             // The only method supported in OpenID 1.x 77             // redirect-URL usually limited ~2048 bytes 78             response.sendRedirect(authReq.getDestinationUrl(true)); 79         } else { 80             // Option 2: HTML FORM Redirection (Allows payloads >2048 bytes) 81             response.sendRedirect(authReq.getDestinationUrl(true)); 82         } 83     } 84  85     @RequestMapping("/return") 86     public void verifyResponse(HttpServletRequest request) { 87         String email = null; 88         String lastName = null; 89         String firstName = null; 90          91         try { 92             // extract the parameters from the authentication response 93             // (which comes in as a HTTP request from the OpenID provider) 94             ParameterList response = new ParameterList(request.getParameterMap()); 95  96             // retrieve the previously stored discovery information 97             DiscoveryInformation discovered = (DiscoveryInformation) request.getSession().getAttribute("openid-disc"); 98  99             // extract the receiving URL from the HTTP request100             StringBuffer receivingURL = request.getRequestURL();101             String queryString = request.getQueryString();102             if (queryString != null && queryString.length() > 0) {103                 receivingURL.append("?").append(request.getQueryString());104             }105 106             // verify the response; ConsumerManager needs to be the same107             // (static) instance used to place the authentication request108             VerificationResult verification = manager.verify(receivingURL.toString(), response, discovered);109 110             // examine the verification result and extract the verified111             // identifier112             Identifier verified = verification.getVerifiedId();113             if (verified != null) {114                 AuthSuccess authSuccess = (AuthSuccess) verification.getAuthResponse();115 116                 if (authSuccess.hasExtension(AxMessage.OPENID_NS_AX)) {117                     FetchResponse fetchResp = (FetchResponse) authSuccess.getExtension(AxMessage.OPENID_NS_AX);118 119                     List emails = fetchResp.getAttributeValues("email");120                     email = (String) emails.get(0);121                     122                     List lastNames = fetchResp.getAttributeValues("lastName");123                     lastName = (String) lastNames.get(0);124                     125                     List firstNames = fetchResp.getAttributeValues("firstName");126                     firstName = (String) firstNames.get(0);127                     128                     LOGGER.debug("email: {}", email);129                     LOGGER.debug("lastName: {}", lastName);130                     LOGGER.debug("firstName: {}", firstName);131                 }132                 // success133                 134                 // 在这里与安全框架集成 apache-shiro/spring-security135                 // 这里要根据相关的信息自己定义Principal136             }137         } catch (OpenIDException e) {138             LOGGER.error(e.getMessage(), e);139             Throwables.propagate(e);140         }141     }142 }

 

转载地址:http://upyll.baihongyu.com/

你可能感兴趣的文章
ps与top命令简单介绍
查看>>
js12---闭包,原型,继承
查看>>
JavaScript返回上一页代码区别
查看>>
EntityFramework 如何进行异步化(关键词:async·await·SaveChangesAsync·ToListAsync)
查看>>
百度编辑器ueditor每次编辑后多一个空行的解决办法
查看>>
C#扇形的绘制与Hittest交互、图种制作
查看>>
【MVC 4】5.SportsSore —— 一个真实的应用程序
查看>>
Lucene.Net:构造搜索表达式简化搜索
查看>>
Hadoop - Zeppelin 使用心得
查看>>
Android GIS开发系列-- 入门季(2) MapView与图层介绍
查看>>
爪哇国新游记之二十五----图及其遍历查找
查看>>
Windows Live Writer Technical Preview 公布下载
查看>>
iphone:使用NSFileManager取得目录下所有文件(遍历所有文件)
查看>>
IPK僵尸网络 看看其传播手法
查看>>
Visual Studio DSL 入门 14---用Wix制作安装程序
查看>>
SQL Server 各种查询语句执行返回结果
查看>>
Visual Studio 2017 针对移动开发的新特性介绍
查看>>
自定义Notification
查看>>
欧美软件外包系列 (一): 正确看待外包
查看>>
poj 1198 hdu 1401 搜索+剪枝 Solitaire
查看>>