-
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 #35 from hotungkhanh/kan-77/backend-basic-authenti…
…cation Kan 77/backend basic authentication
- Loading branch information
Showing
23 changed files
with
407 additions
and
213 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
{ | ||
"conventionalCommits.scopes": ["database", "display ganttchart"] | ||
"conventionalCommits.scopes": [ | ||
"database", | ||
"display ganttchart" | ||
], | ||
"java.configuration.updateBuildConfiguration": "interactive" | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.acme.security.jpa; | ||
|
||
import io.quarkus.runtime.StartupEvent; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
import jakarta.transaction.Transactional; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
|
||
@Singleton | ||
public class Startup { | ||
@Inject | ||
Config config; | ||
|
||
@Transactional | ||
public void loadUsers(@Observes StartupEvent evt) { | ||
String username = config.getValue("frontend.username", String.class); | ||
String password = config.getValue("frontend.password", String.class); | ||
|
||
// reset and load user | ||
User.deleteAll(); | ||
User.add(username, password, "user"); | ||
} | ||
} |
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,36 @@ | ||
package org.acme.security.jpa; | ||
|
||
import io.quarkus.elytron.security.common.BcryptUtil; | ||
import io.quarkus.hibernate.orm.panache.PanacheEntity; | ||
import io.quarkus.security.jpa.Password; | ||
import io.quarkus.security.jpa.Roles; | ||
import io.quarkus.security.jpa.UserDefinition; | ||
import io.quarkus.security.jpa.Username; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Table; | ||
|
||
@Entity | ||
@Table(name = "frontend_user") | ||
@UserDefinition | ||
public class User extends PanacheEntity { | ||
@Username | ||
public String username; | ||
@Password | ||
public String password; | ||
@Roles | ||
public String role; | ||
|
||
/** | ||
* Adds a new user to the database | ||
* @param username the username | ||
* @param password the unencrypted password (it is encrypted with bcrypt) | ||
* @param role the comma-separated roles | ||
*/ | ||
public static void add(String username, String password, String role) { | ||
User user = new User(); | ||
user.username = username; | ||
user.password = BcryptUtil.bcryptHash(password); | ||
user.role = role; | ||
user.persist(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
backend/src/main/java/org/acme/security/jpa/UserResource.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,17 @@ | ||
package org.acme.security.jpa; | ||
|
||
import jakarta.annotation.security.RolesAllowed; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.core.Context; | ||
import jakarta.ws.rs.core.SecurityContext; | ||
|
||
@Path("/login") | ||
public class UserResource { | ||
|
||
@GET | ||
@RolesAllowed({"user"}) | ||
public String me(@Context SecurityContext securityContext) { | ||
return securityContext.getUserPrincipal().getName(); | ||
} | ||
} |
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
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
Oops, something went wrong.