angular - 如何在 Jhipster(spring boot + angular)应用程序上设置上下文路径

标签 angular spring-boot jhipster contextpath

我生成了一个 Jhipster 项目经典的 Spring Auth + Angular 客户端应用程序。

我只需要在 Jhipster 应用程序上设置自定义上下文路径“网关”

在服务器上,我刚刚在文件“src\main\resources\config\application-dev.yml”上设置:

.....
server:
  port: 8080
  servlet:
     context-path: /gateway
...

在 Angular 客户端上我不明白如何设置它。

在“webpack\webpack.common.js”文件中设置:

....
new webpack.DefinePlugin({
    'process.env': {
        NODE_ENV: `'${options.env}'`,
        BUILD_TIMESTAMP: `'${new Date().getTime()}'`,
        // APP_VERSION is passed as an environment variable from the Gradle / Maven build tasks.
        VERSION: `'${process.env.hasOwnProperty('APP_VERSION') ? process.env.APP_VERSION : 'DEV'}'`,
        DEBUG_INFO_ENABLED: options.env === 'development',
        // The root URL for API calls, ending with a '/' - for example: `"https://www.jhipster.tech:8081/myservice/"`.
        // If this URL is left empty (""), then it will be relative to the current context.
        // If you use an API server, in `prod` mode, you will need to enable CORS
        // (see the `jhipster.cors` common JHipster property in the `application-*.yml` configurations)
        SERVER_API_URL: `'http://localhost:8080/gateway/'`
    }
}),
......        
new BaseHrefWebpackPlugin({ baseHref: '/gateway' })
....

但是不行,总是登录失败。 我还想念其他东西吗?

Gaël Marziou 评论更新

似乎来自客户端“http://localhost:9000/gateway/”的登录无法登录到服务“http://localhost:8080/gateway/api/authentication”,代码为 403,服务器上出现一些 csfr 异常:

已解决 [org.springframework.security.web.csrf.MissingCsrfTokenException:无法验证提供的 CSRF token ,因为未找到您的 session 。]

所以这可能是 spring boot 的安全配置上的一些错误:

    @Override
    public void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        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())
            .permitAll()
        .and()
            .logout()
            .logoutUrl("/api/logout")
            .logoutSuccessHandler(ajaxLogoutSuccessHandler())
            .permitAll()
        .and()
            .headers()
            .contentSecurityPolicy("default-src 'self'; frame-src 'self' data:; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://storage.googleapis.com; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:")
        .and()
            .referrerPolicy(ReferrerPolicyHeaderWriter.ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN)
        .and()
            .featurePolicy("geolocation 'none'; midi 'none'; sync-xhr 'none'; microphone 'none'; camera 'none'; magnetometer 'none'; gyroscope 'none'; speaker 'none'; fullscreen 'self'; payment 'none'")
        .and()
            .frameOptions()
            .deny()
        .and()
            .authorizeRequests()
            .antMatchers("/**/api/authenticate").permitAll()
            .antMatchers("/**/api/register").permitAll()
            .antMatchers("/**/api/activate").permitAll()
            .antMatchers("/**/api/account/reset-password/init").permitAll()
            .antMatchers("/**/api/account/reset-password/finish").permitAll()
            .antMatchers("/**/api/**").authenticated()
            .antMatchers("/**/management/health").permitAll()
            .antMatchers("/**/management/info").permitAll()
            .antMatchers("/**/management/prometheus").permitAll()
            .antMatchers("/**/management/**").hasAuthority(AuthoritiesConstants.ADMIN);
        // @formatter:on
    }

最佳答案

我找到了以下 5 个地方。

webpack.common.js

new BaseHrefWebpackPlugin({ baseHref: '/gateway/' })

webpack.dev.js

proxy: [{
    context: [
        '/gateway/api',
        '/gateway/services',
       ...

应用程序上下文.ts

export const SERVER_API_URL = process.env.SERVER_API_URL + "/gateway/";

资源\配置\应用-dev.yml

.....
server:
  port: 8080
  servlet:
     context-path: /gateway

index.html

<base href="/gateway/" />

关于angular - 如何在 Jhipster(spring boot + angular)应用程序上设置上下文路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59843787/

相关文章:

angular - 将函数作为 arg 传递时调用管道 Pipe angular2

java - 使用 JaCoCo 和 spring-boot-maven-plugin 生成代码覆盖率

spring-boot - 没有 OAuth 的 Spring 安全 JWT

spring-boot - 插件 [id : 'org.springframework.boot' , version: '2.2.4.RELEASE' ] 未在以下任何来源中找到:

java - JHipster 应用程序中的 Redis 集成

java - JHipster - Hibernate 2nd 缓存/ehcache 尝试从 JHipster 生成的项目中弹出 JHipster 的问题

javascript - 如何让 Angular 前端等待快速 REST-Call 响应

Java REST-ful 服务器 + Angular 5 客户端 POST PUT 语法

java - 我无法安装generator-jhipster-uml来生成实体

angular - 如何以 Angular 访问 ElementRef.nativeElement 的嵌套元素