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>

请问这是为什么呢?

阅读 6.5k
1 个回答
  1. 在 spring security 4.x 中,name不能随便取名
    clipboard.png

  2. 列表项目

    内存中存储的用户名是会全部转小写的,判断时也是会转为全小写的,参考源码:
    clipboard.png
    前台传到后台时是没有忽略大小写的

    clipboard.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题