简体   繁体   English

JSF Tomcat容器管理的身份验证以及其他登录按钮

[英]JSF Tomcat container managed authentication plus additional login button

I am currently expanding my JSF website. 我目前正在扩展我的JSF网站。 I use Tomcat container managed authentication mechanism (works well, user is forced to login before accessing protected area), and now I want to provide an additional login button. 我使用Tomcat容器管理的身份验证机制(效果很好,在访问保护区之前用户被迫登录),现在我想提供一个附加的登录按钮。

With the login button I want the user to get "LOGGED_IN_CUST" credentials in order that the web site provides additional features (like change / add / remove addresses) before reaching a secured page, like orderProducts.xhtml 使用登录按钮,我希望用户获得“ LOGGED_IN_CUST”凭据,以便网站在到达安全页面(如orderProducts.xhtml)之前提供其他功能(例如更改/添加/删除地址)。

Example: 例:

Current site: index.xhtml 当前站点:index.xhtml
If I click the login link on the upper right side the login.xhtml page is displayed: 如果单击右上角的登录链接,则会显示login.xhtml页面:

    <form action="j_security_check">
        <h:panelGrid columns="2" bgcolor="#eff5fa" cellspacing="5"
            frame="box" styleClass="center">
            <h:outputLabel value="User name:" />
            <h:inputText id="j_username" tabindex="1" />
            <h:outputLabel value="Password:" />
            <h:inputSecret id="j_password" />
            <h:outputLabel value="" />
            <h:commandButton id="login" value="Login" />
        </h:panelGrid>
    </form>

After using correct user credentials and pressing login button, I get the following error: 使用正确的用户凭据并按下登录按钮后,出现以下错误:

HTTP Status 400 - Invalid direct reference to form login page
message: Invalid direct reference to form login page
description: The request sent by the client was syntactically incorrect.

Is there a solution to log in by using an additional login link. 是否有使用其他登录链接登录的解决方案。 And after the customer successfully logged in, the site which was displayed before is active again? 在客户成功登录后,之前显示的站点又可以活动了吗? (In this example index.xhtml) (在本示例中,index.xhtml)

I have rebuilt the application. 我已经重建了应用程序。 Instead of the login form with the action attribute "j_security_check" I created a conventional commandButton which calls the following doLogin method 我创建了一个常规的commandButton ,而不是带有动作属性"j_security_check"的登录表单,该"j_security_check"调用以下doLogin方法

public void doLogin(ActionEvent e) throws IOException {
    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) context
            .getExternalContext().getRequest();

    try {
        // Try to login customer via container management
        request.login(eMail, password);

        /*
         * In case that the Login link was clicked, then the variable
         * "requestedURI" is null ! In that case we can redirect the
         * customer directly to his customerOverview site. A user with role
         * ADMIN will be redirected to systemInfo.xhtml and a user with role
         * CUST is redirected to customerOverview.xhtml.
         */
        if(requestedURI == null){
            if(request.isUserInRole(ROLE_ADMIN)){
                requestedURI = BASE_NAME+"/faces/sections/admin/systemInfo.xhtml";
            }
            else if(request.isUserInRole(ROLE_CUST)){
                requestedURI = BASE_NAME+"/faces/sections/customer/customerOverview.xhtml";
            }
            else{
                requestedURI = BASE_URL;
            }
        }

        /*
         * If everything worked well, redirect the customer to the desired
         * page
         */
        context.getExternalContext().redirect(requestedURI);
    } catch (ServletException se) {
        context.addMessage(null, new FacesMessage(Internationalization.getLoginFailed()));
    }
}

This solution works fine for me :) 这个解决方案对我来说很好:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM