2013-11-23 3 views
-1

내 JSF 프로젝트에 대한 로그인을 작성하고이를 테스트하기 위해 개인 섹션을 정의했습니다. 지금 누군가가 예를 들어 "/private/test.html"을 요청JSF 로그인 후 요청한 페이지로 리디렉션

@Named 
@SessionScoped 
public class LoginService implements Serializable { 
    private String username = ""; 
    private String password = ""; 
    @Inject 
    private NavigationService navigationService = null; 
    @Inject 
    private DAO dao = null; 
    private String originalURL = ""; 

    @PostConstruct 
    public void init() { 
     ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); 

     if(externalContext.getRequestMap().containsKey("originalURL")) { 
      this.originalURL = (String)externalContext.getRequestMap().get("originalURL"); 
     } else { 
      this.originalURL = navigationService.externalContextRedirectToIndex(); 
     } 
    } 

    public void doLogin() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { 
     FacesContext context = FacesContext.getCurrentInstance(); 
     ExternalContext externalContext = context.getExternalContext(); 

     User user = dao.findUserByUsername(this.username); 

     boolean validated = PasswordHasher.validatePassword(this.password, user.getPassword()); 

     if(user != null && validated) { 
      externalContext.getSessionMap().put("user", user); 
      vexternalContext.redirect(this.originalURL); 
     } else { 
      context.addMessage(null, new FacesMessage("False username or password!")); 
     } 
    } 

    public void doLogout() throws IOException { 
     ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); 

     externalContext.invalidateSession(); 
     externalContext.redirect(externalContext.getRequestContextPath() + navigationService.toLogin()); 
    } 

    // Getter and Setter 
} 

경우 내 로그인 페이지가 나타나고이 내 LoginService입니다

@WebFilter("/private/*") 
public class LoginFilter implements Filter { 
    @Inject 
    private NavigationService navigationService = null; 

    @Override 
    public void init(FilterConfig config) throws ServletException {} 

    @Override 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
     HttpServletRequest req = (HttpServletRequest)request; 
     HttpServletResponse res = (HttpServletResponse)response; 

     String originalURL = req.getRequestURL().toString(); 

     if(req.getQueryString() != null) { 
      originalURL += "?" + req.getQueryString(); 
     } 

     User user = (User) req.getSession().getAttribute("user"); 

     if(user != null) { 
      chain.doFilter(request, response); 
     } else { 
      req.getSession().setAttribute("originalURL", originalURL); 

      res.sendRedirect(req.getContextPath() + navigationService.toLogin()); 
     } 
    } 

    @Override 
    public void destroy() {} 
} 

:

내 LoginFilter입니다 사용자가 로그인해야합니다. 그 후 사용자가 성공적으로 에 로그인 한 경우 원래 요청한 test.html로 리디렉션하고 싶습니다.

이제 sessionMap에는 "originalURL"이라는 키가 없으며 항상 이 내 index.html로 리디렉션됩니다.

는 Btw는 내 navigationservice는 두 가지 방법을 제공합니다 :

public String redirectToIndex() { 
    return "/index.html?faces-redirect=true"; 
} 

public String externalContextRedirectToIndex() { 
    return "/Web-Tschuwwa/index.html?faces-redirect=true"; 
} 

내가 첫 번째 방법을 사용할 때 내가 index.html을 의 리디렉션 때문에 내가 로그인 후 리디렉션을 위해 특별히 추가 한 두 번째 방법 내 glassfish docsroot 폴더. 그 이유는 내가 모르는 또 다른 것입니다. Web-Tschuwwa는 내 프로젝트의 이름입니다.

로그인 한 후 요청한 페이지로 어떻게 리디렉션 할 수 있습니까?

답변

0

아 코드의 실수 .....

변경이 있었다 :이 상점

if(externalContext.getRequestMap().containsKey("originalURL")) { 
    this.originalURL = (String)externalContext.getRequestMap().get("originalURL"); 
} else { 
    this.originalURL = navigationService.externalContextRedirectToIndex(); 
} 

가 :

if(externalContext.getSessionMap().containsKey("originalURL")) { 
    this.originalURL = (String)externalContext.getSessionMap().get("originalURL"); 
} else { 
    this.originalURL = navigationService.externalContextRedirectToIndex(); 
} 

그리고 그것은 작동합니다.

관련 문제