springboot - spring security 表单登录疑惑

查看:327
本文介绍了springboot - spring security 表单登录疑惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

使用spring boot 配合 spring security做权限验证登录的时候出现了2个疑惑。

  1. 登录的表单中的两个input标签(账号和密码)的name标签可以随意填吗?后台是怎样的得到name值的。

  2. 登录的时候发现用户账户大小写不敏感。

这是后台配置文件:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.authorizeRequests()
                .antMatchers("/css/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login.html")
                .defaultSuccessUrl("/index.html",true)
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/user/logout")
                .invalidateHttpSession(true)
                .clearAuthentication(true)
                .logoutSuccessUrl("/login.html").permitAll();
    }

    @Autowired
    public void configGlobal(AuthenticationManagerBuilder auth) throws Exception{
        //这里是怎样获取表单字段的?大小写不敏感?
        auth.inMemoryAuthentication().withUser("user").password("1").roles("USER");
        auth.inMemoryAuthentication().withUser("admin").password("1").roles("ADMIN");
    }

}

这是前端登录页面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <meta charset="UTF-8"/>
    <title>登录</title>
    <link rel="stylesheet" type="text/css" th:href="@{css/login.css}" />
</head>
<body>
    <div class="login-div">
        <form class="login-form" th:action="@{/login.html}" method="post">
            <label for="account"> 用户:</label>
            <input id="account" type="text" name="username" /> <br/>
            <label for="password">密码:</label>
            <input id="password" type="password" name="password" /> <br/>
            <button type="submit">登&nbsp;录</button>
        </form>
    </div>
</body>
</html>

请问这是为什么呢?

解决方案

  1. 在 spring security 4.x 中,name不能随便取名

  2. 列表项目

    内存中存储的用户名是会全部转小写的,判断时也是会转为全小写的,参考源码:

    前台传到后台时是没有忽略大小写的

这篇关于springboot - spring security 表单登录疑惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