Spring 如果.antMatcher是http之后的第一个方法,那么默认登录会给出404,但为什么呢?
使用spring boot v2.0.0M3,我想使用spring security提供的默认登录页面,还想使用不同的安全配置拥有两个不同的端点,如下所述: 我的完整安全配置如下所示:Spring 如果.antMatcher是http之后的第一个方法,那么默认登录会给出404,但为什么呢?,spring,spring-mvc,spring-boot,login,spring-security,Spring,Spring Mvc,Spring Boot,Login,Spring Security,使用spring boot v2.0.0M3,我想使用spring security提供的默认登录页面,还想使用不同的安全配置拥有两个不同的端点,如下所述: 我的完整安全配置如下所示: @Configuration @Order(1) @EnableWebSecurity class ApiSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { private final JwtAuthentication
@Configuration
@Order(1)
@EnableWebSecurity
class ApiSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
private final JwtAuthenticationFilter jwtAuthenticationFilter;
public ApiSecurityConfigurationAdapter(JwtAuthenticationFilter jwtAuthenticationFilter) {
this.jwtAuthenticationFilter = jwtAuthenticationFilter;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/v1/**")
.csrf()
.disable()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(this::commence)
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
}
}
@Configuration
@Order(2)
class NonApiSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/manage/**")
.hasRole("USER")
.and().formLogin();
}
}
对于第二部分:以下配置起作用,它将用户重定向到登录页面,并显示默认登录页面:
http
.authorizeRequests()
.antMatchers("/manage/**")
.hasRole("USER")
.and().formLogin();
但是,在成功地将用户重定向到登录后,以下配置为/login
提供了404
:
http
.antMatcher("/manage/**")
.authorizeRequests()
.anyRequest()
.hasRole("USER")
.and().formLogin();
为什么呢?我认为这两种组合应该都能起作用,我在文档中找不到任何提示这不起作用的内容。使用默认formlogin设置创建一个新的WebSecurityConfigureAdapter类
http.formLogin(),该方法创建默认的/login
页面
解释:这是意料之中的,因为第一个antMatchers声明,只有当整个HttpSecurity以/manage/
开头时,它才会工作,而formLogin默认为/login
。要解决此问题,您需要遵循上述解决方法。使用默认formlogin设置创建一个新的WebSecurity配置适配器类
http.formLogin(),该方法创建默认的/login
页面
解释:这是意料之中的,因为第一个antMatchers声明,只有当整个HttpSecurity以/manage/
开头时,它才会工作,而formLogin默认为/login
。要解决此问题,您需要遵循上述解决方法。这是两种不同的配置。。。第一个保护所有URL并保护/manage
起始URL。但是后者仅将安全性应用于/manage
起始URL,包括登录表单。@M.Deinum这是有意的。我想我不能清楚地解释我的意图,所以我更新了这个问题。正如我在评论中所说的,这些是不同的配置。这与你的问题不清楚无关。是的,我知道他们是不同的。问题是,为什么默认登录页面会给出404和最后一个示例配置?`你读过我的评论了吗?因为这只适用于以/manage
开头的URL。。。这现在包括您的登录页面。这是两种不同的配置。。。第一个保护所有URL并保护/manage
起始URL。但是后者仅将安全性应用于/manage
起始URL,包括登录表单。@M.Deinum这是有意的。我想我不能清楚地解释我的意图,所以我更新了这个问题。正如我在评论中所说的,这些是不同的配置。这与你的问题不清楚无关。是的,我知道他们是不同的。问题是,为什么默认登录页面会给出404和最后一个示例配置?`你读过我的评论了吗?因为这只适用于以/manage
开头的URL。。。这现在包括您的登录页面。