史上最简单的Spring Security教程(二十四):自定义用户名密码参数名及用户名密码验证路径

 

​无论是表单登录配置器(FormLoginConfigurer),还是用户密码验证Filter(UsernamePasswordAuthenticationFilter),Spring Security 默认的用户名、密码参数名均为 usernamepassword

表单登录配置:

public FormLoginConfigurer() {
    super(new UsernamePasswordAuthenticationFilter(), null);
    usernameParameter("username");
    passwordParameter("password");
}

用户密码验证Filter:

public class UsernamePasswordAuthenticationFilter extends
        AbstractAuthenticationProcessingFilter {
    // ~ Static fields/initializers
    // =====================================================================================
    public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "username";
    public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "password";
    private String usernameParameter = SPRING_SECURITY_FORM_USERNAME_KEY;
    private String passwordParameter = SPRING_SECURITY_FORM_PASSWORD_KEY;

同样的,还有用户密码验证Filter(UsernamePasswordAuthenticationFilter)的拦截路径,即用户名密码验证路径,Spring Security 默认为 /login,且只支持 POST 方式访问。

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

不过,这些并不是不可改变的。下面就来说一下如何改变这些默认配置。

首先就是用户名密码验证路径,非常容易加一项配置即可。

protected void configure(HttpSecurity http) throws Exception {
    http
        .formLogin()
        .loginProcessingUrl("/j_spring_security_check")
        ...... 
}

对应的,需要相应的修改一下页登录面中表单的提交路径,与此呼应。

<form th:action="@{/j_spring_security_check}" method="post" th:method="post" class="mt-1">

然后,就是修改用户名、密码参数名称。

protected void configure(HttpSecurity http) throws Exception {
    http
        .formLogin()
        ......
        .usernameParameter("j_username")
        .passwordParameter("j_password")
        ......
}

对应的,需要相应的修改一下页登录面中表单的用户名、密码控件名称,与此呼应。

<div class="form-group">
  <input type="text" class="form-control" name="j_username" placeholder="用户名">
</div>
<div class="form-group">
  <input type="password" class="form-control" name="j_password" placeholder="密码">
</div>

用过Spring Security 老版本的,比如 3.1.x,其中的 filterProcessingUrl新版本为 loginProcessingUrl)默认为 j_spring_security_check,而用户名、密码参数名则为 j_usernamej_password。同样的,默认登录页地址为 spring_security_check新版本则为 /login。注意,此 /login 为 GET 方式的view路径,而不是 POST 方式的表单提交action /login

一切准备就绪,启动系统,访问系统,正常跳转到登录页。F12查看元素,一切正如我们设置的那样完美呈现。

输入用户名、密码后,正常跳转到系统首页。

改造成功!

其它详细源码,请参考文末源码链接,可自行下载后阅读。

我是银河架构师,十年饮冰,难凉热血,愿历尽千帆,归来仍是少年! 

如果文章对您有帮助,请举起您的小手,轻轻【三连】,这将是笔者持续创作的动力源泉。当然,如果文章有错误,或者您有任何的意见或建议,请留言。感谢您的阅读!

文章不定时更新,可微信搜索「银河架构师」,精彩内容,先睹为快!

 

源码

github

https://github.com/liuminglei/SpringSecurityLearning/tree/master/24

gitee

https://gitee.com/xbd521/SpringSecurityLearning/tree/master/24

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值