Spring Security:在上下文之外访问Angular的URL

时间:2018-04-01 10:59:05

标签: java spring spring-security angular5 jhipster

我已经生成了一个JHipster(4.13.1)应用程序。在后端,我有一个Spring启动应用程序,Spring安全配置如下:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf()
        .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
    .and()
        .addFilterBefore(corsFilter, CsrfFilter.class)
        .exceptionHandling()
        .authenticationEntryPoint(problemSupport)
        .accessDeniedHandler(problemSupport)
    .and()
        .rememberMe()
        .rememberMeServices(rememberMeServices)
        .rememberMeParameter("remember-me")
        .key(jHipsterProperties.getSecurity().getRememberMe().getKey())
    .and()
        .formLogin()
        .loginProcessingUrl("/api/authentication")
        .successHandler(ajaxAuthenticationSuccessHandler())
        .failureHandler(ajaxAuthenticationFailureHandler())
        .usernameParameter("j_username")
        .passwordParameter("j_password")
        .permitAll()
    .and()
        .logout()
        .logoutUrl("/api/logout")
        .logoutSuccessHandler(ajaxLogoutSuccessHandler())
        .permitAll()
    .and()
        .headers()
        .frameOptions()
        .disable()
    .and()
        .authorizeRequests()
        .antMatchers("/api/register").permitAll()
        .antMatchers("/api/activate").permitAll()
        .antMatchers("/api/authenticate").permitAll()
        .antMatchers("/api/account/reset-password/init").permitAll()
        .antMatchers("/api/account/reset-password/finish").permitAll()
        .antMatchers("/api/profile-info").permitAll()
        .antMatchers(org.springframework.http.HttpMethod.OPTIONS, "/api/**").permitAll()
        .antMatchers("/api/**").authenticated()
        .antMatchers("/api/**").fullyAuthenticated()
        .antMatchers("/management/health").permitAll()
        .antMatchers("/management/**").hasAuthority(AuthoritiesConstants.ADMIN)
        .antMatchers("/v2/api-docs/**").permitAll()
        .antMatchers("/swagger-resources/configuration/ui").permitAll()
        .antMatchers("/swagger-ui/index.html").hasAuthority(AuthoritiesConstants.ADMIN);

}

在前端,我有一个Angular(5)客户端,它对Spring启动应用程序进行REST调用。我从Angular登录如下:

login(credentials): Observable<any> {
    const data = 'j_username=' + encodeURIComponent(credentials.username) +
        '&j_password=' + encodeURIComponent(credentials.password) +
        '&remember-me=' + credentials.rememberMe + '&submit=Login';
    const headers = new Headers ({
        'Content-Type': 'application/x-www-form-urlencoded'
    });

    return this.http.post(SERVER_API_URL + 'api/authentication', data, { headers });
}

登录后我可以拨打任何电话,例如&#34; this.http.get(url)&#34;。

现在我有两个按钮。 ButtonOne在上下文中进行调用(例如http://localhost:8080)并且ButtonTwo进行远程REST调用(例如到http://serverx:8080)。我在application.yml

中启用了cors
cors:
    allowed-origins: "*"
    allowed-methods: "*"
    allowed-headers: "*"
    exposed-headers: "Link,X-Total-Count"
    allow-credentials: true
    max-age: 1800

第一个工作(我登录时)和第二个触发身份验证(http 401)。我喜欢重用凭据来访问远程API。在Angular中对上下文之外的URL进行身份验证/访问的正确方法是什么?

我在调用远程Rest API时发现了以下资源:

separating-front-end-and-api

swagger-cli

BTW:当使用JHipster微服务网关/应用程序配置时,这个用例可以工作,因为我可以保留在我自己的上下文中并通过注册表调用rest API。但我想避免注册表,因为应用程序必须独立工作。

0 个答案:

没有答案