AEM6 :: Sling Servlet未找到404

时间:2015-02-25 03:47:57

标签: aem siteminder

我创建了一个包含表单的登录组件,它对指向siteminder URL https://test1-myuhc.uhc.com/siteminderagent/forms/login-aa.fcc

的URL有POST操作
<input type="hidden" name="target" value="/bin/uhc/myuhcauthenticationhandler">

在表单内部,“目标”隐藏字段是siteminder转发请求的位置。我在这里设置的目标指向Sling Servlet。某种身份验证时的Servlet会重定向到AEM中的相应页面。问题是当我在组件上提交登录按钮时,siteminder将请求转发给https://test1-myuhc.uhc.com/bin/uhc/myuhcauthenticationhandler,这会产生404错误。当我将目标设置为存在于/ content下的某个真实页面时,它可以正常工作。我该怎么做才能解决这个问题。

以下是代码:

package com.myuhc.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.settings.SlingSettingsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// TODO: Auto-generated Javadoc
/**
 * The Class LoginAuthenticationHandlerServlet.
 */
/*@SlingServlet(
        paths={"/bin/myuhc/authenticationhandler"},
        methods = {"POST","GET"},
        metatype=true
        )*/
@Component(immediate = true, metatype = true)
@Service
@Properties({
        @Property(name = "sling.servlet.paths", value = "/bin/uhc/myuhcauthenticationhandler"),
        @Property(name = "sling.servlet.methods", value = {"GET","POST"}) })
public class LoginAuthenticationHandlerServlet extends SlingAllMethodsServlet {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /** The Constant log. */
    private static final Logger log = LoggerFactory.getLogger(LoginAuthenticationHandlerServlet.class);

    @Reference
    private SlingSettingsService settingsService;

    /* (non-Javadoc)
     * @see org.apache.sling.api.servlets.SlingAllMethodsServlet#doPost(org.apache.sling.api.SlingHttpServletRequest, org.apache.sling.api.SlingHttpServletResponse)
     */
    @Override
    protected void doPost(SlingHttpServletRequest request,SlingHttpServletResponse response) {
        try {
            if (settingsService.getRunModes().contains("publish")) {
                //response.setStatus(HttpServletResponse.SC_OK);
                response.setContentType("text/plain");
                Cookie[] cookies = request.getCookies();
                String cookieValue = null;
                String uhcRole = null;
                String uhcaccountstatus = null;
                String url = null;
                String LOGIN_PAGE = "/content/myuhc/en/myuhc-login-page.html";
                String HOME_PAGE = "/content/myuhc/en/myuhc-home-page.html";
                //getting headers
                uhcRole = request.getHeader("uhcRole");
                uhcaccountstatus = request.getHeader("uhcaccountstatus");
                //getting SMRESPONSECODE cookie set by Siteminder
                if (cookies !=null) {
                    for (Cookie cookie : cookies) {
                        if(cookie.getName().equals("SMRESPONSECODE")) {
                            cookieValue = cookie.getValue();
                        }
                        // TODO:: Needs to be validated
                        cookie.setMaxAge(-1);
                    }
                    //Setting the URL by checking the different conditions
                    if (cookieValue !=null) {
                        if (cookieValue.equals("1")) {
                            request.setAttribute("message", "Member not found");
                            url = LOGIN_PAGE;
                        }
                        else if(cookieValue.equals("2")) {
                            request.setAttribute("message", "Wrong password. Please try again.");
                            url = LOGIN_PAGE;
                        }
                        else if((uhcaccountstatus!=null) && !uhcaccountstatus.equals("A") && cookieValue.equals("3")) {
                            request.setAttribute("message", "Member not found");
                            url = LOGIN_PAGE;
                        }
                        else if((uhcaccountstatus!=null) && (uhcRole!=null) && !uhcRole.equals("employer") && uhcaccountstatus.equals("A") && cookieValue.equals("0")) {
                            url = HOME_PAGE;
                        }
                        else if((uhcRole!=null) && uhcRole.equals("employer") && cookieValue.equals("4")) {
                            url = HOME_PAGE;
                        }
                        else if (cookieValue.equals("5") || cookieValue.equals("6")) {
                            request.setAttribute("message", "Member not found or Wrong password");
                            url = LOGIN_PAGE;
                        }

                    }else {
                        log.info("No Cookies found!");
                    }
                }
                //Forwarding the request to the url set above
                request.getRequestDispatcher(url).forward(request, response);
            }
        }
        catch (ServletException e) {
            log.info("Inside ServletException -->"+e.getMessage());
        }
        catch (IOException e) {
            log.info("Inside IOException -->"+e.getMessage());
        }
    }

