Spring Security 3的一些体会

    之前,项目中用到了Spring Security 3,以前没用过的,用过之后,感觉到框架用起来真的很简单,为什么说简单呢?因为只要你看了官方的example之后就可以用,如果遇到不熟悉的也可以翻翻文档解决问题。但是如果为了长远发展,带来的负面效应也是很大的,如果用一个框架不求甚解,只是为了框架而去用框架,后患无穷啊~~因为有可能你只用到框架的一小部分内容,却引入了庞大的框架,如果你想改变一些东西,却发现和框架起了冲突,多么可笑。。。
    言归正传,讲讲我的辛酸历程吧~不过也算得上是一种收获。
    项目中用了Spring Security 3 (简称SS3)之后,其实项目中并没用到那么详细的权限控制,我感觉Spring Security 3的好处之一就是权限控制可以非常的细化。可以说只是用到了登录的控制。但是现在用到了这么一个需求,就是单点登录,从另外一个系统直接可以登入到这个系统,用户数据可以是同步的,初步的想法就是一个URL加上参数,也就是用户账号密码了,加密出来,然后用来登录。当然安全等级不是很高,这样已经满足了需求,有兴趣了解单点登录的朋友可以去看看淘宝,登入支付宝的时候就是单点登录,很好的一个例子,不过人家的安全等级较高。继续回到问题上来,本来以为这么简单就解决了需求,然后配置之后,但是却发现该怎么登进来呢,我有了账号密码,该怎么直接跳到这个系统?首先想到的就是直接跳转到一个action里,取出用户数据放到Session里(当时还仔细看SS3),太天真了。结果通不过SS3的身份验证,后来仔细看了看,原来有一个过滤器,也就是下边的:
<s:intercept-url pattern="/user/**" access="isAuthenticated()" />

   其它的页面基本上都有这个过滤,所以是通不过的,原来有一个接口:
       
public interface Authentication extends Principal, Serializable {
     Collection<GrantedAuthority> getAuthorities();
     Object getCredentials();
     Object getDetails();
     Object getPrincipal();
     boolean isAuthenticated();
     void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
}

存放的是用户经过SS3身份验证后一些信息。而isAuthenticated();指的是是否通过认证。于是我就想,是不是取到Authentication之后改一下isAuthenticated();就可以通过认证了,我试了试不行。想必用过SS3的朋友都知道这个j_spring_security_check,必然会说为什么不直接用这个在后缀上加上用户信息呢,这个就是SS3开放的登录借口,接受用户名密码的,格式都是j_username类型的。不是不想用,关键是不行,为什么不行呢?因为之前我试了在上边加用户名密码就是通不过认证,我也很纳闷,一样的表单的,后来看了源码发现了原来这个SS3过滤的URL只接受POST请求的。接下来我们一步步解开SS3登录的过程吧。
     首先看SS3配置文件中登录的配置吧
<s:form-login login-page="/login.jsp" default-target-url="/index.jsp" always-use-default-target="true" authentication-failure-url="/login.jsp?error=1" />

首先这里边其实有些默认的,比如login-processing-url=""这个参数其实就参数内容就是默认j_spring_security_check,这样的话,SS3就会自动给他一拦截器,当连接是j_spring_security_check的时候,就会调用
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.security.web.authentication;


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

import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.util.TextEscapeUtils;
import org.springframework.util.Assert;


/**
 * Processes an authentication form submission. Called {@code AuthenticationProcessingFilter} prior to Spring Security
 * 3.0.
 * <p>
 * Login forms must present two parameters to this filter: a username and
 * password. The default parameter names to use are contained in the
 * static fields {@link #SPRING_SECURITY_FORM_USERNAME_KEY} and {@link #SPRING_SECURITY_FORM_PASSWORD_KEY}.
 * The parameter names can also be changed by setting the {@code usernameParameter} and {@code passwordParameter}
 * properties.
 * <p>
 * This filter by default responds to the URL {@code /j_spring_security_check}.
 *
 * @author Ben Alex
 * @author Colin Sampaleanu
 * @author Luke Taylor
 * @since 3.0
 */
