如何在Spring Boot Security 2.0中使用2种不同的身份验证方法

bzzhuh

我的应用程序使用基本身份验证进行spring-rest-rest访问(在“ / api / **”下的所有内容),并使用面向用户的API的UserDetailsS​​ervice进行JWT身份验证(在本示例中,在“ / component”下的所有内容)。在升级到Spring Boot 2之前,我的工作如下:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
    private UserDetailsService userDetailsService;
    private BCryptPasswordEncoder passwordEncoder;

    public SecurityConfig( UserDetailsService userDetailsService, BCryptPasswordEncoder passwordEncoder )
    {
        this.userDetailsService = userDetailsService;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public void configure( AuthenticationManagerBuilder authenticationManagerBuilder ) throws Exception
    {
        authenticationManagerBuilder
                .userDetailsService( userDetailsService ).passwordEncoder( passwordEncoder )
                .and()
                .inMemoryAuthentication()
                .withUser( "rest" ).password( "something" ).authorities( "REST" ).roles( "REST" );
    }

    @Override
    protected void configure( HttpSecurity http ) throws Exception
    {
        http
                .cors()
                .and()
                .csrf().disable()
                .authorizeRequests()
                    .antMatchers( HttpMethod.POST, SecurityProperties.SIGN_UP_URL, SecurityProperties.LOGIN_URL ).permitAll()
                    .antMatchers( HttpMethod.GET, "/component1/**", "/component2/**", "/component3/**", "/component4/**" ).authenticated()
                    .antMatchers( HttpMethod.POST, "/component1/**", "/component2/**", "/component3/**", "/component4/**" ).authenticated()
                    .antMatchers( HttpMethod.GET, "/api/**" ).hasRole( "REST" )
                    .antMatchers( HttpMethod.OPTIONS, "/api/**" ).hasRole( "REST" )
                    .antMatchers( HttpMethod.PUT, "/api/**" ).hasRole( "REST" )
                    .antMatchers( HttpMethod.PATCH, "/api/**" ).hasRole( "REST" )
                    .antMatchers( HttpMethod.DELETE, "/api/**" ).hasRole( "REST" )
                .anyRequest().authenticated()
                .and()
                .httpBasic()
                .and()
                .addFilter( new JWTAuthenticationFilter( authenticationManager() ) )
                .addFilter( new JWTAuthorizationFilter( authenticationManager() ) )
                .addFilter( new BasicAuthenticationFilter( authenticationManager() ) );
    }
}

对于CORS,有一个单独的配置,我现在不关心,因为它以前运行良好,现在也不是任何麻烦的根源。

因此,在升级到Spring Boot Security 2时,我尝试将每种安全性中的所有内容放入2个不同的WebSecurityConfigurerAdapter中,这将使它们中的一种起作用,无论哪个@Order(1)编号都较小。我在某种程度上遵循了此处的示例:https : //github.com/spring-projects/spring-boot/wiki/Spring-Boot-Security-2.0

我想知道现在是否应该使用AuthenticationManagerBuilder摆脱此配置覆盖?还是将它们合并为一个文件?我不确定该怎么做,有人对此有经验吗?

bzzhuh

我今天回到这个。我放弃了整个多个WebSecurityConfigurerAdapter的东西,只是回到使用其中一个。使用过的Spring Security日志记录级别= DEBUG显示了一些容易修复的问题,因为我所做的事情不再支持Spring 5。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Spring Boot 2和Spring Security 5进行多重身份验证

如何在 Spring Boot 中使用预定义的令牌绕过 Oauth2 身份验证?

Spring boot、Security、OAuth2:是否可以使用自定义 AuthorizationCodeResourceDetails?身份验证服务器需要重定向 url 中的特定参数

如何在Spring Boot中以Spring Security级别启用CORS

如何在Spring Boot中覆盖Spring Security默认配置

在XwsSecurityInterceptor中使用SpringPlainTextPasswordValidationCallbackHandler时使用Spring Security身份验证失败的Spring Boot

如何在Spring Boot Security中设置自己的权限?

如何在Spring Boot Security中启用或禁用用户

在Spring Boot OAuth2授权服务器中使用Active Directory身份验证

如何使用Spring Security实现Ldap身份验证(Spring Boot)

Spring Boot Security中OAuth2Client中的SSO

如何在Spring Boot中使用spring-security.xml中的配置?

从Spring Boot Oauth2迁移到Spring Security 5

Spring Boot 2,Spring Security 5和@WithMockUser

带有Spring Boot和OAuth 2的Spring Security

Angular 2,Spring Boot,Spring Security,登录表单

如何使用Spring Security OAuth2在Spring Boot中注册自定义BasicAuthenticationFilter AuthenticationProvider

Grails Spring Security Shiro,如何创建具有 2 个不同身份验证成功 url 的 2 个登录表单?

Spring Boot Security Oauth2-添加动态OIDC参数

Spring Boot 2 Security在登录时下载字体文件

如何配置Spring Boot和Spring Security以支持表单登录和Google OAuth2登录

连接多种身份验证机制Spring Boot Security

Spring Boot Security-允许无需身份验证

Javascript WebApp 的 Spring Security Oauth2 身份验证

如何使用Spring Boot + Angular处理外部OAuth2身份验证

如何使用Spring Boot伪装客户端进行Oauth2身份验证?

如何在Spring Security中关闭Websocket的身份验证?

使用外部身份验证源时如何在Spring Boot中模拟身份验证

如何在Spring Boot应用程序中关闭Spring Security