将spring安全性与Ajax调用集成

将spring安全性与Ajax调用集成,ajax,jsf,spring-security,primefaces,Ajax,Jsf,Spring Security,Primefaces,所以我有一个web应用程序,它使用jsf和primefaces进行前端演示 我们将Spring安全性用于登录机制,并定义了并发性 <session-management session-fixation-protection="newSession"> <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" expired-url="multipleLogins.xhtml" /> &

所以我有一个web应用程序,它使用jsf和primefaces进行前端演示

我们将Spring安全性用于登录机制,并定义了并发性

<session-management session-fixation-protection="newSession">
<concurrency-control max-sessions="1" error-if-maximum-exceeded="true" 
expired-url="multipleLogins.xhtml" />
</session-management>

问题似乎在于,当用户有两个来自不同浏览器的登录时,某些按钮执行的ajax操作不会触发重定向。似乎只有提交表单或重定向到页面本身的按钮才能识别多次登录操作

例如,此按钮

<p:commandButton id="retrieve" value="#{msgs.BUTTON_RETRIEVE}"
action="#{removeContactForm.retrieve}" update="@form"/>

如果有多个登录,则从web服务检索内容并将其显示在页面上不会触发重定向

<p:commandButton id="remove" value="#{msgs.BUTTON_REMOVE}"
action="/pages/confirm/confirmRemove.xhtml" ajax="false" process="@this"          
immediate="true" rendered="#{!empty removeContactManager and   
removeContactManager.contactRolesSuccessful}" />

但是,此按钮将被禁用(因为它重定向到另一页)


有人知道如何让webapp将这些ajax调用注册为事件,而不将所有基于ajax的内容粘贴到新页面吗?

我使用了Ben Simpson编写的JSFDirectStrategy,在会话过期时使用会话管理过滤器重定向到会话过期的url。来源可以找到。 我认为可以在这里应用相同的方法,但我们需要删除名称空间配置,并添加一些bean,如下所示:

<http>
  <custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />
  <custom-filter position="FORM_LOGIN_FILTER" ref="myAuthFilter" />
  <session-management session-authentication-strategy-ref="sas"/>
</http>

<beans:bean id="concurrencyFilter"
   class="org.springframework.security.web.session.ConcurrentSessionFilter">
  <beans:constructor-arg name="sessionRegistry" ref="sessionRegistry" />
  <beans:constructor-arg name="expiredUrl" value="/multipleLogins.xhtml" />
  <!-- this permits redirection to session timeout page from javascript/ajax or http -->
  <beans:property name="redirectStrategy" ref="jsfRedirectStrategy" />
</beans:bean>

<beans:bean id="myAuthFilter" class=
   "org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
  <beans:property name="sessionAuthenticationStrategy" ref="sas" />
  <beans:property name="authenticationManager" ref="authenticationManager" />
</beans:bean>

<beans:bean id="sas" class=
 "org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">
  <beans:constructor-arg name="sessionRegistry" ref="sessionRegistry" />
  <beans:property name="maximumSessions" value="1" />
  <beans:property name="alwaysCreateSession" value="true" />
  <beans:property name="exceptionIfMaximumExceeded" value="true" />
</beans:bean>

<beans:bean id="jsfRedirectStrategy" class="com.examples.JsfRedirectStrategy"/>
<beans:bean id="sessionRegistry"
    class="org.springframework.security.core.session.SessionRegistryImpl" />

现在,您可以检查请求是否是ajax请求,然后在JSFDirectStrategy类中发送如下重定向: 下面是从ICEfaces教程复制的代码

/**
 * This class represents an extension to the way DefaultRedirectStrategy works.
 * This class takes into account if the incoming request causing action by Spring Security
 * requires a "partail-response" xml redirect instead of a response.sendRedirect().
 *
 * @author Ben Simpson ben.simpson@icesoft.com
 */
public class JsfRedirectStrategy implements RedirectStrategy {

    protected final Log logger = LogFactory.getLog(getClass());

    private boolean contextRelative;


    /**
     * Redirects the response to the supplied URL.
     * <p>
     * If <tt>contextRelative</tt> is set, the redirect value will be the value after the request context path. Note
     * that this will result in the loss of protocol information (HTTP or HTTPS), so will cause problems if a
     * redirect is being performed to change to HTTPS, for example.
     */
    public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
        String redirectUrl = calculateRedirectUrl(request.getContextPath(), url);
        redirectUrl = response.encodeRedirectURL(redirectUrl);

