Java 如何在一个应用程序中处理基于表单的身份验证和基于(jwt)令牌的身份验证

Java 如何在一个应用程序中处理基于表单的身份验证和基于(jwt)令牌的身份验证,java,spring-boot,spring-security,Java,Spring Boot,Spring Security,我有一个spring boot应用程序。现在我需要将一些数据导出到第三方,我想使用jwt。现在我的挑战是如何使用spring安全性在一个应用程序中处理基于表单的身份验证和基于jwt的身份验证 简而言之,api/public的请求必须使用jwt进行身份验证/授权。而所有其他请求重定向到/login页面 这可能吗 这就是我的安全配置现在的样子: @Override protected void configure(HttpSecurity http) throws Exception {

我有一个spring boot应用程序。现在我需要将一些数据导出到第三方,我想使用jwt。现在我的挑战是如何使用spring安全性在一个应用程序中处理基于表单的身份验证和基于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() ...
            ;
        }

使用过滤器自己处理其他URL,并在
configure
方法中的
http
上使用
.antMatcher
,并给出希望spring security处理的前缀。 因此,spring security将只处理给定前缀,如下所示:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/spring_handle_urls/**").authorizeRequests()
            .and() ...
            ;
        }

使用过滤器自己处理其他URL,并使用此代码更新WebSecurityConfigureAdapter.java文件

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
    http.addFilterBefore(requestValidateFilter(), BasicAuthenticationFilter.class);
    http.authorizeRequests().antMatchers("/spring_handle_urls/**").authenticated();
    http.addFilterAfter(responseValidateFilter(), BasicAuthenticationFilter.class);
}

使用以下代码更新WebSecurityConfigureAdapter.java文件

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
    http.addFilterBefore(requestValidateFilter(), BasicAuthenticationFilter.class);
    http.authorizeRequests().antMatchers("/spring_handle_urls/**").authenticated();
    http.addFilterAfter(responseValidateFilter(), BasicAuthenticationFilter.class);
}

你实施了这个吗?有错误吗?你实现了吗?有什么错误吗?