我正在尝试使用 Java EE 8 中的安全API ,对密码进行哈希处理。我创建了一个 @DatabaseIdentityStoreDefinition
@BasicAuthenticationMechanismDefinition(
realmName = "Book Realm"
)
@DatabaseIdentityStoreDefinition(
dataSourceLookup = "jdbc/userAuth",
callerQuery = "select password from users where USERNAME = ?",
groupsQuery = "select g.GROUP_NAME from USER_GROUPS ug, users u, GROUPS g where ug.USER_ID = u.user_id and g.GROUP_ID= ug.GROUP_ID and u.USERNAME=?"
hashAlgorithm = Pbkdf2PasswordHash.class,
hashAlgorithmParameters = {
"Pbkdf2PasswordHash.Iterations=3072",
"Pbkdf2PasswordHash.Algorithm=PBKDF2WithHmacSHA512",
"Pbkdf2PasswordHash.SaltSizeBytes=64"
}
)
@DeclareRoles({"user", "admin"})
@WebServlet("/securedServlet")
@ServletSecurity(
@HttpConstraint(rolesAllowed = {"admin"}))
public class SecuredServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("Congratulations, login successful.");
}
}
当我将war文件部署到Glassfish 5应用程序服务器时,出现以下异常:
java.lang.IllegalStateException: java.security.NoSuchAlgorithmException: PBKDF2WithHmacSHA512 SecretKeyFactory not available
此异常来自我创建的用于自动创建用户并将其插入数据库的类。
此类的相关部分:
@Resource(mappedName = "jdbc/userAuth")
private DataSource dataSource;
@Inject
private Pbkdf2PasswordHash passwordHash;
public void init(@Observes @Initialized(ApplicationScoped.class) Object init) {
Map<String, String> parameters = new HashMap<>();
parameters.put("Pbkdf2PasswordHash.Iterations", "3072");
parameters.put("Pbkdf2PasswordHash.Algorithm", "PBKDF2WithHmacSHA512");
parameters.put("Pbkdf2PasswordHash.SaltSizeBytes", "64");
passwordHash.initialize(parameters);
executeUpdate(dataSource, "INSERT INTO USERS (USER_ID,USERNAME,FIRST_NAME,MIDDLE_NAME,LAST_NAME,PASSWORD) VALUES (1,'beaver','Beaver','The Dam','Lake','" + passwordHash.generate("beaverattack".toCharArray()) + "')");
在pom.xml中,我具有以下依赖性:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.security.enterprise</groupId>
<artifactId>javax.security.enterprise-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
Java版本:
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)