Skip to content

Commit

Permalink
add oidc
Browse files Browse the repository at this point in the history
  • Loading branch information
xindongbook committed Jun 14, 2020
1 parent 2f0e35d commit cfdc7bf
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/com/oauth/ch04/JWTTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ public static void main(String[] args) {

System.out.println("jwt header:" + header);
System.out.println("jwt body:" + body);
System.out.println("jwt body:" + body.getSubject());
System.out.println("jwt sub:" + body.getSubject());
System.out.println("jwt aud:" + body.getAudience());
System.out.println("jwt iss:" + body.getIssuer());
System.out.println("jwt exp:" + body.getExpiration());
System.out.println("jwt iat:" + body.getIssuedAt());
}

}
51 changes: 51 additions & 0 deletions src/com/oauth/ch09/AppIndexServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.oauth.ch09;

import com.my.util.URLParamsUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* **
* 使用此类来模拟【第三方软件的首页】
* 浏览器输入 http://localhost:8080/AppIndexServlet-ch09
*/
@WebServlet("/AppIndexServlet-ch09")
public class AppIndexServlet extends HttpServlet {

//8080:三方软件,8081:授权服务,8081:受保护资源服务 为了演示方便我们将授权服务和受保护资源服务放在同一个服务上面

String oauthUrl = "http://localhost:8081/OauthServlet-ch09?reqType=oauth";
String redirectUrl = "http://localhost:8080/AppServlet-ch09";


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//授权码许可流程,DEMO CODE
System.out.println("app index ...");

Map<String, String> params = new HashMap<String, String>();
params.put("response_type","code");
params.put("redirect_uri",redirectUrl);
params.put("app_id","APPID_RABBIT");
params.put("scope","today history");


String toOauthUrl = URLParamsUtil.appendParams(oauthUrl,params);//构造请求授权的URl

System.out.println("toOauthUrl: "+toOauthUrl);

response.sendRedirect(toOauthUrl);//授权码流程的【第一次】重定向

}
}
109 changes: 109 additions & 0 deletions src/com/oauth/ch09/AppServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.oauth.ch09;

import com.my.util.HttpURLClient;
import io.jsonwebtoken.*;

import javax.crypto.spec.SecretKeySpec;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Key;
import java.util.HashMap;
import java.util.Map;


/**
* **
* 使用此类来模拟【第三方软件的Server端】
*
*/
@WebServlet("/AppServlet-ch09")
public class AppServlet extends HttpServlet {

String oauthURl="http://localhost:8081/OauthServlet-ch09";
String protectedURl="http://localhost:8081/ProtectedServlet-ch09";

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//授权码许可流程,DEMO CODE

String code = request.getParameter("code");

Map<String, String> params = new HashMap<String, String>();
params.put("code",code);
params.put("grant_type","authorization_code");
params.put("app_id","APPID_RABBIT");
params.put("app_secret","APPSECRET_RABBIT");

System.out.println("start post code for token ...");
String result = HttpURLClient.doPost(oauthURl,HttpURLClient.mapToStr(params));

System.out.println("result:"+result);
String[] arry = result.split("&");
String accessToken = arry[0];
String id_token = arry[1];

System.out.println("accessToken:"+accessToken);
System.out.println("id_token:"+id_token);

//获取用户登录标识
Map<String,String> map = parseJwt(id_token);

request.setAttribute("sub",map.get("sub"));

//跳转到授权页面
request.getRequestDispatcher("/oidc.jsp").forward(request,response);


/*//使用 accessToken 请求受保护资源服务
Map<String, String> paramsMap = new HashMap<String, String>();
paramsMap.put("app_id","APPID_RABBIT");
paramsMap.put("app_secret","APPSECRET_RABBIT");
paramsMap.put("token",accessToken);
HttpURLClient.doPost(protectedURl,HttpURLClient.mapToStr(paramsMap));
*/

}

private Map<String,String> parseJwt(String jwt){
String sharedTokenSecret="hellooauthhellooauthhellooauthhellooauth";
Key key = new SecretKeySpec(sharedTokenSecret.getBytes(),
SignatureAlgorithm.HS256.getJcaName());

Map<String,String> map = new HashMap<String, String>();

Jws<Claims> claimsJws = Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(jwt);

// JwsHeader header = claimsJws.getHeader();
Claims body = claimsJws.getBody();

// System.out.println("jwt header:" + header);
System.out.println("jwt body:" + body);

map.put("sub",body.getSubject());
map.put("aud",body.getAudience());
map.put("iss",body.getIssuer());

return map;
}


