【问题标题】:How to use 2 different authentication methods with Spring Boot Security 2.0如何在 Spring Boot Security 2.0 中使用 2 种不同的身份验证方法
【发布时间】:2018-11-25 19:15:59
【问题描述】:

我的应用程序对 spring-data-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 摆脱这个配置覆盖?还是将它们合并到一个文件中?我不知道该怎么做,有没有人有这方面的经验?

【问题讨论】:

    标签: spring spring-boot spring-security


    【解决方案1】:

    我今天回来了。我放弃了整个多个 WebSecurityConfigurerAdapter 的事情,然后又重新使用了一个。使用的 Spring Security 日志记录级别 =DEBUG 显示了一些很容易修复的问题,我正在这样做,不再支持 Spring 5。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-14
      • 2014-12-20
      • 2020-05-12
      • 2020-12-05
      • 2022-08-16
      • 2018-01-01
      • 1970-01-01
      • 2013-02-04
      相关资源
      最近更新 更多