Angular 弹簧安全和角度7。登录后请求

Angular 弹簧安全和角度7。登录后请求,angular,spring-boot,frontend,angular7,Angular,Spring Boot,Frontend,Angular7,我想登录到我的后端appSpring启动和安全,但我在从Angular应用程序登录时遇到问题。当我向邮递员提出请求时,一切正常。 我有自己的SimpleRuthenticationFailureHandler实现,当我调试它时,我看到Angular的请求中没有任何参数。我通过调用System.out.printlnrequest.getParameterusername;在AuthenticationFailure方法中对此进行检查;。 当我向邮递员请求时,我看到了登录,当然我设置了错误的登录

我想登录到我的后端appSpring启动和安全,但我在从Angular应用程序登录时遇到问题。当我向邮递员提出请求时,一切正常。 我有自己的SimpleRuthenticationFailureHandler实现,当我调试它时,我看到Angular的请求中没有任何参数。我通过调用System.out.printlnrequest.getParameterusername;在AuthenticationFailure方法中对此进行检查;。 当我向邮递员请求时,我看到了登录,当然我设置了错误的登录

这是我来自前端身份验证服务的代码

export class SecurityService {

 private baseUrl = environment.baseUrl;

 constructor(private http: HttpClient){

 }

login(){
console.log(this.baseUrl);

let username = 'user';
let password = 'userPass';

let body = JSON.stringify({ username: username, password: password })
let httpParams = new HttpParams();
httpParams = httpParams.append('username', username).append('password', password);
let headers = new HttpHeaders({ 'Content-Type': 'application/json' });


this.http.post<any>(this.baseUrl + "login", {body, headers})
  .subscribe(value => console.log(value));

}

}

您可以这样做,它将完美地指导您使用Spring安全性和Angular进行身份验证。我也在我的项目中使用了这个。我的答案有点晚了

如果可以,我建议放弃loginForm路径,使用更安全的Oauth2/Oidc,可以是无状态的、面向云的或基本的身份验证!。如果您选择basicauth,请遵循spring官方教程,一切都会顺利进行

如果您必须维护LoginForm,我们发现了两种传递数据的方式:

第一个残酷的

第二个干净光滑


在没有看到安全配置的情况下,听起来好像您将所有端点都设置为需要身份验证。如果是这种情况,您需要向所有流量公开/login端点。您不需要向请求传递参数。为什么您希望它们可以在服务器上使用?@TheHeadlush您可以看到我的配置,我认为这是可以的,因为它可以从postman处工作。@NormundsKalnberzins我不确定如何传递登录数据。作为一个身体,params还是什么?我做过很多组合,但现在我不知道如何正确组合。
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private RestAuthenticationEntryPoint restAuthenticationEntryPoint;

@Autowired
private SuccessHandler successHandler;

@Autowired
private FailureLoginHandler failureLoginHandler;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
            .withUser("admin").password(passwordEncoder().encode("adminPass")).roles("ADMIN")
            .and()
            .withUser("user").password(passwordEncoder().encode("userPass")).roles("USER");

}

@Bean
public PasswordEncoder passwordEncoder(){
    return new BCryptPasswordEncoder();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .csrf().disable()
            .exceptionHandling()
            .authenticationEntryPoint(restAuthenticationEntryPoint)
            .and()
            .authorizeRequests()
            .antMatchers("/api/foo").authenticated()
            .antMatchers("api/admin/**").hasRole("ADMIN")
            .antMatchers("/login").permitAll()
            .and()
            .formLogin()
            .successHandler(successHandler)
            .failureHandler(failureLoginHandler)
            .and()
            .logout();
}


}
return this.http.post<any>(<my_login_url>, {}, {
    params: new HttpParams({
      fromObject: {
        username: form.username,
        password: form.passowrd
        submit: 'login'
      }
    })
  })
const loginForm: FormData = new FormData();
loginForm.append('username', form.username);
loginForm.append('password', form.password);
loginForm.append('submit', 'login');
return this.http.post(<my_login_url>, loginForm)