springboot jwt starter

其他类别 2025-08-20

[中文版]

                _             _                 _       _          _         _             _
 ___ _ __  _ __(_)_ __   __ _| |__   ___   ___ | |_    (_)_      _| |_   ___| |_ __ _ _ __| |_ ___ _ __
/ __| '_ | '__| | '_  / _` | '_  / _  / _ | __|   |   / / / __| / __| __/ _` | '__| __/ _  '__|
__  |_) | |  | | | | | (_| | |_) | (_) | (_) | |_    | | V  V /| |_  __  || (_| | |  | ||  __/ |
|___/ .__/|_|  |_|_| |_|__, |_.__/ ___/ ___/ __|  _/ | _/_/  __| |___/____,_|_|   _____|_|
    |_|                 |___/                        |__/

带有AngularJS和Springboot(JSON Web令牌)的基于Springboot令牌的安全入门套件

如果您要使用Angular 4进行前端实现,请签出Angular-Spring-Starter,一个带有Angular 4,路由器,形式,http,Services,Services,Spring Boot,Spring Boot,JSON Web Token的Fullstack入门套件

现场演示

身份验证是使用JWT的最常见情况。登录用户后,每个后续请求将包括JWT,允许用户访问该令牌允许的路由,服务和资源。单个标志是当今广泛使用JWT的功能,因为它的开销很小,并且能够轻松地在不同域中使用。

- auth0

快速开始

确保您有Maven和Java 1.7或更高

 # clone our repo
# --depth 1 removes all but one .git commit history
git clone --depth 1 https://*github.*co*m/bfwg/springboot-jwt-starter.git

# change directory to our repo
cd springboot-jwt-starter

# install the repo with mvn
mvn install

# start the server
mvn spring-boot:run

# the app will be running on port 8080
# there are two built-in user accounts to demonstrate the differing levels of access to the endpoints:
# - User - user:123
# - Admin - admin:123

与Docker一起运行

 docker-compose up --build -d

文件结构

 springboot-jwt-starter/
 ├──src/                                                        * our source files
 │   ├──main
 │   │   ├──java.com.bfwg
 │   │   │   ├──config
 │   │   │   │   └──WebSecurityConfig.java                      * config file for filter, custom userSerivce etc.
 │   │   │   ├──model
 │   │   │   │   ├──Authority.java
 │   │   │   │   ├──UserTokenState.java                         * JWT model
 │   │   │   │   └──User.java                                   * our main User model
 │   │   │   ├──repository                                      * repositories folder for accessing database
 │   │   │   │   └──UserRepository.java
 │   │   │   ├──rest                                            * rest endpoint folder
 │   │   │   │   ├──AuthenticationController.java               * auth related REST controller, refresh token endpoint etc.
 │   │   │   │   └──UserController.java                         * REST controller to handle User related requests
 │   │   │   ├──security                                        * Security related folder(JWT, filters)
 │   │   │   │   ├──auth
 │   │   │   │   │   ├──JwtAuthenticationRequest.java           * login request object, contains username and password
 │   │   │   │   │   ├──RestAuthenticationEntryPoint.java       * handle auth exceptions, like invalid token etc.
 │   │   │   │   │   ├──TokenAuthenticationFilter.java          * the JWT token filter, configured in WebSecurityConfig
 │   │   │   │   │   └──TokenBasedAuthentication.java           * this is our custom Authentication class and it extends AbstractAuthenticationToken
 │   │   │   │   └──TokenHelper.java                            * token helper class
 │   │   │   ├──service
 │   │   │   │   ├──impl
 │   │   │   │   │   ├──CustomUserDetailsService.java           * custom UserDetailsService implementation, tells formLogin() where to check username/password
 │   │   │   │   │   └──UserServiceImpl.java
 │   │   │   │   └──UserService.java
 │   │   │   └──Application.java                                * Application main class
 │   │   └──recources
 │   │       ├──static                                          * static assets are served here (Angular and html templates)
 │   │       ├──application.yml                                 * application variables are configured here
 │   │       └──import.sql                                      * h2 database query (table creation)
 │   └──test                                                    * Junit test folder
 └──pom.xml                                                     * what maven uses to manage its dependencies and configuration

目录

  • 文件结构
  • 配置
  • JSON网络令牌

配置

  • websecurityconfig.java :服务器端身份验证配置。
  • application.yml :应用程序级别属性即代币过期,令牌秘密等。您可以在此处找到所有应用程序属性的参考。
  • JWT令牌TTL :JWT令牌配置为10分钟后到期,您可以通过再次登录来获得新的令牌。
  • 使用其他数据库:此入门套件使用嵌入式H2数据库,该数据库自动由Spring Boot配置。如果要连接到另一个数据库,则必须在资源目录中的应用程序中指定连接。这是MySQL DB的示例:
 spring:
  jpa:
    hibernate:
      # possible values: validate | update | create | create-drop
      ddl-auto: create-drop
  datasource:
    url: jdbc:mysql://localhost/myDatabase
    username: myUser
    password: myPassword
    driver-class-name: com.mysql.jdbc.Driver

提示:对于其他数据库,例如MySQL序列不适用于ID生成。因此,您必须将实体豆中的生成型更改为“自动”或“身份”。

JSON网络令牌

JSON Web令牌是一种开放的行业标准RFC 7519方法,用于在两方之间牢固地表示索赔。有关更多信息,请结帐https://j*wt.i*o*/

贡献

我会接受几乎所有的东西

这个项目不受欢迎

  • Cerberus
  • jwt-spring-security-demo

执照

麻省理工学院

下载源码

通过命令行克隆项目:

git clone https://github.com/bfwg/springboot-jwt-starter.git