Advertisement
Guest User

LoginBB.java

a guest
Mar 8th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. package com.jsfcourse.security;
  2.  
  3. import javax.faces.application.FacesMessage;
  4. import javax.faces.bean.ManagedBean;
  5. import javax.faces.context.FacesContext;
  6. import javax.servlet.http.HttpSession;
  7.  
  8. @ManagedBean
  9. public class LoginBB {
  10.     private static final String PAGE_MAIN = "index";
  11.     private static final String PAGE_LOGIN = "login";
  12.     private static final String PAGE_STAY_AT_THE_SAME = null;
  13.  
  14.     private String login;
  15.     private String pass;
  16.  
  17.     public String getLogin() {
  18.         return login;
  19.     }
  20.  
  21.     public void setLogin(String login) {
  22.         this.login = login;
  23.     }
  24.  
  25.     public String getPass() {
  26.         return pass;
  27.     }
  28.  
  29.     public void setPass(String pass) {
  30.         this.pass = pass;
  31.     }
  32.  
  33.     public boolean validateData() {
  34.         boolean result = true;
  35.         FacesContext ctx = FacesContext.getCurrentInstance();
  36.  
  37.         // check if not empty
  38.         if (login == null || login.length() == 0) {
  39.             ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
  40.                     "podaj login", "null"));
  41.         }
  42.  
  43.         if (pass == null || pass.length() == 0) {
  44.             ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
  45.                     "podaj hasło", "null"));
  46.         }
  47.  
  48.         if (ctx.getMessageList().isEmpty()) {
  49.             result = true;
  50.         } else {
  51.             result = false;
  52.         }
  53.         return result;
  54.  
  55.     }
  56.  
  57.     public String doLogin() {
  58.         FacesContext ctx = FacesContext.getCurrentInstance();
  59.         User user = null;
  60.  
  61.         // 1. check parameters and stay if errors
  62.         if (!validateData()) {
  63.             return PAGE_STAY_AT_THE_SAME;
  64.         }
  65.  
  66.         // 2. verify login and pass - get User from "database"
  67.         user = getUserFromDatabase(login, pass);
  68.  
  69.         // 3. if bad login or pass - stay with error info
  70.         if (user == null) {
  71.             ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
  72.                     "Niepoprawny login lub hasło", null));
  73.             return PAGE_STAY_AT_THE_SAME;
  74.         }
  75.  
  76.         // 4. if login ok - save User object in session
  77.         HttpSession session = (HttpSession) ctx.getExternalContext()
  78.                 .getSession(true);
  79.         session.setAttribute("user", user);
  80.  
  81.         // and enter the system
  82.         return PAGE_MAIN;
  83.     }
  84.  
  85.     public User getUser() {
  86.         HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
  87.                 .getExternalContext().getSession(true);
  88.         return (User) session.getAttribute("user");
  89.     }
  90.    
  91.     public String doLogout(){
  92.         HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
  93.                 .getExternalContext().getSession(true);
  94.         //Invalidate session
  95.         // - all objects within session will be destroyed
  96.         // - new session will be created (with new ID)
  97.         session.invalidate();
  98.         return PAGE_LOGIN;
  99.     }
  100.    
  101.     // simulate finding user in database
  102.     private User getUserFromDatabase(String login, String pass) {
  103.         User u = null;
  104.  
  105.         if (login.equals("login") && pass.equals("pass")) {
  106.             u = new User(login, pass);
  107.             u.setName("Jan");
  108.             u.setSurname("Kowalski");
  109.  
  110.             // assumed system roles
  111.             u.getRoles().add("role1");
  112.             u.getRoles().add("role2");
  113.             u.getRoles().add("admin");
  114.         }
  115.  
  116.         return u;
  117.     }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement