结合使用X509证书身份验证和基于JWT令牌的身份验证

时间:2020-10-30 07:58:19

标签: java spring-boot spring-security x509certificate jwt-auth

我有一个当前使用X509证书身份验证的Spring Boot应用程序。我可以同时使用JWT通过X509协议通过令牌和应用程序来保护API的安全吗?

用例:

应使用证书对公开/部署的应用程序URL进行身份验证,一旦验证,用户应出示有效的JWT令牌以访问应用程序内部的任何API。我目前为我的X509证书配置了以下Websecurity。

public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${security.enable-csrf}")
    private boolean csrfEnabled;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated().and().x509().subjectPrincipalRegex("CN=(.*?)(?:,|$)")
                .userDetailsService(userDetailsService());
        
        if (!csrfEnabled) {
            http.csrf().disable();
        }
    }

    @Bean
    public UserDetailsService userDetailsService() {
        return (UserDetailsService) username -> {
            if (username.equals("XXXX")) {
                return new User(username, "", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
            } else {
                throw new UsernameNotFoundException(String.format("User %s not found", username));
            }
        };

0 个答案:

没有答案