-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBasicAuthenticationExampleController.java
More file actions
43 lines (30 loc) · 1.23 KB
/
BasicAuthenticationExampleController.java
File metadata and controls
43 lines (30 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package controllers;
import org.slf4j.LoggerFactory;
import play.mvc.Controller;
import play.mvc.Http;
import play.mvc.Result;
import java.util.Base64;
import java.util.Optional;
public class BasicAuthenticationExampleController extends Controller {
final org.slf4j.Logger logger = LoggerFactory.getLogger(FormExampleController.class);
/**
* Basic Authentication example
*/
public Result authenticate(Http.Request request) {
// read the header
Optional<String> s = request.header("Authorization");
logger.info("Authorization header: " + s.orElse("is not present"));
if (s.isEmpty()){
// return error, if header is not present
return unauthorized().withHeader("WWW-Authenticate","Basic realm=\"Access\"");
}
String base64decoded = new String(Base64.getDecoder().decode(s.get().split(" ")[1]));
logger.info("Decoded header: " + base64decoded);
if (base64decoded.equals("admin:123")){
return ok("user name and password are valid. Access granted");
} else{
// ask for username and password again
return unauthorized().withHeader("WWW-Authenticate","Basic realm=\"Access\"");
}
}
}