Cristian,
Поскольку ServletFilter всегда будет вызываться до FacesServlet, , вы всегда получите null .
Так что не используйте
private @In FacesContext facesContext;
больше при использовании Servlet Filter.
Решение: ну, это не может быть лучшим решением, но оно решает, что вы хотите сделать
Создайте CustomAuthenticationFilter следующим образом
@Scope(APPLICATION)
@Name("org.jboss.seam.web.authenticationFilter")
@Install(value = false, precedence = BUILT_IN)
@BypassInterceptors
@Filter(within = "org.jboss.seam.web.exceptionFilter")
public class CustomAuthenticationFilter extends org.jboss.seam.web.AbstractFilter {
/**
* Because of some private methods defined in AuthenticationFilter
* do Ctrl + C / Ctrl + V All of source code of AuthenticationFilter
*
* Except authenticate method which is shown bellow
*/
private void authenticate(HttpServletRequest request, final String username) throws ServletException, IOException {
new ContextualHttpServletRequest(request) {
@Override
public void process() throws ServletException, IOException, LoginException {
Identity identity = Identity.instance();
identity.getCredentials().setUsername(username);
try {
identity.preAuthenticate();
/**
* Yes, THE SAME LOGIC performed by authenticate-method must goes here
*/
/**
* Do not use @In-jection here
*
* Use context lookup instead
* For instance, UserService userService = (UserService) Contexts.lookupInStatefulContexts("userService");
*/
identity.postAuthenticate();
} finally {
// Set password to null whether authentication is successful or not
identity.getCredentials.setPassword(null);
}
}
}.run();
}
}
Теперь переопределяет фильтр по умолчанию AuthenticationFilter в /WEB-INF/componets.xml
<web:rewrite-filter view-mapping="/resource/rest/*"/>
<component name="org.jboss.seam.web.authenticationFilter" class="br.com.ar.seam.CustomAuthenticationFilter">
<property name="urlPattern">/resource/rest/*</property>
<property name="authType">basic</property>
</component>
И Чтобы включить restURL, выполните следующие действия:
web.xml
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!--HERE GOES NON-REST INTERCEPTED URL-->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.seam</url-pattern>
</servlet-mapping>
<!--HERE GOES REST INTERCEPTED URL-->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Теперь предположим, что вы хотите вызвать / resource / rest / user , что соответствует сопоставлению сервлета restURL.В /WEB-INF/pages.xml объявляем
<page view-id="<VIEW_ID_GOES_HERE>">
<rewrite pattern="/resource/rest/user"/>
<rewrite pattern="/resource/rest/{userId}"/>
<!--userId comes from pattern shown above-->
<param name="userId" value="#{userService.userId}"/>
</page>
Чтобы избежать FacesServlet перехватывать любые клиентские ресурсы, такие как файлы CSS, JavaScript и изображения, вы можете определить
<servlet>
<servlet-name>Seam Resource Servlet</servlet-name>
<servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Seam Resource Servlet</servlet-name>
<url-pattern>/resource/*</url-pattern>
</servlet-mapping>
Который не применяется жизненный цикл JSF. Убедитесь, что поставил сервлет ресурсов Seam над объявлением FacesServlet.