spring - 将 Null 传递给 Spring Security UserDetailsS​​ervice

标签 spring spring-mvc spring-security

我正在尝试使用 spring security 将登录添加到我的 webapp。

这是我第一次尝试使用纯代码配置添加 spring-security 3.2(我之前使用 xml 配置和标准 UserDetailsS​​ervice 实现多次使用它)。

我还看到了关于迁移到 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 代码,似乎 obtainUsername(request) 调用只是从请求中获取 null。

谁能告诉我哪里可能出了问题,或者从哪里开始寻找合适的地方? spring 在请求对象中在哪里设置用户名/密码?


更新

我也尝试过只更改表单的操作 url 并发布到普通的 MVC Controller - 调试,发布的数据存在并且在 parameterMap 中是正确的 - 所以它似乎是安全链中的东西从请求中删除我发布的数据?

最佳答案

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

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

The username must be present as the HTTP parameter named username

The password must be present as the HTTP parameter named password

尝试一下,看看它是否有效?

关于spring - 将 Null 传递给 Spring Security UserDetailsS​​ervice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19846270/

相关文章:

spring-boot - keycloak openid single log out with spring boot

java - JWT Spring - 基于用户的访问

session - GRAILS:如何通过Spring Security核心插件获取当前登录用户的数量?

java - Spring aop切入点表达式跨其他项目不适用

java - 在jsp中访问request属性(Spring 3 mvc)

java - Spring集成测试并将字段设置为 Controller

spring - BootRun 无法启动?

java - Spring MVC - Eclipse 警告 - 验证消息

java - 使用 rest 调用更新 xml 文件路径

Spring 4.2.4(不使用 spring boot)+ EhCache 3 + Hibernate 4.2.1