【问题标题】:How can I pass credentials from Angular2 frontend to Spring backend (Basic-Authentification with Spring Security)如何将凭据从 Angular 前端传递到 Spring 后端(Spring Security 的基本身份验证)
【发布时间】:2017-07-27 13:48:48
【问题描述】:

我正在开发一个使用 spring 作为后端angular2 作为前端的应用程序,后端是安全的(使用 Spring security),当我运行它时,我有默认的登录表单。我想从客户端登录到服务器端,但是当我尝试传递凭据时,浏览器控制台中出现这些错误

配置类

@Configuration
@EnableWebSecurity

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN", "USER").and().withUser("user")
                .password("user").roles("USER");
    }

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

        http
        .csrf().disable()
        .authorizeRequests()
        .antMatchers("/etudiant/list").hasRole("ADMIN")
            .anyRequest().authenticated()
            .and()
        .formLogin()
        .and()
        .httpBasic();
    }

}

web.xml 中的安全过滤器:

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>
        org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

angular2端的登录表单:

<div class="login jumbotron center-block">
   <h1>Login</h1>
   <form role="form" (submit)="login(username.value, password.value)">
   <div class="form-group">
     <label for="username">Username</label>
     <input type="text" #username class="form-control" id="username" placeholder="Username">
   </div>
   <div class="form-group">
     <label for="password">Password</label>
     <input type="password" #password class="form-control" id="password" placeholder="Password">
   </div>
   <button type="submit" class="btn btn-default">Submit</button>
 </form>
 </div>

【问题讨论】:

标签: json spring angular spring-security restful-authentication


【解决方案1】:

我将使用表单调用的以下方法创建一个@Injectable()AuthService。

login(usercreds) {
  const headers = new Headers();
  const creds = 'name=' + usercreds.username + '&password=' + usercreds.password;

  headers.append('Content-Type', 'application/x-www-form-urlencoded');

  return new Promise((resolve) => {
    this.http.post('http://localhost:3333/authenticate', creds, {headers: headers}).subscribe((data) => {
        if (data.json().success) {
          // window.localStorage.setItem('auth_key', data.json().token);
          this.userId = data.json().userId;
          this.isAuthenticated = true;
        }
        resolve(this.isAuthenticated);
     });
  });
}

不要忘记在app.module.ts 文件中将此组件声明为提供者

【讨论】:

  • 我确实喜欢这个,我只是在 post 方法中更改了 url,但它显示了这些错误 [i.stack.imgur.com/dsscG.png](我编辑了我的问题)
  • 好吧,您的网址 http://localhost:8080/studentmanager/login 似乎不存在,这就是您遇到错误 HTTP 404 的原因
  • 这是我运行后端应用程序时默认登录页面的链接,你认为我必须做一个自定义登录页面吗??如果您有任何想法,请提出!我需要建议
  • 我已经在前端应用程序上完成了登录页面,该页面将凭据发布到后端应用程序。在我使用OAuth2 的情况下,它可以正常工作
  • 我认为是春季刊
猜你喜欢
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
  • 2017-09-07
  • 2016-04-30
  • 2011-02-11
  • 2016-03-20
  • 1970-01-01
相关资源
最近更新 更多