2021-07-17

SpringSecurity中,loginProcessingUrl无效,报错404

问题描述:

        使用SpringSecurity,表单登陆,登陆提示404,yml中debu设置为true,在登陆请求时后台日志一行也没有(说明了请求根本就没到后台来,打开浏览器控制台,发现也在发请求)

我的代码

//application.yml配置文件
server:
  port: 8888
  servlet:
    context-path: /api

//maven配置
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>



//后台代码
protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/user/login")
                .defaultSuccessUrl("/test/index").permitAll()//登陆成功之后,跳转路径
                .and().authorizeRequests()
                .antMatchers("/","/test/hello","/user/login").permitAll()
                .anyRequest().authenticated()
                .and().csrf().disable();//关闭csrf防护
    }



//登陆界面
<form action="/user/login" method="post">
    同户名 <input name="username" type="text"><br>
    密码<input name="password" type="text"><br>
    提交<input type="submit">
</form>

解决办法:

        问题出在:

        1、我在application.yml中配置了context-path: /api

        2、form表单中的action配置的是"/user/login"

        3、后台代码配置的地址是.loginProcessingUrl("/user/login")

        错就错在form表单中的action,应该配置成"/api/user/login",其他地方都不用改了。

解决问题的流程:

        登陆报404之后,我尝试过

        1、把form表单中的action和后台代码的loginProcessingUrl都设置成/api/user/login,结果报错

        2、我怀疑是springBoot的版本问题,从2.5.2降到2.2.1.release,还是没用

        3、我怀疑pom是不是缺少springSecurity的相关包,检查后排出

        4、我用postman测试后台是否有提供/user/login登陆服务,

                首先测试请求:localhost:8888/user/login,postman也报错

                然后我再试了下:localhost:8888/api/user/login,尼玛,通了。我就晓得问题在表单上了

        这个问题费了两个小时,记录一下。

备注:

        登陆认证的url确实是SpringSecurity帮我们做好了的,你不要怀疑,loginProcessingUrl里面的url你乱求写,只要相应的和表单中的地址能匹配上,都可以认证,不信你看:

 记住: 

context-path + loginProcessingUrl = 表单action

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值