Spring Boot中的两种登录认证方式
Two login authentication ways in Spring Boot
我需要开发一个具有两个身份验证端点的应用程序:一个是登录 Web 表单,另一个是通过自定义令牌发送凭据。
我创建了两个 WebSecurityConfigurerAdapter
并且登录表单可以正常工作,但令牌不行:当我尝试通过令牌进行识别时,它 运行 可以,但是始终重定向到取消登录表单页面。
这是我的配置:
protected void configure(HttpSecurity http) throws Exception {
http
.addFilterBefore(authenticationFilter(), CustomAuthenticationFilter.class)
.authorizeRequests()
.mvcMatchers(PublicUrls.URLS).permitAll()
.anyRequest().fullyAuthenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/")
.permitAll()
.and()
.cors()
.and()
.logout()
.invalidateHttpSession(true)
.clearAuthentication(true)
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login?logout")
.permitAll();
}
..和令牌配置:
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.mvcMatcher(LOGINJWT)
.addFilterBefore(authenticationFilter(), WebAsyncManagerIntegrationFilter.class)
.authorizeRequests()
.antMatchers(LOGINJWT).permitAll()
.anyRequest().fullyAuthenticated()
.and()
.logout()
.invalidateHttpSession(true)
.clearAuthentication(true)
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login?logout")
.permitAll();
// @formatter:on
}
当我尝试通过令牌进行身份验证时,它 运行 customFilter
和自定义身份验证提供程序正确但始终重定向到登录页面。
类命令注释是这样的:
// Token annotation class
@Configuration
@Order(1)
@EnableWebSecurity
public class JwtWebSecurityConfigurerAdapter
extends WebSecurityConfigurerAdapter {....}
//login annotation clas
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
@EnableWebSecurity
@Configuration
@RequiredArgsConstructor
@Slf4j
@Order(2)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {...}
我没有看到问题。
我发现了问题:JWT 过滤器在 WebAsyncManagerIntegrationFilter
之前执行。
我需要开发一个具有两个身份验证端点的应用程序:一个是登录 Web 表单,另一个是通过自定义令牌发送凭据。
我创建了两个 WebSecurityConfigurerAdapter
并且登录表单可以正常工作,但令牌不行:当我尝试通过令牌进行识别时,它 运行 可以,但是始终重定向到取消登录表单页面。
这是我的配置:
protected void configure(HttpSecurity http) throws Exception {
http
.addFilterBefore(authenticationFilter(), CustomAuthenticationFilter.class)
.authorizeRequests()
.mvcMatchers(PublicUrls.URLS).permitAll()
.anyRequest().fullyAuthenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/")
.permitAll()
.and()
.cors()
.and()
.logout()
.invalidateHttpSession(true)
.clearAuthentication(true)
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login?logout")
.permitAll();
}
..和令牌配置:
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.mvcMatcher(LOGINJWT)
.addFilterBefore(authenticationFilter(), WebAsyncManagerIntegrationFilter.class)
.authorizeRequests()
.antMatchers(LOGINJWT).permitAll()
.anyRequest().fullyAuthenticated()
.and()
.logout()
.invalidateHttpSession(true)
.clearAuthentication(true)
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login?logout")
.permitAll();
// @formatter:on
}
当我尝试通过令牌进行身份验证时,它 运行 customFilter
和自定义身份验证提供程序正确但始终重定向到登录页面。
类命令注释是这样的:
// Token annotation class
@Configuration
@Order(1)
@EnableWebSecurity
public class JwtWebSecurityConfigurerAdapter
extends WebSecurityConfigurerAdapter {....}
//login annotation clas
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
@EnableWebSecurity
@Configuration
@RequiredArgsConstructor
@Slf4j
@Order(2)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {...}
我没有看到问题。
我发现了问题:JWT 过滤器在 WebAsyncManagerIntegrationFilter
之前执行。