首页 > 解决方案 > Spring Boot Security ,基于表单的登录以调用自定义 API 进行身份验证

问题描述

所以想知道这是否可能。我正在尝试使用已经存在的验证用户的发布 API 在 Spring Boot 中验证我的其余 API。

我正在使用基于 fromLogin 的身份验证并尝试调用该 Rest Controller 并通过传递 post 参数来使用该登录 API。在那里,我正在创建 spring 安全上下文
,我试图在登录提交时调用登录的 POST API。身份验证不起作用。

我们可以使用表单登录来实现这一点吗?如果我对 Spring Boot 安全性的理解很新,请告诉我

我的代码有点像这样

控制器

 @RestController
 @RequestMapping("/somemapping")
 public class AuthController {

@RequestMapping(value = "/login", method = RequestMethod.POST)
public UserData authenticateUser(@RequestBody(required = true) UserInfo userInfo, HttpServletRequest request) {
    // get user data goes here 
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
        userdata.getUsername(), userdata.getPassword(), new ArrayList < > ());
    authentication.setDetails(userdata);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    send the info to caller
    return userdata;
}

//安全适配器

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

      http.authorizeRequests()
      .antMatchers("/somemapping/**", "/login*", ).permitAll()
      .anyRequest().authenticated().and()
      .formLogin().loginPage("/")
      .and().authenticationEntryPoint(authenticationPoint);
  }

标签: springspring-bootspring-security

解决方案


推荐阅读