Oauth和Spring安全

Oauth和Spring安全,spring,rest,spring-mvc,spring-security,oauth-2.0,Spring,Rest,Spring Mvc,Spring Security,Oauth 2.0,我有一个服务器端应用程序,它由两部分组成。第一部分是RESTAPI,另一部分是网页仪表板,用于管理整个服务器端应用程序 现在,我已经使用Oauth和spring security来保护REST API,并使用spring security的基于表单的身份验证来保护网页仪表板。 但是使用基于表单的身份验证会与oauth配置产生冲突 在SpringSecurity中有没有办法同时使用Oauth和基于表单的身份验证 这是securityConfiguration文件。我尝试添加两个配置,但没有成功,所

我有一个服务器端应用程序,它由两部分组成。第一部分是RESTAPI,另一部分是网页仪表板,用于管理整个服务器端应用程序

现在,我已经使用Oauth和spring security来保护REST API,并使用spring security的基于表单的身份验证来保护网页仪表板。 但是使用基于表单的身份验证会与oauth配置产生冲突

在SpringSecurity中有没有办法同时使用Oauth和基于表单的身份验证

这是securityConfiguration文件。我尝试添加两个配置,但没有成功,所以我尝试了使用两个安全标签的配置。在这里,表单登录配置可以正常工作,但用于oauth的其他配置不起作用。我可以获取访问令牌,但oauth不起作用,并且在尝试访问REST API时,我被重定向到仪表板登录页面

@Configuration
@EnableWebMvcSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

private static final Logger LOG = Logger.getLogger(SecurityConfiguration.class);

@Autowired
UserDetailsService webUserDetailsService;



BCryptPasswordEncoder passwordEncoder;

public SecurityConfiguration(){
    LOG.debug("OAuthSecurityConfiguration initialized");

    passwordEncoder = new BCryptPasswordEncoder();

    LOG.debug(passwordEncoder);
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {


    auth
        .userDetailsService(webUserDetailsService)
        .passwordEncoder(passwordEncoder);
    LOG.debug("user and password details");
    LOG.debug( auth
        .userDetailsService(webUserDetailsService)
        .passwordEncoder(passwordEncoder));
}

@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {

    LOG.debug("authenticationManagerBean");

    return super.authenticationManagerBean();
}



@Configuration
@EnableResourceServer
@ComponentScan(basePackages = {"**"})
@Order(1)                                                        
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {


    public void configure(ResourceServerSecurityConfigurer resources) {
        LOG.debug(resources);
        resources.resourceId("admin");
        resources.resourceId("admin");
        LOG.debug("configureresource:"+resources.resourceId("admin"));
    }

    protected void configure(HttpSecurity http) throws Exception {
        http
            .antMatcher("..")                   
            .authorizeRequests()               
                .anyRequest().hasRole("ADMIN");
                .and()
            .httpBasic();
    }
}

@Configuration  
@ComponentScan(basePackages = {"**"})
@Order(2)
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable()
        .authorizeRequests()
         .antMatchers("/login"
                    ).permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin().loginPage("/login/new")
          .defaultSuccessUrl("/**", true)
            .failureUrl("/login/fail")
                .permitAll()
           .and()
        .logout()
            .logoutUrl("/logout")
            .logoutSuccessUrl("/login/new")                                  
            .permitAll();
    ;
    }

}

提供的Sparkr示例很好地涵盖了这一点,请参见OAuth2ServerConfig类

在资源服务器配置类(扩展
ResourceServerConfigurerAdapter
)中:

在这里,您在
/api/**
下的资源将可供为“某些OAuth范围”授权的OAuth 2.0客户端访问,或由“管理员”角色“直接”认证的用户访问。请参阅:
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/api/**").access("#oauth2.hasScope('some_oauth_scope') or (!#oauth2.isOAuth() and hasRole('ROLE_ADMIN'))")
            // Enable form login 
            .and().formLogin();
    }
}