forked from vert-x/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request vert-x#22 from vert-x3/feature/example-jwt
jwt example
- Loading branch information
Showing
5 changed files
with
129 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
web-examples/src/main/java/io/vertx/example/web/jwt/Server.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.vertx.example.web.jwt; | ||
|
||
import io.vertx.core.AbstractVerticle; | ||
import io.vertx.core.http.HttpHeaders; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.example.util.Runner; | ||
import io.vertx.ext.auth.AuthProvider; | ||
import io.vertx.ext.auth.jwt.JWTAuth; | ||
import io.vertx.ext.auth.jwt.JWTOptions; | ||
import io.vertx.ext.auth.shiro.ShiroAuth; | ||
import io.vertx.ext.auth.shiro.ShiroAuthRealmType; | ||
import io.vertx.ext.web.Router; | ||
import io.vertx.ext.web.handler.*; | ||
import io.vertx.ext.web.sstore.LocalSessionStore; | ||
|
||
/* | ||
* @author <a href="mailto:[email protected]">Paulo Lopes</a> | ||
*/ | ||
public class Server extends AbstractVerticle { | ||
|
||
// Convenience method so you can run it in your IDE | ||
public static void main(String[] args) { | ||
Runner.runExample(Server.class); | ||
} | ||
|
||
@Override | ||
public void start() throws Exception { | ||
|
||
Router router = Router.router(vertx); | ||
|
||
// Create a JWT Auth Provider | ||
JWTAuth jwt = JWTAuth.create(new JsonObject() | ||
.put("keyStoreType", "jceks") | ||
.put("keyStoreURI", "classpath:///keystore.jceks") | ||
.put("keyStorePassword", "secret")); | ||
|
||
// protect the API | ||
router.route("/api/*").handler(JWTAuthHandler.create(jwt, "/api/newToken")); | ||
|
||
// this route is excluded from the auth handler | ||
router.get("/api/newToken").handler(ctx -> { | ||
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "text/plain"); | ||
ctx.response().end(jwt.generateToken(new JsonObject(), new JWTOptions().setExpiresInSeconds(60))); | ||
}); | ||
|
||
// this is the secret API | ||
router.get("/api/protected").handler(ctx -> { | ||
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "text/plain"); | ||
ctx.response().end("a secret you should keep for yourself..."); | ||
}); | ||
|
||
// Serve the non private static pages | ||
router.route().handler(StaticHandler.create()); | ||
|
||
vertx.createHttpServer().requestHandler(router::accept).listen(8080); | ||
} | ||
} | ||
|
55 changes: 55 additions & 0 deletions
55
web-examples/src/main/java/io/vertx/example/web/jwt/webroot/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<html> | ||
<head> | ||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> | ||
</head> | ||
<body> | ||
|
||
<h1>Web site with public and private pages</h1> | ||
|
||
<br> | ||
<br> | ||
|
||
<div id="login"> | ||
<a href="#" id="generateToken">Get a Token</a> | ||
<div id="token">Current token:</div> | ||
</div> | ||
|
||
<br> | ||
<br> | ||
|
||
<div id="test"> | ||
<a href="#" id="getProtected">Get Protected Resource</a> | ||
<div id="protected"></div> | ||
</div> | ||
|
||
<script language="JavaScript"> | ||
$(document).ready(function () { | ||
$('#generateToken').on('click', function () { | ||
$.ajax({ | ||
url: '/api/newToken', | ||
dataType: 'text', | ||
success: function (text) { | ||
$('#token').html('Current Token: ' + text); | ||
} | ||
}); | ||
}); | ||
|
||
$('#getProtected').on('click', function () { | ||
$.ajax({ | ||
url: '/api/protected', | ||
dataType: 'text', | ||
headers: { | ||
"Authorization": "Bearer " + $('#token').html().substring(15) | ||
}, | ||
success: function (text) { | ||
$('#protected').html(text); | ||
}, | ||
error: function (err) { | ||
$('#protected').html('Error: ' + err.toString()); | ||
} | ||
}); | ||
}); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
Binary file not shown.