1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.example.demo;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import javax.enterprise.context.RequestScoped;
26 import javax.faces.context.ExternalContext;
27 import javax.faces.context.FacesContext;
28 import javax.inject.Named;
29 import javax.servlet.ServletException;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpSession;
32 import java.io.IOException;
33 import java.lang.invoke.MethodHandles;
34
35 @Named
36 @RequestScoped
37 public class LoginController {
38
39 private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
40
41 private String username;
42 private String password;
43
44 public Outcome login() throws ServletException {
45 final FacesContext facesContext = FacesContext.getCurrentInstance();
46 final ExternalContext externalContext = facesContext.getExternalContext();
47 final HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
48
49 LOG.info("Try to login user: '{}'", username);
50 request.login(username, password);
51 LOG.info("Successful login user: '{}'", username);
52
53 return Outcome.CONCEPT_SECURITY_ROLES_XLOGIN;
54 }
55
56 public String logout() throws ServletException, IOException {
57 final FacesContext facesContext = FacesContext.getCurrentInstance();
58 final ExternalContext externalContext = facesContext.getExternalContext();
59 final HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
60
61 request.logout();
62
63 return facesContext.getViewRoot().getViewId();
64 }
65
66 public String resetSession() throws IOException {
67 LOG.info("Resetting the session.");
68 final FacesContext facesContext = FacesContext.getCurrentInstance();
69 final HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
70 if (session != null) {
71 session.invalidate();
72 }
73 final ExternalContext externalContext = facesContext.getExternalContext();
74
75
76
77
78
79 externalContext.redirect(externalContext.getRequestContextPath() + "/");
80 facesContext.responseComplete();
81 return null;
82 }
83
84 public String getUsername() {
85 return username;
86 }
87
88 public void setUsername(final String username) {
89 this.username = username;
90 }
91
92 public String getPassword() {
93 return password;
94 }
95
96 public void setPassword(final String password) {
97 this.password = password;
98 }
99 }