Spring 具有多个登录页面的安全性仅采用顺序 1
Spring Security with multiple login pages only takes order 1
我有一个 Spring 启动应用程序,其中登录页面将位于 index(nav) 和登录页面。我做了订单注释配置,但它只按预期工作订单 1(通过切换订单测试,1 仅工作正常)对于订单 2 错误:不支持请求方法 'POST',知道吗?
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Qualifier("userDetailsServiceImpl")
@Autowired
private UserDetailsService userDetailsService;
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Configuration
@Order(1)
public static class WebSecurityConfig1 extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(new AntPathRequestMatcher("/**"))
.authorizeRequests()
.antMatchers("/resources/**", "/registration", "/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/loginIndex")
.usernameParameter("username")
.passwordParameter("password")
.failureUrl("/loginIndex?error")
.loginProcessingUrl("/loginIndex")
.permitAll()
.and()
.logout()
.permitAll();
}
}
@Configuration
@Order(2)
public static class WebSecurityConfig2 extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(new AntPathRequestMatcher("/**"))
.authorizeRequests()
.antMatchers("/resources/**", "/registration","/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.usernameParameter("username")
.passwordParameter("password")
.failureUrl("/login?error")
.loginProcessingUrl("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
}
@Bean
public AuthenticationManager customAuthenticationManager() throws Exception {
return authenticationManager();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}
你的配置有问题
WebSecurityConfig1
和 WebSecurityConfig2
都配置为匹配每个 url。 .ie new AntPathRequestMatcher("/**")
- 这意味着
@Order(1)
将始终满足所有请求。
解决方案
因此,首先您需要决定要将哪些 URL 的用户重定向到第一个登录页面,以及要将哪些 URL 的用户重定向到第二个登录页面。
例如,您可以说以 /user
开头的网址转到 loginPage("/loginIndex")
,其他所有内容转到 loginPage("/login")
。您可以通过在 WebSecurityConfig1
中将 new AntPathRequestMatcher("/**")
替换为 (new AntPathRequestMatcher("/user*")
) 来实现它
我有一个 Spring 启动应用程序,其中登录页面将位于 index(nav) 和登录页面。我做了订单注释配置,但它只按预期工作订单 1(通过切换订单测试,1 仅工作正常)对于订单 2 错误:不支持请求方法 'POST',知道吗?
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Qualifier("userDetailsServiceImpl")
@Autowired
private UserDetailsService userDetailsService;
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Configuration
@Order(1)
public static class WebSecurityConfig1 extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(new AntPathRequestMatcher("/**"))
.authorizeRequests()
.antMatchers("/resources/**", "/registration", "/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/loginIndex")
.usernameParameter("username")
.passwordParameter("password")
.failureUrl("/loginIndex?error")
.loginProcessingUrl("/loginIndex")
.permitAll()
.and()
.logout()
.permitAll();
}
}
@Configuration
@Order(2)
public static class WebSecurityConfig2 extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(new AntPathRequestMatcher("/**"))
.authorizeRequests()
.antMatchers("/resources/**", "/registration","/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.usernameParameter("username")
.passwordParameter("password")
.failureUrl("/login?error")
.loginProcessingUrl("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
}
@Bean
public AuthenticationManager customAuthenticationManager() throws Exception {
return authenticationManager();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}
你的配置有问题
WebSecurityConfig1
和WebSecurityConfig2
都配置为匹配每个 url。 .ienew AntPathRequestMatcher("/**")
- 这意味着
@Order(1)
将始终满足所有请求。
解决方案
因此,首先您需要决定要将哪些 URL 的用户重定向到第一个登录页面,以及要将哪些 URL 的用户重定向到第二个登录页面。
例如,您可以说以 /user
开头的网址转到 loginPage("/loginIndex")
,其他所有内容转到 loginPage("/login")
。您可以通过在 WebSecurityConfig1
new AntPathRequestMatcher("/**")
替换为 (new AntPathRequestMatcher("/user*")
) 来实现它