public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
    //~ Static fields/initializers =====================================================================================

    public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "j_username";
    public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "j_password";
    public static final String SPRING_SECURITY_LAST_USERNAME_KEY = "SPRING_SECURITY_LAST_USERNAME";

    private String usernameParameter = SPRING_SECURITY_FORM_USERNAME_KEY;
    private String passwordParameter = SPRING_SECURITY_FORM_PASSWORD_KEY;
    private boolean postOnly = true;

    //~ Constructors ===================================================================================================

    public UsernamePasswordAuthenticationFilter() {
        super("/j_spring_security_check");
    }

    //~ Methods ========================================================================================================

    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        if (postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        }

        String username = obtainUsername(request);
        String password = obtainPassword(request);

        if (username == null) {
            username = "";
        }

        if (password == null) {
            password = "";
        }

        username = username.trim();

        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);

        // Place the last username attempted into HttpSession for views
        HttpSession session = request.getSession(false);

        if (session != null || getAllowSessionCreation()) {
            request.getSession().setAttribute(SPRING_SECURITY_LAST_USERNAME_KEY, TextEscapeUtils.escapeEntities(username));
        }

        // Allow subclasses to set the "details" property
        setDetails(request, authRequest);

        return this.getAuthenticationManager().authenticate(authRequest);
    }

    /**
     * Enables subclasses to override the composition of the password, such as by including additional values
     * and a separator.<p>This might be used for example if a postcode/zipcode was required in addition to the
     * password. A delimiter such as a pipe (|) should be used to separate the password and extended value(s). The
     * <code>AuthenticationDao</code> will need to generate the expected password in a corresponding manner.</p>
     *
     * @param request so that request attributes can be retrieved
     *
     * @return the password that will be presented in the <code>Authentication</code> request token to the
     *         <code>AuthenticationManager</code>
     */
    protected String obtainPassword(HttpServletRequest request) {
        return request.getParameter(passwordParameter);
    }

    /**
     * Enables subclasses to override the composition of the username, such as by including additional values
     * and a separator.
     *
     * @param request so that request attributes can be retrieved
     *
     * @return the username that will be presented in the <code>Authentication</code> request token to the
     *         <code>AuthenticationManager</code>
     */
    protected String obtainUsername(HttpServletRequest request) {
        return request.getParameter(usernameParameter);
    }

    /**
     * Provided so that subclasses may configure what is put into the authentication request's details
     * property.
     *
     * @param request that an authentication request is being created for
     * @param authRequest the authentication request object that should have its details set
     */
    protected void setDetails(HttpServletRequest request, UsernamePasswordAuthenticationToken authRequest) {
        authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
    }

    /**
     * Sets the parameter name which will be used to obtain the username from the login request.
     *
     * @param usernameParameter the parameter name. Defaults to "j_username".
     */
    public void setUsernameParameter(String usernameParameter) {
        Assert.hasText(usernameParameter, "Username parameter must not be empty or null");
        this.usernameParameter = usernameParameter;
    }

    /**
     * Sets the parameter name which will be used to obtain the password from the login request..
     *
     * @param passwordParameter the parameter name. Defaults to "j_password".
     */
    public void setPasswordParameter(String passwordParameter) {
        Assert.hasText(passwordParameter, "Password parameter must not be empty or null");
        this.passwordParameter = passwordParameter;
    }

    /**
     * Defines whether only HTTP POST requests will be allowed by this filter.
     * If set to true, and an authentication request is received which is not a POST request, an exception will
     * be raised immediately and authentication will not be attempted. The <tt>unsuccessfulAuthentication()</tt> method
     * will be called as if handling a failed authentication.
     * <p>
     * Defaults to <tt>true</tt> but may be overridden by subclasses.
     */
    public void setPostOnly(boolean postOnly) {
        this.postOnly = postOnly;
    }

    public final String getUsernameParameter() {
        return usernameParameter;
    }

    public final String getPasswordParameter() {
        return passwordParameter;
    }
}


在attemptAuthentication(HttpServletRequest request, HttpServletResponse response)中我们可以看到,此方法只接受POST请求的,这个方法的主要作用就是取出用户名密码,然后根据SS3配置文件中的authenticationManager获取用户的信息,具体细节不在赘述,相信用过的朋友都知道他取的过程。
<s:authentication-manager alias="authenticationManager">
		<s:authentication-provider user-service-ref="userDetailsService">
			<s:password-encoder hash="plaintext" />
		</s:authentication-provider>
	</s:authentication-manager>

    而这个类的父类就是控制登录是否成功或失败的AbstractAuthenticationProcessingFilter。他主要控制登录是否成功并且控制成功后的跳转URL。下边是这个类的过滤器过滤内容:
 
  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        if (!requiresAuthentication(request, response)) {
            chain.doFilter(request, response);

            return;
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Request is to process authentication");
        }

        Authentication authResult;

        try {
            authResult = attemptAuthentication(request, response);
            if (authResult == null) {
                // return immediately as subclass has indicated that it hasn't completed authentication
                return;
            }
            sessionStrategy.onAuthentication(authResult, request, response);
        }
        catch (AuthenticationException failed) {
            // Authentication failed
            unsuccessfulAuthentication(request, response, failed);

            return;
        }

        // Authentication success
        if (continueChainBeforeSuccessfulAuthentication) {
            chain.doFilter(request, response);
        }

        successfulAuthentication(request, response, authResult);
    }

我们可以看到它取出了用户的数据然后放在了Authentication中了。如果成功则调用 successfulAuthentication(request, response, authResult);这个方法,失败则调用unsuccessfulAuthentication(request, response, failed);
    不再过多的贴代码了,接着说的就是successfulAuthentication(request, response, authResult);这个方法主要做的就是首先查看是否勾选了记住密码选项然后跳转页面也就是目标页,当然其中还有很多机制,比如没条用一个URL就会经国LogoutFilter判断URL了,比如注入了很多Bean,再比如用了大量的回调方法,比如对角色权限等等的细分。很多很多。但是只看了两三天的源码,也只是懂了点SS3的基本原理以及一些内部的实现机制,还差好多。以后继续再看看,再跟大家分享咯。。

最后想说的就是框架其实只是用来快速开发用的,很多框架基本上都封装了实现的细节,我们只知道怎么用是远远不够的,那样干的只是体力活,更多的我们应该去知道它为什么这么用,怎么实现的,然后我们可以不用框架,也可以写出很好的代码。其实说白了,我们最需要掌握的就是解决问题的能里,能不是去跟别人说我会用什么什么框架之类的!

猜你喜欢

转载自jclick.iteye.com/blog/1179165