【发布时间】:2020-05-07 16:08:42
【问题描述】:
我生成了一个 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/”的登录似乎无法使用代码 403 登录服务“http://localhost:8080/gateway/api/authentication”,因为服务器上出现了一些 csfr 异常:
已解决 [org.springframework.security.web.csrf.MissingCsrfTokenException: 由于未找到您的会话,无法验证提供的 CSRF 令牌。]
所以可能是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
}
【问题讨论】:
-
它是如何失败的?你在浏览器控制台看到了什么?
-
您是否尝试在 baseHref 末尾添加尾随
/?{ baseHref: '/gateway/' } -
找到任何解决方案了吗?
-
如果您使用 tomcat 或类似的 servlet,我发现一个简单的解决方案是将 index.html
转换为
标签: angular spring-boot jhipster contextpath