我有一个 spring-boot 应用程序。现在我需要将一些数据导出到第三方,我想到使用 jwt。现在我的挑战是如何使用 spring-security 在一个应用程序中处理基于表单的身份验证和 jwt 基本身份验证。
简而言之,到达 api/public
的请求必须使用 jwt 进行身份验证/授权。而所有其他请求都重定向到 /login
页面。
这可能吗?
这就是我现在的安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").authenticated()
.antMatchers("/users").authenticated()
.antMatchers(LOGIN_URL).permitAll()
.and()
.formLogin()
.loginPage(LOGIN_URL).permitAll()
.and()
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/auth/logout")).logoutSuccessUrl(LOGIN_URL)
.and()
.rememberMe().tokenValiditySeconds(3600*24).key("key").userDetailsService(userPrincipalDetailService);
}
最佳答案
在 configure
方法中的 http
上使用 .antMatcher
并给出您希望 spring-security 处理的前缀。
所以 spring security 只会处理给定的前缀,如下所示:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/spring_handle_urls/**").authorizeRequests()
.and() ...
;
}
使用过滤器和 jwt
自行处理其他网址。
关于java - 如何在一个应用程序中处理基于表单的身份验证和基于 (jwt) token 的身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57890321/