Khekhekhe
Khekhekhe

Reputation: 53

How to hard code username and password in Secure module Play 1.2.5

Below is the code found on http://www.playframework.com/documentation/1.2.5/secure.

package controllers;

public class Security extends Secure.Security {

static boolean authenticate(String username, String password) {

     User user = User.find("byEmail", username).first();
     return user != null && user.password.equals(password);
   }
}

I want to hardcode the username and password into the controller itself. So that it accepts only those login credentials.

I'm not sure how to do it, but it must be something like this:

package controllers;

public class Security extends Secure.Security {

static boolean authenticate(String username, String password) {

     if (username=="[email protected]" && password=="abc123")
     return....
     .......
   }
}

Upvotes: 0

Views: 1641

Answers (1)

RaptorDotCpp
RaptorDotCpp

Reputation: 1465

You are on the right track. However, some notes:

  • Don't check for String equality using the == operator. Use String#equals() instead.
  • When using equals, call the method on the literal String. These can never be null and help eliminating NullPointerExceptions.
  • Hardcoding the password and username as plain strings can be dangerous: they can be read from memory without too much effort.
  • Because username=="[email protected]" && password=="abc123" is a boolean value, you can simply return that result.

Disregarding point 3 about the security issue, your method could look like this:

static boolean authenticate(String username, String password) {
    return "[email protected]".equals(username) && "abc123".equals(password);
}

Upvotes: 1

Related Questions