예제 #1
0
 @PostConstruct
 public void init() {
   // TODO temporary convience for login
   setUserName("");
   setPassword("");
   Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
   printUser(authToken);
   if (authToken != null) {
     setLoggedUser(authToken.getName());
     setLoggedRole(validateAdmin() ? Role.ADMIN.display() : Role.USER.display());
   }
 }
예제 #2
0
  public String login() throws ServletException, IOException {
    try {

      AppUser user = new AppUser();
      user.setLogin("admin");
      user.setPassword("password");
      user.addRole(Role.USER.value());
      user.addRole(Role.ADMIN.value());
      getUserSvc().update(user);
      System.out.println("LoginForm" + getUserSvc().getById(userName));

      WebApplicationContext ac =
          WebApplicationContextUtils.getWebApplicationContext(
              (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext());
      AuthenticationManager authenticationManager = ac.getBean(AuthenticationManager.class);
      Authentication authentication =
          authenticationManager.authenticate(
              new UsernamePasswordAuthenticationToken(getUserName(), getPassword()));
      SecurityContextHolder.getContext().setAuthentication(authentication);

      setLoggedUser(authentication.getName());
      setLoggedRole(validateAdmin() ? Role.ADMIN.display() : Role.USER.display());

      getSessionModel().reset();

    } catch (Exception ex) {
      // log.equals(ex.getMessage());
      // ConfigUtil.addMessage("Login Failed: " + ex.getMessage());
      ex.printStackTrace();
      String message = ex.getLocalizedMessage();
      if (message == null) {
        message = "Incorrect user name or password";
      }
      FacesContext.getCurrentInstance()
          .addMessage("growl", new FacesMessage("Error", "Login Failed: " + message));
      return null;
    }

    return ConfigUtil.getSavedUrl() + "?faces-redirect=true";
  }