    /* (non-Javadoc)
     * @see org.apache.sling.api.servlets.SlingAllMethodsServlet#doGet(org.apache.sling.api.SlingHttpServletRequest, org.apache.sling.api.SlingHttpServletResponse)
     */
    @Override
    protected void doGet(SlingHttpServletRequest request,SlingHttpServletResponse response) {
        try {
            if (settingsService.getRunModes().contains("publish")) {
                //response.setStatus(HttpServletResponse.SC_OK);
                response.setContentType("text/plain");
                Cookie[] cookies = request.getCookies();
                String cookieValue = null;
                String uhcRole = null;
                String uhcaccountstatus = null;
                String url = null;
                String LOGIN_PAGE = "/content/myuhc/en/myuhc-login-page.html";
                String HOME_PAGE = "/content/myuhc/en/myuhc-home-page.html";
                //getting headers
                uhcRole = request.getHeader("uhcRole");
                uhcaccountstatus = request.getHeader("uhcaccountstatus");
                //getting SMRESPONSECODE cookie set by Siteminder
                if (cookies !=null) {
                    for (Cookie cookie : cookies) {
                        if(cookie.getName().equals("SMRESPONSECODE")) {
                            cookieValue = cookie.getValue();
                        }
                        // TODO:: Needs to be validated
                        cookie.setMaxAge(-1);
                    }
                    //Setting the URL by checking the different conditions
                    if (cookieValue !=null) {
                        if (cookieValue.equals("1")) {
                            request.setAttribute("message", "Member not found");
                            url = LOGIN_PAGE;
                        }
                        else if(cookieValue.equals("2")) {
                            request.setAttribute("message", "Wrong password. Please try again.");
                            url = LOGIN_PAGE;
                        }
                        else if((uhcaccountstatus!=null) && !uhcaccountstatus.equals("A") && cookieValue.equals("3")) {
                            request.setAttribute("message", "Member not found");
                            url = LOGIN_PAGE;
                        }
                        else if((uhcaccountstatus!=null) && (uhcRole!=null) && !uhcRole.equals("employer") && uhcaccountstatus.equals("A") && cookieValue.equals("0")) {
                            url = HOME_PAGE;
                        }
                        else if((uhcRole!=null) && uhcRole.equals("employer") && cookieValue.equals("4")) {
                            url = HOME_PAGE;
                        }
                        else if (cookieValue.equals("5") || cookieValue.equals("6")) {
                            request.setAttribute("message", "Member not found or Wrong password");
                            url = LOGIN_PAGE;
                        }

                    }else {
                        log.info("No Cookies found!");
                    }
                }
                //Forwarding the request to the url set above
                request.getRequestDispatcher(url).forward(request, response);
            }
        }
        catch (ServletException e) {
            log.info("Inside ServletException -->"+e.getMessage());
        }
        catch (IOException e) {
            log.info("Inside IOException -->"+e.getMessage());
        }
    }
}

,这是表格

<form id="site-login" method="post" action="https://test1-myuhc.uhc.com/siteminderagent/forms/login-aa.fcc">
                    <fieldset class="borderless">
                        <legend class="hide-text">
                           ${xss:encodeForHTML(xssAPI, loginTitle)}
                        </legend>
                        <div>
                            <label for="username" class="micro strong label--inline">${xss:encodeForHTML(xssAPI, usrnamelbl)}</label>
                            <input type="text" id="username" name="USER" class="input--login" />
                        </div>
                        <div>
                            <label for="password" class="micro strong label--inline">${xss:encodeForHTML(xssAPI, pswrdlbl)}</label>
                            <input type="password" id="password" name="PASSWORD" class="input--login" />
                        </div>
                        <button class="button--blue milli float-right">${xss:encodeForHTML(xssAPI, btnlbl)}</button>

                    </fieldset>
                    <input type="hidden" name="IDToken0" value="">
                    <input type="hidden" name="SMENC" value="ISO-8859-1">
                    <input type="hidden" name="SMLOCALE" value="en-us">
                    <input type="hidden" name="target" value="/bin/uhc/myuhcauthenticationhandler">
                    <input type="hidden" name="theme" value="myuhc">
                </form>

提前致谢

1 个答案:

答案 0 :(得分:0)

如下所示为您的servlet命名或任何唯一的[在工作区中搜索此名称应该是唯一的]

@SlingServlet(
        paths={"/bin/myuhc/authenticationhandler"},
        methods = {"POST","GET"},
        metatype=true,
        name = "authentication handler servlet" // give name in servlet and build the project
        )

再次清洁安装快照jar后

转到localhost:/ system / console / components(你可以直接进入felix控制台(系统/控制台/捆绑) - &gt; OSGI - &gt;组件)

ctrl + f搜索名称(在我们的示例中是“身份验证处理程序servlet”) 在那里你会找到servlet的路径。检查以下

Implementation Class = LoginAuthenticationHandlerServlet 
sling.servlet.paths = [/bin/myuhc/authenticationhandler]

实现类和路径应该与您的servlet匹配。如果路径不匹配,则表示正被命中的servlet url属于甚至可能不存在的其他servlet。另外,仔细检查名称在工作区中应该是唯一的。

希望有所帮助。