繁体   English   中英

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

[英]How can handle form-based authentication together with (jwt)token based authentication in one app

我有一个春季启动应用程序。 现在我需要将一些数据导出到第三方,我想到了使用jwt。 现在我面临的挑战是如何使用spring-security在一个应用程序中处理基于表单的身份验证和基于jwt的身份验证。

简而言之,必须使用jwt对api/public进行身份验证/授权。 而所有其他请求都重定向到/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自己处理其他网址。

使用此代码更新WebSecurityConfigurerAdapter.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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM