SpringSecurity兌現多登錄成功頁面和登錄成功返回被攔截界面


SpringSecurity實現多登錄成功頁面和登錄成功返回被攔截界面

使用SrpingSceurity作為認證和授權的安全框架可以省下很多基礎工作.

具體可以參考SpringSecurity,這里不多說了.主要是記錄一下使用中碰到的問題.

問題1

項目有不同客戶端需要不同的返回界面,比如Android的登錄返回json格式數據.網頁登錄跳轉到登錄成功頁面.

SpringSecurity的默認配置是做不到這點的.以下是配置登錄成功頁面的地方.

<s:form-login login-page="/login.action" default-target-url="/loginsuccess.jsp" authentication-failure-url="/login.action?error=true" /> 

 這里如果loginsuccess.jsp頁面是登錄成功頁,那么Android的登錄就不好返回json格式了.

解決方法

使用AuthenticationSuccessHandler

----------------示例見下----------------

1.定制自己的AuthenticationSuccessHandler類,實現AuthenticationSuccessHandler接口

 

package com.gt.util;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

public class MyAuthenticationSuccessHandler implements
        AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
            HttpServletResponse response, Authentication auth)
            throws IOException, ServletException {        
        String f = request.getParameter("f");
        if (StringUtils.isNotEmpty(f)) {
            if(f.equals("android")){
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write("登錄成功"+LoginUserUtil.getUser());
            }
            
        }else{
            
            request.getRequestDispatcher("/account/user.exp").forward(request, response);
                        
        }

    }

}

2.登錄頁面中指定f參數.只是示例,可以自己根據業務定制.

3.修改配置文件

增加authentication-success-handler-ref="expaiSuccessHandler"

去掉default-target-url="/loginsuccess.jsp"

<s:form-login login-page="/login.exp" authentication-success-handler-ref="expaiSuccessHandler" authentication-failure-url="/login.exp?error=true" />

 官方文檔介紹

 Attribute : authentication-success-handler-ref

Reference to an AuthenticationSuccessHandler bean which should be used to handle a successful 

 authentication request. Should not be used in combination with default-target-url (or always-use-

 default-target-url) as the implementation should always deal with navigation to the subsequent 

 destination

4.修改配置文件,增加bean定義

<bean id="expaiSuccessHandler" class="com.gt.util.MyAuthenticationSuccessHandler"></bean>

---------------------------問題1end--------------------- 

 問題2

登錄后返回攔截前的界面

思路

在攔截后,進入登錄頁面前,把被攔截地址放入session中.登錄成功從session取出被攔截地址並且跳轉.

-------------代碼示例-----------

1.增加MyLoginUrlAuthenticationEntryPoint 繼承 LoginUrlAuthenticationEntryPoint

package com.gt.util;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
import org.springframework.security.web.util.RedirectUrlBuilder;

public class MyLoginUrlAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {

    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
            throws IOException, ServletException {
        String returnUrl = buildHttpReturnUrlForRequest(request);
        request.getSession().setAttribute("ru", returnUrl);
        super.commence(request, response, authException);
    }

    protected String buildHttpReturnUrlForRequest(HttpServletRequest request)
            throws IOException, ServletException {


        RedirectUrlBuilder urlBuilder = new RedirectUrlBuilder();
        urlBuilder.setScheme("http");
        urlBuilder.setServerName(request.getServerName());
        urlBuilder.setPort(request.getServerPort());
        urlBuilder.setContextPath(request.getContextPath());
        urlBuilder.setServletPath(request.getServletPath());
        urlBuilder.setPathInfo(request.getPathInfo());
        urlBuilder.setQuery(request.getQueryString());

        return urlBuilder.getUrl();
    }

}

2.修改配置文件,增加引用

<s:http auto-config="true" use-expressions="true" entry-point-ref="loginUrlAuthenticationEntryPoint"> 
<bean id="loginUrlAuthenticationEntryPoint" class="com.gt.util.MyLoginUrlAuthenticationEntryPoint">
        <property name="useForward" value="true" />
        <property name="loginFormUrl" value="/login.exp" />
</bean>

3.修改MyAuthenticationSuccessHandler,增加獲取被攔截地址並且跳轉代碼

 

package com.gt.util;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
            HttpServletResponse response, Authentication auth)
            throws IOException, ServletException {        
        String f = request.getParameter("f");
        if (StringUtils.isNotEmpty(f)) {
            if(f.equals("android")){
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write("登錄成功"+LoginUserUtil.getUser());
            }
            
        }else{
            String ru = (String)request.getSession().getAttribute("ru");
            request.getSession().removeAttribute("ru");
            if(StringUtils.isNotEmpty(ru)){
                response.sendRedirect(ru);
                //request.getRequestDispatcher(ru).forward(request, response);
            }else{
                request.getRequestDispatcher("/account/user.exp").forward(request, response);
            }
            
        }

    }

}

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM