Spring Security.config - 无法获取登录页面

时间:2016-03-29 17:51:05

标签: java spring spring-mvc spring-security

我有一个webapp,我正在使用Spring Security。我在configure中将Spring Security security.config函数作为:

@Override
protected void configure(HttpSecurity http) throws Exception {

    http
         .csrf().disable()
         .authorizeRequests()
             .antMatchers("/static/**").permitAll()
             .antMatchers("/settings/api/**").permitAll()
             .antMatchers("/api/**").permitAll()
             .anyRequest().authenticated()
         .and()
         .formLogin()
             .loginPage("/login").permitAll()
             .defaultSuccessUrl("/", true);
}

我遇到的问题是,它始终会映射到@RequestMapping(value="/login", method=RequestMethod.GET)而不是@RequestMapping(value="/login", method=RequestMethod.POST)。因此,每当我尝试从Http请求中检索用户名和密码时,它都会显示为null。如何将其映射到POST方法?

我的帖子控制器为

    @RequestMapping(value="/login", method=RequestMethod.POST)
public String login(HttpServletRequest request, HttpServletResponse response) {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    boolean f = false;
    f = customAuthenticationProvider.verifyUser(username,password,request);
    if(f == false) 
        return "loginError";
    else
        return "index";
}

1 个答案:

答案 0 :(得分:0)

使用formlogin(),您不必提供POST控制器,Spring将在UsernamePasswordAuthenticationFilter上处理。您正在配置的只是loginPage(“/ login”),您必须在其中添加一个带有POST操作的表单到“/ login”,并添加用户名和密码输入。

这样的事情:

<form action="login" method="post">
    <div>
        <label for="username">Username:</label>
        <input type="text" class="form-control" id="username" name="username"/>
    </div>
    <div>
        <label for="password">Password:</label>
        <input type="password" class="form-control" id="password" name="password"/>
    </div>
    <button type="submit">Submit</button>
</form>