【问题标题】:How to provide custom security configuration for oauth2 with spring-boot 1.3.0.RC1如何使用 spring-boot 1.3.0.RC1 为 oauth2 提供自定义安全配置
【发布时间】:2016-01-25 23:02:59
【问题描述】:

在 spring-cloud Angel.SR3 版本中,我遵循了 https://github.com/spring-cloud-samples/sso 中的示例,并且在 spring-boot 1.2.6.RELEASE 中一切正常。

但是在 spring-boot 1.3.0.RC1 中,oauth2 的内容已经移入 spring-boot 本身,并且下面的代码无法编译,因为类 OAuth2SsoConfigurerAdapter 不再存在。

创建等效配置的 spring-boot 唯一方法是什么?

public static void main(String[] args) {
    SpringApplication.run(MainAppApplication.class, args);
}

...

@Component
public static class LoginConfigurer extends OAuth2SsoConfigurerAdapter  {

    @Override
    public void match(RequestMatchers matchers) {
        matchers.antMatchers("/dashboard/**");
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/dashboard/**").authorizeRequests().anyRequest()
        .authenticated().and().csrf()
        .csrfTokenRepository(csrfTokenRepository()).and()
        .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);
    }

    private Filter csrfHeaderFilter() {
        return new OncePerRequestFilter() {
    ...
        };
    }

    ...

}

【问题讨论】:

  • 您必须同时配置授权和资源服务器。您的 LoginConfigurer 是资源服务器应该是的那种。看看this例子
  • 我已经配置了授权服务器和资源服务器。我正在寻找的是 OAuth2SsoConfigurerAdapter 类的 Spring-Boot 1.3 等效项(它曾经在 Spring-cloud 的 Angel.SR3 版本中,但从 Brixton.M1 中删除)

标签: java spring spring-security spring-boot spring-cloud


【解决方案1】:

你只需要使用org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter并小心使用这个注解org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso

我写得仔细,因为它的行为取决于你添加它的位置。如javadoc中所述:

启用 OAuth2 单点登录 (SSO)。如果存在用户提供的现有 WebSecurityConfigurerAdapter 并使用 @EnableOAuth2Sso 进行注释,则通过添加身份验证过滤器和身份验证入口点对其进行增强。如果用户只有@EnableOAuth2Sso,但没有在 WebSecurityConfigurerAdapter 上,则添加一个,所有路径均受保护,并按顺序将其置于 Spring Boot 中默认的 HTTP Basic 安全链之前。

希望有帮助!

【讨论】:

    【解决方案2】:

    原来不需要特殊的适配器,只需常规的 WebSecurityConfigurerAdapter 就可以了。如果涉及 oauth2 SSO,您无法从下面分辨代码,更透明,可以说。

    @Configuration 
    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
    protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
    
        @Autowired
        private SecurityProperties security;
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // @formatter:off
            http
            .authorizeRequests()
                .antMatchers("/", "/ssologout").permitAll()
                .anyRequest().fullyAuthenticated()
            .and()
                .formLogin()
                    .loginPage("/login").failureUrl("/login?error")
                .permitAll()
            .and()
                .logout().permitAll();
            // @formatter:on
        }
    
    }
    

    【讨论】:

    • 顺便说一句,我认为您不需要 @Autowired 和私有属性,Spring boot 会自动将其注入到配置方法中
    猜你喜欢
    • 2018-12-05
    • 2016-06-25
    • 2018-07-21
    • 2017-03-13
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 2016-02-12
    • 2014-07-22
    相关资源
    最近更新 更多