        if (logger.isDebugEnabled()) {
            logger.debug("Redirecting to '" + redirectUrl + "'");
        }

        //we should redirect using ajax response if the case warrants
        boolean ajaxRedirect = request.getHeader("faces-request") != null
                && request.getHeader("faces-request").toLowerCase().indexOf("ajax") > -1;

        if(ajaxRedirect) {
            //javax.faces.context.FacesContext ctxt = javax.faces.context.FacesContext.getCurrentInstance();
            //ctxt.getExternalContext().redirect(redirectUrl);

            String ajaxRedirectXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<partial-response><redirect url=\""+redirectUrl+"\"></redirect></partial-response>";
            response.setContentType("text/xml");
            response.getWriter().write(ajaxRedirectXml);
        } else {
            response.sendRedirect(redirectUrl);
        }


    }

    private String calculateRedirectUrl(String contextPath, String url) {
        if (!UrlUtils.isAbsoluteUrl(url)) {
            if (contextRelative) {
                return url;
            } else {
                return contextPath + url;
            }
        }

        // Full URL, including http(s)://

        if (!contextRelative) {
            return url;
        }

        // Calculate the relative URL from the fully qualified URL, minus the scheme and base context.
        url = url.substring(url.indexOf("://") + 3); // strip off scheme
        url = url.substring(url.indexOf(contextPath) + contextPath.length());

        if (url.length() > 1 && url.charAt(0) == '/') {
            url = url.substring(1);
        }

        return url;
    }

    /**
     * If <tt>true</tt>, causes any redirection URLs to be calculated minus the protocol
     * and context path (defaults to <tt>false</tt>).
     */
    public void setContextRelative(boolean useRelativeContext) {
        this.contextRelative = useRelativeContext;
    }
}
/**
*此类表示DefaultRedirectStrategy工作方式的扩展。
*该类考虑了传入请求是否会导致Spring Security的操作
*需要“partail response”xml重定向,而不是response.sendRedirect()。
*
*@作者本·辛普森·本。simpson@icesoft.com
*/
公共类JSFDirectStrategy实现重定向策略{
受保护的最终日志记录器=LogFactory.getLog(getClass());
私有布尔上下文相对;
/**
*将响应重定向到提供的URL。
*
*如果设置了contextRelative,重定向值将是请求上下文路径之后的值。注意
*这将导致协议信息(HTTP或HTTPS)丢失,因此如果
*例如,正在执行重定向以更改为HTTPS。
*/
public void sendRedirect(HttpServletRequest请求、HttpServletResponse响应、字符串url)引发IOException{
String redirectUrl=calculateRedirectUrl(request.getContextPath(),url);
redirectUrl=response.encodeRedirectURL(重定向URL);
if(logger.isDebugEnabled()){
debug(“重定向到“'+重定向URL+””);
}
//如果情况允许,我们应该使用ajax响应重定向
布尔ajaxRedirect=request.getHeader(“faces request”)!=null
&&request.getHeader(“faces request”).toLowerCase().indexOf(“ajax”)>-1;
如果(ajaxRedirect){
//javax.faces.context.FacesContext ctxt=javax.faces.context.FacesContext.getCurrentInstance();
//ctxt.getExternalContext().redirect(重定向URL);
字符串ajaxRedirectXml=“”+
"";
setContentType(“text/xml”);
response.getWriter().write(ajaxRedirectXml);
}否则{
sendRedirect(重定向URL);
}
}
私有字符串CalculaterDirectURL(字符串上下文路径,字符串url){
如果(!UrlUtils.isAbsoluteUrl(url)){
if(contextRelative){
返回url;
}否则{
返回contextPath+url;
}
}
//完整URL,包括http://
如果(!contextRelative){
返回url;
}
//从完全限定的URL计算相对URL,减去方案和基础上下文。
url=url.substring(url.indexOf(“:/”)+3);//剥离方案
url=url.substring(url.indexOf(contextPath)+contextPath.length());
如果(url.length()>1&&url.charAt(0)=='/')){
url=url.substring(1);
}
返回url;
}
/**
*如果为true,则导致计算任何重定向URL减去协议
*和上下文路径(默认为false)。
*/
public void setContextRelative(布尔UserRelativeContext){
this.contextRelative=useRelativeContext;
}
}

第二个按钮是否重定向到confirmRemove或multipleLogins?如果有多次登录,第二个按钮将正确重定向到“multipleLogins”。第一个不会,因为它是初始化ajax调用,而不是转到新页面。