Java 如何使用spring boot 1.3.0.RC1为oauth2提供自定义安全配置

Java 如何使用spring boot 1.3.0.RC1为oauth2提供自定义安全配置,java,spring,spring-security,spring-boot,spring-cloud,Java,Spring,Spring Security,Spring Boot,Spring Cloud,对于SpringCloudAngel.SR3发行版,我遵循了中的示例,SpringBoot1.2.6.release运行良好 然而,在SpringBoot 1.3.0.RC1中,oauth2已经转移到SpringBoot本身,下面的代码无法编译,因为类OAuth2ssoconfigureAdapter不再存在 创建等效配置的spring boot唯一方法是什么 public static void main(String[] args) { SpringApplication.run(M

对于SpringCloudAngel.SR3发行版,我遵循了中的示例,SpringBoot1.2.6.release运行良好

然而,在SpringBoot 1.3.0.RC1中,oauth2已经转移到SpringBoot本身,下面的代码无法编译,因为类OAuth2ssoconfigureAdapter不再存在

创建等效配置的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() {
    ...
        };
    }

    ...

}

事实证明,不需要特殊的适配器,只需要常规的WebSecurity配置适配器就可以了。如果涉及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
    }

}

事实证明,不需要特殊的适配器,只需要常规的WebSecurity配置适配器就可以了。如果涉及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
    }

}

您只需使用
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
并仔细使用此注释
org.springframework.boot.autoconfigure.security.oauth2.client.enableAuth2sso

我写得很仔细,因为它的行为取决于你在哪里添加它。正如javadoc中所述:

启用OAuth2单点登录(SSO)。如果用户提供了一个现有的WebSecurityConfigureAdapter,并用@EnableOAuth2Sso进行了注释,则可以通过添加身份验证筛选器和身份验证入口点来增强它。如果用户只有@enableAuth2sso,但在WebSecurityConfigureAdapter上没有,则添加一个,所有路径都受保护,并且在Spring Boot中的顺序将其置于默认HTTP基本安全链之前


希望有帮助

您只需使用org.springframework.security.config.annotation.web.configuration.websecurityConfigureAdapter并小心地使用此注释
org.springframework.boot.autoconfigure.security.oauth2.client.enableAuth2sso

我写得很仔细,因为它的行为取决于你在哪里添加它。正如javadoc中所述:

启用OAuth2单点登录(SSO)。如果用户提供了一个现有的WebSecurityConfigureAdapter,并用@EnableOAuth2Sso进行了注释,则可以通过添加身份验证筛选器和身份验证入口点来增强它。如果用户只有@enableAuth2sso,但在WebSecurityConfigureAdapter上没有,则添加一个,所有路径都受保护,并且在Spring Boot中的顺序将其置于默认HTTP基本安全链之前


希望有帮助

您必须同时配置授权和资源服务器。您的LoginConfigure是资源服务器应该具备的功能。看一看示例我配置了授权服务器和资源服务器。我想要的是Spring Boot 1.3版本的OAuth2ssoconfigureAdapter类(该类以前在Spring cloud的Angel.SR3版本中,但已从Brixton.M1中删除),您必须同时配置授权和资源服务器。您的LoginConfigure是资源服务器应该具备的功能。看一看示例我配置了授权服务器和资源服务器。我要找的是Spring Boot 1.3版本的OAuth2sSoConfigureAdapter类(它以前是Spring cloud的Angel.SR3版本,但从Brixton.M1中删除了),顺便说一句,我认为您不需要@Autowired和private属性,Spring boot将自动将其注入配置方法顺便说一句,我认为您不需要@Autowired和private属性,Spring boot将自动将其注入配置方法