Spring Boot Security,基于表单的登录,以调用自定义API进行身份验证

时间:2020-04-14 14:43:52

标签: spring spring-boot spring-security

所以想知道是否有可能。我正在尝试使用已存在的后期API对我的其余API进行身份验证,以验证用户身份。

我正在使用基于fromLogin的身份验证,并尝试通过传递post参数来调用该Rest Controller并使用该登录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);
  }

0 个答案:

没有答案