-1

这是我的安全配置文件

    package com.data.topic;


 @EnableWebSecurity
 @ComponentScan(basePackageClasses = CustomUserDetailService.class)
 public class Security extends WebSecurityConfigurerAdapter {

@Autowired
private UserDetailsService userDetailsService;

 @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/css/**", "/index").permitAll()
                .antMatchers("/js/**").permitAll()
                .antMatchers("/node_modules").permitAll()


                .antMatchers("/topic/**").hasRole("user")           
                .and()
            .formLogin().loginPage("/sch_signin")

                .usernameParameter("username")
                .passwordParameter("password")
                .successForwardUrl("/")
                .failureUrl("/login-error")
                .and().csrf().disable();

    }

 @Autowired
 public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {    
     auth.userDetailsService(userDetailsServiceBean());
 } 

 @Override
    public UserDetailsService userDetailsServiceBean() throws Exception {
        return new CustomUserDetailService();
    }

   }

我想知道我应该如何使用angular2发送用户名和密码我在提交时尝试了这个方法

 onSubmit(){

let url="http://localhost:8080/sch_signin";
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });

console.log(this.user);
 this.http.post(url,JSON.stringify(this.user),options);
 console.log('data submited');

 }

我没有收到任何错误,也没有通过身份验证

请帮助我了解spring如何拦截身份验证请求

4

1 回答 1

0

经过一番研究,我得到了解决方案。我以错误的方式发布表单,这是在 Angular2 中发布表单的正确方式。

let url="http://localhost:8080/sch_signin";
let headers = new Headers({'Content-Type':'application/x-www-form-urlencoded'});
let options = new RequestOptions({ headers: headers });

this.http.post(url,body.toString(),options).subscribe((data)=>console.log(data));

首先内容类型应该是application/x-www-form-urlencoded,其次您必须在请求正文中发送数据,以便 Spring Security 可以读取它。

于 2017-02-09T02:37:54.263 回答