身份验证在本地主机上工作,不适用于生产
Authentification working on localhost and doesn't work on production
当我在本地测试我的应用程序时,它可以工作,但是当我将它部署到产品环境时,它就不再工作了。我收到 401 错误。
应用程序部署在远程服务器上时,是否有特定的配置?
我用spring boot + angular 6 + msal v1.
重现步骤
在 Azure 上配置应用程序以 spring 启动 api
在 Azure 上为使用 msal 版本 1 的 angular 6 ap 配置应用程序
Exemple link
失败给出的日志消息
Http failure response for https://domain:8443/app-api/toto: 401 OK
这是我的 spring 引导配置:
@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and() // (1)
.authorizeRequests().anyRequest().authenticated() // (2)
.and()
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt); // (3)
}
}
app.properties:
spring.security.oauth2.resourceserver.jwt.issuer-uri=https://sts.windows.net/tenantId
Angular msal 配置:
export const protectedResourceMap: [string, string[]][] = [
['https://graph.microsoft.com/v1.0/me', ['user.read']],
[azureConfig.api_endpoint, ['api://appId/api_access_scop']]
];
export const unprotectedResource = [
];
export const scopes = [
"api://appId/api_access_scop"
];
MsalModule.forRoot({
auth: {
clientId: azureConfig.applicationID,
authority: "https://login.microsoftonline.com/" + azureConfig.tenantID,
validateAuthority: true,
redirectUri: azureConfig.rediretUri,
postLogoutRedirectUri: azureConfig.rediretUri,
navigateToLoginRequestUrl: true,
},
cache: {
cacheLocation: "localStorage",
storeAuthStateInCookie: true,
},
},
{
popUp: false,
consentScopes: scopes,
unprotectedResources: unprotectedResource,
protectedResourceMap: protectedResourceMap,
extraQueryParameters: {}
}
)
401 是未经授权的,可能由于以下情况导致客户端错误响应。
- 401 Unauthorized Error 表示请求的资源被限制并需要身份验证,但客户端未能提供任何此类身份验证。
- 一些应用程序使用 401 未授权错误来限制来自特定 IP 地址的访问请求,因此如果身份验证不是问题,服务器端禁止可能是问题所在。
- 401 未经授权错误的最常见原因只是输入了错误的 URL。仔细检查返回 401 Unauthorized Error 错误的确切 URL 是个好主意,以确保它是您打算请求的确切资源。
- 大多数现代网络应用程序利用 cookie 来存储用户身份验证状态,用于通知网络应用程序用户是否处于活动状态以及当前客户端(浏览器)应授予何种授权
并且应该存在无效或损坏的 cookie 问题,导致服务器身份验证不正确。
- 就像 cookie 一样,本地浏览器缓存也可能导致出现 401 未经授权错误。对应用程序实时版本的更改与设备上已有的缓存版本冲突,这有时会导致 401 未授权错误。尝试清除浏览器的缓存,看看是否能解决问题。
- 如果您最近清除了浏览器 cookie,但问题仍然存在,应用程序可能 运行 与您之前的会话有问题。如果服务器无法识别客户端发送的会话令牌,或者服务器出现错误表明令牌无效,您可能会因此看到 401 未经授权的错误。
- 我还建议您检查您的 Web 服务器 configuration.For 示例:服务器可能配置为拒绝对某些目录或 URL 的请求,这可能会导致 401 未经授权错误。
- 检查某种形式的服务器端日志。应用程序日志通常是应用程序所做的历史记录,服务器日志与 运行 应用程序的实际硬件相关。
以上参考点为:https://airbrake.io/blog/http-errors/401-unauthorized-error
显然,这是一个已知问题:see
当我在服务器上配置 SSL 证书时,它开始工作
当我在本地测试我的应用程序时,它可以工作,但是当我将它部署到产品环境时,它就不再工作了。我收到 401 错误。 应用程序部署在远程服务器上时,是否有特定的配置?
我用spring boot + angular 6 + msal v1.
重现步骤 在 Azure 上配置应用程序以 spring 启动 api 在 Azure 上为使用 msal 版本 1 的 angular 6 ap 配置应用程序 Exemple link
失败给出的日志消息
Http failure response for https://domain:8443/app-api/toto: 401 OK
这是我的 spring 引导配置:
@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and() // (1)
.authorizeRequests().anyRequest().authenticated() // (2)
.and()
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt); // (3)
}
}
app.properties:
spring.security.oauth2.resourceserver.jwt.issuer-uri=https://sts.windows.net/tenantId
Angular msal 配置:
export const protectedResourceMap: [string, string[]][] = [
['https://graph.microsoft.com/v1.0/me', ['user.read']],
[azureConfig.api_endpoint, ['api://appId/api_access_scop']]
];
export const unprotectedResource = [
];
export const scopes = [
"api://appId/api_access_scop"
];
MsalModule.forRoot({
auth: {
clientId: azureConfig.applicationID,
authority: "https://login.microsoftonline.com/" + azureConfig.tenantID,
validateAuthority: true,
redirectUri: azureConfig.rediretUri,
postLogoutRedirectUri: azureConfig.rediretUri,
navigateToLoginRequestUrl: true,
},
cache: {
cacheLocation: "localStorage",
storeAuthStateInCookie: true,
},
},
{
popUp: false,
consentScopes: scopes,
unprotectedResources: unprotectedResource,
protectedResourceMap: protectedResourceMap,
extraQueryParameters: {}
}
)
401 是未经授权的,可能由于以下情况导致客户端错误响应。
- 401 Unauthorized Error 表示请求的资源被限制并需要身份验证,但客户端未能提供任何此类身份验证。
- 一些应用程序使用 401 未授权错误来限制来自特定 IP 地址的访问请求,因此如果身份验证不是问题,服务器端禁止可能是问题所在。
- 401 未经授权错误的最常见原因只是输入了错误的 URL。仔细检查返回 401 Unauthorized Error 错误的确切 URL 是个好主意,以确保它是您打算请求的确切资源。
- 大多数现代网络应用程序利用 cookie 来存储用户身份验证状态,用于通知网络应用程序用户是否处于活动状态以及当前客户端(浏览器)应授予何种授权 并且应该存在无效或损坏的 cookie 问题,导致服务器身份验证不正确。
- 就像 cookie 一样,本地浏览器缓存也可能导致出现 401 未经授权错误。对应用程序实时版本的更改与设备上已有的缓存版本冲突,这有时会导致 401 未授权错误。尝试清除浏览器的缓存,看看是否能解决问题。
- 如果您最近清除了浏览器 cookie,但问题仍然存在,应用程序可能 运行 与您之前的会话有问题。如果服务器无法识别客户端发送的会话令牌,或者服务器出现错误表明令牌无效,您可能会因此看到 401 未经授权的错误。
- 我还建议您检查您的 Web 服务器 configuration.For 示例:服务器可能配置为拒绝对某些目录或 URL 的请求,这可能会导致 401 未经授权错误。
- 检查某种形式的服务器端日志。应用程序日志通常是应用程序所做的历史记录,服务器日志与 运行 应用程序的实际硬件相关。
以上参考点为:https://airbrake.io/blog/http-errors/401-unauthorized-error
显然,这是一个已知问题:see
当我在服务器上配置 SSL 证书时,它开始工作