使用JWT(Json Web Token)与Spring Security和Spring Boot 2的演示
使用JWT(Json Web Token)与Spring Security和Spring Boot 2的演示
这是一个使用 JWT(JSON Web Token) 与 Spring Security 以及 Spring Boot 的示例项目。我完全重写了第一版。现在这个解决方案基于 JHipster 项目的代码库。我尝试提取了 JWT 认证所需的最小配置和类,并做了一些修改。
此示例使用 Maven 3.6.x 和 Java 11 构建。
只需使用 Spring Boot Maven 插件启动应用程序(mvn spring-boot:run)。应用程序运行在
http://localhost:8080。
您可以使用 H2-Console 在 http://localhost:8080/h2-console 下探索数据库:
提供了三个用户账户,用于演示对 API 中端点的不同访问权限级别以及不同的授权异常:
Admin - admin:admin
User - user:password
Disabled - disabled:password (this user is deactivated)有四个端点适合用于演示:
/api/authenticate - authentication endpoint with unrestricted access
/api/user - returns detail information for an authenticated user (a valid JWT token must be present in the request header)
/api/persons - an example endpoint that is restricted to authorized users with the authority 'ROLE_USER' (a valid JWT token must be present in the request header)
/api/hiddenmessage - an example endpoint that is restricted to authorized users with the authority 'ROLE_ADMIN' (a valid JWT token must be present in the request header)我编写了一个小型的 JavaScript 客户端,并在代码中添加了一些注释,希望能让这个演示易于理解。 您可以在 /src/main/resources/static/js/client.js 找到它。
我使用 bcrypt 来对密码进行加密。您可以使用这个简单的工具生成哈希值: Bcrypt Generator
暂无开放 Issues,或尚未同步最近议题。