spring-security-5.0版本之UsernamePasswordFilter

首先,先上代码。

package pn.empire.security.filter;

import java.security.interfaces.RSAPrivateKey;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;

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

import org.apache.commons.lang3.StringUtils;
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.util.Assert;

import pn.empire.security.exception.CodeException;
import pn.empire.security.exception.ForbidIPException;
import pn.empire.util.NetWorkUtil;
import pn.empire.util.RSAUtils;

public class UsernamePasswordFilter
        extends org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter {
    // ~ 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_FORM_REDERICT_KEY = "spring-security-redirect";

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

    private String redirectParameter = SPRING_SECURITY_FORM_REDERICT_KEY;
    // ~ 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());
        }
        if (request == null) {
            throw new AuthenticationServiceException("request is null");
        }
        String username = obtainUsername(request);
        String password = obtainPassword(request);
        String code = request.getParameter("code");
        String sessionCode = (String) request.getSession(true).getAttribute("code");
        request.getSession(true).removeAttribute("code");
        if (sessionCode == null) {
            throw new CodeException("您的验证码已过期,请刷新验证码!");
        }
        // 验证码相关
        if (code == null) {
            throw new CodeException("请输入验证码");
        }
        if (!code.toLowerCase().equals(sessionCode.toLowerCase())) {
            throw new CodeException("请正确输入验证码!");
        }
        ResourceBundle resource = ResourceBundle.getBundle("forbidip");
        String forbidIpsStr = resource.getString("ip.address");
        List<String> forbidIps = new ArrayList<String>();
        if (forbidIpsStr != null && forbidIpsStr.indexOf(",") != -1) {
            String[] ips = forbidIpsStr.split(",");
            forbidIps = Arrays.asList(ips);
        }
        String ip = "";
        ip = NetWorkUtil.getIpAddress(request);
        if (ip.indexOf("非法地址:") != -1) {
            throw new ForbidIPException("非法IP");
        }
        if (forbidIps.size() > 0 && forbidIps.contains(ip)) {
            throw new ForbidIPException("非法IP");
        }
        RSAPrivateKey privateKey = (RSAPrivateKey) request.getSession().getAttribute("privateKey");
        try {
            String str = RSAUtils.decryptByPrivateKey(password, privateKey);
            password = StringUtils.reverse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String redirectUrl = obtainRedercitUrl(request);
        if (username == null) {
            username = "";
        }

        if (password == null) {
            password = "";
        }
        // 自定义回调URL,若存在则放入Session
        if (redirectUrl != null && !"".equals(redirectUrl)) {
            request.getSession().setAttribute("callCustomRediretUrl", redirectUrl);
        }
        username = username.trim();
        // 在这里处理密码或者用户名
        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
        // 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 "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 "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;
    }

    protected String obtainRedercitUrl(HttpServletRequest request) {
        return request.getParameter(redirectParameter);
    }

}

说一下变量:
1.SPRING_SECURITY_FORM_USERNAME_KEY:这个静态常量定义了页面传递到后台中用户名的变量名。
2.SPRING_SECURITY_FORM_PASSWORD_KEY:这个静态常量定义了页面传递到后台中用户密码的变量名。
3.SPRING_SECURITY_FORM_REDERICT_KEY:这个静态常量定义了页面传递到后台中路径的变量名,因为是ajax登录,所以可以通过该变量控制登录成功后跳转至哪里。如果是表单登录,应该可以通过重定向来实现。
核心方法attemptAuthentication:

        if (postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        }

上面这一段代码控制了登录请求只能以post方式进行。

        String code = request.getParameter("code");
        String sessionCode = (String) request.getSession(true).getAttribute("code");
        request.getSession(true).removeAttribute("code");
        if (sessionCode == null) {
            throw new CodeException("您的验证码已过期,请刷新验证码!");
        }
        // 验证码相关
        if (code == null) {
            throw new CodeException("请输入验证码");
        }
        if (!code.toLowerCase().equals(sessionCode.toLowerCase())) {
            throw new CodeException("请正确输入验证码!");
        }

上面代码简单实现了一个验证码校验功能,在访问登录页面时,后台生成验证码并存储到session中,之后取值移除并进行校验,此处抛出了自定义异常CodeException,目的是方便失败后进行异常捕捉,明确错误信息并提示给用户。

        ResourceBundle resource = ResourceBundle.getBundle("forbidip");
        String forbidIpsStr = resource.getString("ip.address");
        List<String> forbidIps = new ArrayList<String>();
        if (forbidIpsStr != null && forbidIpsStr.indexOf(",") != -1) {
            String[] ips = forbidIpsStr.split(",");
            forbidIps = Arrays.asList(ips);
        }
        String ip = "";
        ip = NetWorkUtil.getIpAddress(request);
        if (ip.indexOf("非法地址:") != -1) {
            throw new ForbidIPException("非法IP");
        }
        if (forbidIps.size() > 0 && forbidIps.contains(ip)) {
            throw new ForbidIPException("非法IP");
        }

上面代码是通过读取配置文件进行ip限制,如果是当前ip被禁止则抛出自定义异常。
当然请求的ip是从request中取得,此种获取ip的方法并不可靠,聊胜于无,如果大家有好的方法,欢迎指导。

       RSAPrivateKey privateKey = (RSAPrivateKey)request.getSession().getAttribute("privateKey");
        try {
            String str = RSAUtils.decryptByPrivateKey(password, privateKey);
            password = StringUtils.reverse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

这一段代码是对前台传递过来的密码进行rsa解密,目前公钥私钥都是存储在session 中的,每个会话都是单独生成的,至于此种做法是否更加安全,待验证。

至于redirectUrl,此属性是为了登录成功后配合ajax可以跳转到指定路径。

  setDetails(request, authRequest);
  return this.getAuthenticationManager().authenticate(authRequest);

设置details,这里就是设置org.springframework.security.web.authentication.WebAuthenticationDetails实例到details中
之后会通过UserDetailServiceImpl进行校验。
以上便是UsernamePasswordFilter的主要内容了。
之后会写UserDetailServiceImpl。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,294评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,780评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,001评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,593评论 1 289
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,687评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,679评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,667评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,426评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,872评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,180评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,346评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,019评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,658评论 3 323
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,268评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,495评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,275评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,207评论 2 352

推荐阅读更多精彩内容