Java 无法使用Spring Security清除弹出的用户凭据请求?
我正在将Spring安全性集成到我的Spring MVC和Angular应用程序中 版本: 弹簧安全性:4.1.5.1释放 SpringMVC:4.2.0.0版本 角度:4 集成后,我看到浏览器显示用户名和密码的弹出窗口。即使在进行了以下更改之后,我也无法摆脱弹出窗口,继续使用angular中的自定义登录表单Java 无法使用Spring Security清除弹出的用户凭据请求?,java,spring,angular,spring-security,Java,Spring,Angular,Spring Security,我正在将Spring安全性集成到我的Spring MVC和Angular应用程序中 版本: 弹簧安全性:4.1.5.1释放 SpringMVC:4.2.0.0版本 角度:4 集成后,我看到浏览器显示用户名和密码的弹出窗口。即使在进行了以下更改之后,我也无法摆脱弹出窗口,继续使用angular中的自定义登录表单 首先,我使用的是http基本身份验证机制。 为此,请按照以下步骤创建SecurityConfig.java。 [更新] @Configuration @EnableWebSecurity
首先,我使用的是http基本身份验证机制。 为此,请按照以下步骤创建SecurityConfig.java。 [更新]
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:on
auth.inMemoryAuthentication().withUser("user1").password("user1").roles("USER").and().withUser("user2")
.password("user2").roles("USER").and().withUser("admin").password("admin").roles("ADMIN");
// @formatter:off
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:on
http
.authorizeRequests().antMatchers("/index.html", "/", "/login", "*.*", "/*.bundle.*", "/*.ico")
.permitAll().anyRequest().authenticated().and().csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
// @formatter:off
}
}
对于资源和路径解析,按照ServletContextConfiguration.java创建
@Configuration
@EnableWebMvc
public class ServletContextConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(true).ignoreAcceptHeader(true).useJaf(false)
.defaultContentType(MediaType.APPLICATION_JSON).mediaType("html",
MediaType.TEXT_HTML)
.mediaType("xml", MediaType.APPLICATION_XML).mediaType("json",
MediaType.APPLICATION_JSON);
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean(name = "contentNegotiatingViewResolver")
public ViewResolver getContentNegotiatingViewResolver(ContentNegotiationManager manager) {
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
resolver.setContentNegotiationManager(manager);
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/dist/").resourceChain(true)
.addResolver(new PathResourceResolver());
}
}
以下是我的web.xml
:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>*.html</url-pattern>
<url-pattern>/index.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- Spring Security -->
<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>
</web-app>
角度变化:
在app.module.ts
中,引入了http拦截器,按照建议将http头X-request-With
插入
从'@angular/platform browser'导入{BrowserModule};
从'@angular/forms'导入{FormsModule};
从'@angular/http'导入{HttpModule};
从'@angular/router'导入{RouterModule,Routes};
从“@angular/core”导入{NgModule};
从“@angular/core”导入{Injectable};
进口{
HttpEvent、HttpInterceptor、HttpHandler、HttpRequest、HTTP_拦截器
}来自“@angular/common/http”;
从“rxjs/Observable”导入{Observable};
从“./app.component”导入{AppComponent};
从“./components/store/store.component”导入{StoreComponent};
从“./services/store.service”导入{StoreService};
从“./components/login/login.component”导入{LoginComponent};
从“./components/logout/logout.component”导入{LogoutComponent};
@可注射()
导出类XhrInterceptor实现HttpInterceptor{
截取(请求:HttpRequest
我认为,请求可能并没有到达angular客户端,服务器端发生了一些错误。
这里可能缺少什么
更新:
根据建议,我已将http.basic()
请求替换为http.authorizeRequests()
。现在,不会为/login
请求显示弹出窗口。但在我尝试通过调用/authenticate
在自定义登录页面中提交用户凭据后,我再次看到弹出窗口:(
这里可能缺少什么
请澄清
谢谢。这是由于配置中的http.httpBasic()
造成的,它应该是http.authorizeRequests()
这是由于配置中的http.httpBasic()
造成的,它应该是http.authorizeRequests()
您仍然在使用spring security的http基本身份验证,这就是为什么它会显示基本身份验证弹出窗口,不要使用它,使用http.autherizeRequest()并使用Macher允许您的登录url。
在routemodule中,为登录组件定义相同的路径。它会起作用。您仍然在使用spring security的http基本身份验证,这就是为什么它会显示基本身份验证弹出窗口,不要使用它,使用http.autherizeRequest()并使用Macher允许您的登录url。
在routemodule中为登录组件定义相同的路径。这会起作用。但在Spring安全教程中,我不明白他为什么会提到http.basic()和to avoid()在客户端弹出X-request-With-header的介绍,这在这里没有任何区别。可能X-request-With-header也可以工作,但我在遵循此官方文档时也遇到了相同的问题。我在spring security中使用angular和其他自定义登录表单文档,这很有效。但是在spring security教程中,我不明白他为什么谈到http.basic()和避免()客户端弹出X-request-With-header的介绍,这在这里没有任何区别。可能X-request-With-header也有效,但我在遵循此官方文档时也遇到了相同的问题。我在spring security中使用angular和其他自定义登录表单文档,这很有效。你能解决你的问题吗m、 我也有同样的问题。如果你能帮忙…谢谢苏迪尔。你能解决你的问题吗?我也有同样的问题。如果你能帮忙…谢谢苏迪尔
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app
http://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd">
<wls:context-root>/</wls:context-root>
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>org.slf4j.*</wls:package-name>
<wls:package-name>org.springframework.*</wls:package-name>
<wls:package-name>org.springframework.web.servlet.view.*</wls:package-name>
<wls:package-name>oracle.core.*</wls:package-name>
<wls:package-name>oracle.jdbc.*</wls:package-name>
<wls:package-name>oracle.net.*</wls:package-name>
<wls:package-name>oracle.sql.*</wls:package-name>
<wls:package-name>oracle.security.*</wls:package-name>
<wls:package-name>org.hibernate.*</wls:package-name>
<wls:package-name>com.fasterxml.*</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
</wls:weblogic-web-app>
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule, Routes } from '@angular/router';
import { NgModule } from '@angular/core';
import { Injectable } from '@angular/core';
import {
HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HTTP_INTERCEPTORS
} from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { AppComponent } from './app.component';
import { StoreComponent } from './components/store/store.component';
import { StoreService } from './services/store.service';
import { LoginComponent } from './components/login/login.component';
import { LogoutComponent } from './components/logout/logout.component';
@Injectable()
export class XhrInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler) {
const xhr = req.clone({
headers: req.headers.set('X-Requested-With', 'XMLHttpRequest')
});
console.log(">>>>>>>>>>HttpRequest intercepted...");
return next.handle(xhr);
}
}
const appRoutes: Routes = [
{ path:'', pathMatch: 'full', redirectTo: 'login' },
{ path: 'login', component: LoginComponent},
{ path: 'logout', component: LogoutComponent },
{ path: 'store', component: StoreComponent }
]
@NgModule({
declarations: [
AppComponent,
StoreComponent,
LoginComponent,
LogoutComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
RouterModule.forRoot(appRoutes)
],
providers: [ StoreService, { provide: HTTP_INTERCEPTORS, useClass: XhrInterceptor, multi: true } ],
bootstrap: [ AppComponent ]
})
export class AppModule { }