This tiny library will let you add a simple auth mechanism. You must to provide your own implementation of UserService interface to tell to the login service where to fetch the user data.
By default this library uses JOSE implementation for JWT token creation. It has a password validation in order to validate the user credentials. It uses BCrypt with a salt to verify the encoded password.
<dependency>
<groupId>io.github.gdiazs</groupId>
<artifactId>microprofile-jwt-login</artifactId>
<version>0.0.2</version>
</dependency>Once you have imported this library in a Microprofile project built with https://start.microprofile.io/ you just need to configure a similar class like this. You can provide your own user service implementation if you want
@Dependent
public class AuthservicesConfig {
@Produces
public UserService userService() {
UserServiceBuilder.userBuilder()
.addUser("gdiazs", "$2y$06$HJMVVcT0mBshzc9ZCLtJXuwi0R4CPuKGbJDGVlyGYAt6KnM9UfC6C", "admin", "tester")
.addUser("memo", "$2y$06$HJMVVcT0mBshzc9ZCLtJXuwi0R4CPuKGbJDGVlyGYAt6KnM9UfC6C", "developer");
return UserServiceBuilder.build();
}
@Produces
public PasswordEncoder passwordEncoder() {
return new PasswordEncoderDefault(6);
}
}Don't forget add this properties
microprofile-config.properties
microprofile.jwt.secret=yoursecrethash
microprofile.jwt.privateKey=/privateKey.pem
microprofile.jwt.algorithm=RS256 # or HS256 depends on this the library uses secret or privateKey
microprofile.jwt.aud=web
microprofile.jwt.iss=https://server.example.com
microprofile.jwt.expiration=30000
microprofile.jwt.keyID=JWT-MPPOST http://localhost:port/context/auth
{
"username": "gdiazs",
"password": "Test1234"
}