It is a demo project to configure OAuth2 authorization server with JWT token using Spring Boot 2, JPA, MySQL database and Gradle
-
To configure this project you can generate you keystore file using following command :
keytool -genkeypair -alias jwt -keyalg RSA -keypass password -keystore jwt.jks -storepass password
Note: Windows user you can find keytool in JDK directory. It will generate jwt.jks which contains the Public and Private keys.
-
It is recommended to migrate above generated key to PKCS12. Execute the following command to generate PKCS12
keytool -importkeystore -srckeystore jwt.jks -destkeystore jwt.jks -deststoretype pkcs12
-
Generate public key using following command
keytool -list -rfc --keystore jwt.jks | openssl x509 -inform pem -pubkey
You will get public key on console after executing the above command.
-
Copy from (including) -----BEGIN PUBLIC KEY----- to -----END PUBLIC KEY----- and save it in file public.key file. It will be used in resource server.
Import the project in IntelliJ IDEA by using following steps
- Start IntelliJ IDEA
- Click on Open(if no project is opened) or go to file -> open
- Select build.gradle file in the cloned repository and click on Open as Project
- After build completion, Run Application.java
- Uncomment following code in Application.java to insert admin user for the first time
@Bean public CommandLineRunner setupDefaultUser(UserService service) { return args -> { service.save(new User(1, "admin", "password", Arrays.asList(new Role(1,"ROLE_ADMIN"), new Role(2, "ROLE_EMPLOYEE")),//roles true )); }; }
-
You can use Postman to test the app. Enter client(client id) and secret(client secret) for basic authentication.
-
Enter username and password to generate token. Use password instead of client_credentials for grant_type parameter to test throught password
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.