public static void main(String[] args) {

String ss="95fd88bc-c69e-4add-bf3a-5d75766b85a7&eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJYSUFPTUlOR1RFU1QiLCJhdWQiOiJBUFBJRF9SQUJCSVQiLCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjgwODEvIiwiZXhwIjoxNTg0MTA1NzkwNzAzLCJpYXQiOjE1ODQxMDU5NDgzNzJ9.SoJT62wYOMihpaH3Ttxf3WYwnC6qEyKbJ-bF7jMqxL8";

String[] arry = ss.split("&");
System.out.println("access_token:"+arry[0]);
System.out.println("id_token:"+arry[1]);
}

}
9 changes: 6 additions & 3 deletions src/com/oauth/ch09/OauthServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class OauthServlet extends HttpServlet {
//模拟第三方软件注册之后的数据库存储
appMap.put("app_id","APPID_RABBIT");
appMap.put("app_secret","APPSECRET_RABBIT");
appMap.put("redirect_uri","http://localhost:8080/AppServlet-ch03");
appMap.put("redirect_uri","http://localhost:8080/AppServlet-ch09");
appMap.put("scope","today history");

}
Expand Down Expand Up @@ -115,7 +115,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
//GENATE ID TOKEN
String id_token=genrateIdToken(appId,"XIAOMINGTEST");//模拟用户小明登录

response.getWriter().write(accessToken+"|"+id_token);
response.getWriter().write(accessToken+"&"+id_token);

}

Expand Down Expand Up @@ -159,6 +159,9 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
String scope = request.getParameter("scope");

System.out.println("8081 GET responseType: "+responseType);
System.out.println("8081 GET redirect_uri: "+redirectUri);
System.out.println("8081 GET app_id: "+appId);
System.out.println("8081 GET scope: "+scope);

if(!appMap.get("app_id").equals(appId)){
return;
Expand All @@ -185,7 +188,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
request.setAttribute("app_id",appId);

//跳转到授权页面
request.getRequestDispatcher("/approve.jsp").forward(request,response);
request.getRequestDispatcher("/approve-09.jsp").forward(request,response);

//至此颁发授权码code的准备工作完毕

Expand Down
76 changes: 76 additions & 0 deletions src/com/oauth/ch09/ProtectedServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.oauth.ch09;

import com.oauth.ch09.OauthServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
* **
* 使用此类来模拟【受保护资源服务】
*/
@WebServlet("/ProtectedServlet-ch09")
public class ProtectedServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//省略验证代码

String accessToken = request.getParameter("token");

//根据当时授权的token对应的权限范围,做相应的处理动作
//不同权限对应不同的操作
String[] scope = OauthServlet.tokenScopeMap.get(accessToken);

StringBuffer sbuf = new StringBuffer();
for(int i=0;i<scope.length;i++){
sbuf.append(scope[i]).append("|");
}

if(sbuf.toString().indexOf("query")>0){
queryGoods("");
}

if(sbuf.toString().indexOf("add")>0){
addGoods("");
}

if(sbuf.toString().indexOf("del")>0){
delGoods("");
}

//不同的用户对应不同的数据
String user = OauthServlet.tokenMap.get(accessToken);
queryOrders(user);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {




}


private String queryGoods(String id){
return "";
}

private boolean addGoods(String goods){
return true;
}

private boolean delGoods(String id){
return true;
}

private String queryOrders(String user){
return "";
}

}
37 changes: 37 additions & 0 deletions web/approve-09.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Oauth Test</title>
</head>
<body>

<form action="/OauthServlet-ch09" method="post">
<input type="hidden" name="reqid" value="<%=request.getAttribute("reqid")%>" />
<input type="hidden" name="response_type" value="<%=request.getAttribute("response_type")%>" />
<input type="hidden" name="redirect_uri" value="<%=request.getAttribute("redirect_uri")%>" />
<input type="hidden" name="app_id" value="<%=request.getAttribute("app_id")%>" />

<!--模拟 approve 动作-->
<input type="hidden" name="reqType" value="approve" />

Are you sure you want the authorization code?

<br>
appid: <%=request.getAttribute("app_id")%>

<br>
<input type="checkbox" value="today" name="rscope" checked/>today<br>
<input type="checkbox" value="history" name="rscope"/>history<br>
<%--<input type="checkbox" value="pic" name="rscope"/>pic<br>--%>

<br>

<input type="submit" value="approve"/> <input type="submit" value="refuse"/>
<br>


</form>

</body>
</html>
5 changes: 3 additions & 2 deletions web/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
<title>OAuth 2 code test</title>
</head>
<body>
Hello World
OAuth 2 code test
please go to ch03 or ch09
</body>
</html>
12 changes: 12 additions & 0 deletions web/oidc.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Oauth Test</title>
</head>
<body>

hello ,<%=request.getAttribute("sub")%> ,you have signed in successfully。

</body>
</html>

0 comments on commit cfdc7bf

Please sign in to comment.