Null传递给Spring Security UserDetailsS​​ervice


问题内容

我正在尝试使用Spring Security将登录名添加到我的Web应用程序中。

这是我第一次尝试使用纯代码配置添加spring-security 3.2(之前在xml配置和标准UserDetailsService实现中使用过几次)。

我还看到了其他一些类似的问题,这些问题都移到了3.2上,但都与csrf有关-我目前已禁用这些功能,只是为了排除这些更改。

我的配置如下所示:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired private UserDetailsServiceImpl userDetailsServiceImpl;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf()
                .disable()
            .authorizeRequests()
                .antMatchers("/resources/**").permitAll()
                .antMatchers("/dashboard").permitAll()
                .antMatchers("/sign-up").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/")
                .loginProcessingUrl("/loginprocess")
                .failureUrl("/?loginFailure=true")
                .permitAll()
                .and()
            .logout()
                .logoutUrl("/logout")
                .permitAll();
    }

     @Override
     protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
         auth
            .userDetailsService(userDetailsServiceImpl)
            .passwordEncoder(bCryptPasswordEncoder());
     }

     @Bean 
     public BCryptPasswordEncoder bCryptPasswordEncoder(){
         return new BCryptPasswordEncoder();
     }

我的登录表单如下所示:

<form class="form-signin" action="loginprocess" method="POST">
    <h3 class="form-signin-heading">Already Registered?</h3>
    <input type="text" class="form-control" name='j_username' placeholder="User name">
    <input type="password" class="form-control" name='j_password' placeholder="Password"><br/>
    <input class="btn btn-lg btn-primary" name="submit" type="submit" value='Sign in' >
    <a class="btn btn-lg btn-primary" href="#" >Sign up</a>
</form>

最后,在实现UserDetailsS​​ervice的过程中,我尝试登录并进行调试和遍历代码:

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    username = username.toLowerCase();

我看到在这里传递的用户名字符串始终为null-因为所有这些都由底层的spring机制调用,我正在努力查看提交的登录表单中的值正在发生什么。

我已经看过较早,并遍历了包括UsernamePasswordAuthenticationFilter该类的Spring代码,并且似乎generateUsername(request)调用只是从请求中获取null。

任何人都可以建议出什么问题或哪里可以找到一个好地方吗?春天之前,用户名/密码在请求对象中的哪里设置?


更新

我还尝试过仅更改表单的操作URL并将其发布到普通的MVC控制器-调试已发布的数据存在且在parameterMap中正确-
看来这是安全链中正在删除我的已发布内容来自请求的数据?


问题答案:

该文档建议用户名/密码字段必须具有特定名称:-

http://docs.spring.io/spring-security/site/docs/3.2.0.CI-
SNAPSHOT/reference/htmlsingle/#jc

用户名必须作为名为username的HTTP参数存在

密码必须作为名为password的HTTP参数存在

试试看,看看是否可行?