Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring安全OAuth Java配置_Java_Spring_Spring Mvc_Spring Security_Spring Oauth2 - Fatal编程技术网

Spring安全OAuth Java配置

Spring安全OAuth Java配置,java,spring,spring-mvc,spring-security,spring-oauth2,Java,Spring,Spring Mvc,Spring Security,Spring Oauth2,我有一个Spring应用程序,而不是Spring引导应用程序,我试图将API的一部分声明为OAuth2,并使用Spring安全性进行保护。当我在XML中定义http元素时,我的授权和常规资源访问配置非常有效,但是当我尝试使用ResourceServerConfigureAdapter\35; configure(HttpSecurity)——以及@enableSourceServer等实现资源访问块时,configure方法甚至从不触发(端到端测试也会失败)。下面是Java配置示例:

我有一个Spring应用程序,而不是Spring引导应用程序,我试图将API的一部分声明为OAuth2,并使用Spring安全性进行保护。当我在XML中定义
http
元素时,我的授权和常规资源访问配置非常有效,但是当我尝试使用
ResourceServerConfigureAdapter\35; configure(HttpSecurity)
——以及
@enableSourceServer
等实现资源访问块时,
configure
方法甚至从不触发(端到端测试也会失败)。下面是Java配置示例:

        @Override
        public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
            resources.resourceId("oauth2/control");
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.antMatcher("/api/**")
                .authorizeRequests().anyRequest().hasRole("OAUTH_USER").and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                // before=PRE_AUTH_FILTER
                .addFilterBefore(oAuth2AuthenticationProcessingFilter, AbstractPreAuthenticatedProcessingFilter.class)
                .csrf().disable()
                .anonymous().disable()
                .exceptionHandling()
                .accessDeniedHandler(oAuth2AccessDeniedHandler)
                .authenticationEntryPoint(loginUrlAuthenticationEntryPoint);
        }
我尝试过声明几种不同的方式来实现配置,但没有成功。也就是说,我尝试了扩展
ResourceServerConfigurerAdapter
的顶级
@Configuration
类和返回
ResourceServerConfiguration
的bean方法,如中所示;我还尝试在适当的bean上显式地将order设置为
Ordered.HIGHEST\u priority

很可能需要注意的是,应用程序的大部分遗留安全配置都是通过上述XML元素定义的。关于我的配置的另一个可能的危险信号——为了让令牌端点根据基本身份验证客户端id:client\u secret进行身份验证,我必须在,因为这是对令牌端点进行身份验证的普通且“正确”的方法,所以我希望SpringConfig默认为它

无论如何,在任何情况下,我都无法启动
ResourceServerConfigureAdapter\configure(HttpSecurity)
。我的理论是:

  • 前面的XML HTTP块阻止了我的
    configure
    方法,使其不被调用

  • 这仅仅是一个SpringBoot特有的特性——尽管我还没有看到过这样的语言

  • 我在应用程序上下文中直接缺少了一些对象(同样,除了明显的
    @Configuration
    @Bean
    ,我工作中的操作注释是
    @EnableWebSecurity
    @EnableAuthenticationServer
    ,和
    @EnableResourceServer

  • 我犯了一些配置错误(duh)

任何例子都将不胜感激

下面的示例OAuth2配置类(几个不同迭代之一):

@配置
@启用Web安全性
@EnableResourceServer
公共类OAuth2配置{
等
@豆子
公共资源服务器配置adminResources(){
//从https://github.com/spring-projects/spring-security-oauth/tree/master/tests/annotation/multi
ResourceServerConfiguration resource=新的ResourceServerConfiguration(){
公共无效设置配置程序(列表配置程序){
超级设置配置器(配置器);
}
};
resource.setConfigurers(Collections.singletonList)(新的ResourceServerConfigurerAdapter(){
@凌驾
public void configure(ResourceServerSecurityConfigure资源)引发异常{
resources.resourceId(“oauth2/control”);
}
@凌驾
public void configure(HttpSecurity http)引发异常{
http.antMatcher(“/api/**”)
.authorizeRequests().anyRequest().hasRole(“OAUTH_用户”)。和()
.sessionManagement().sessionCreationPolicy(sessionCreationPolicy.STATELESS)和()
//before=预验证过滤器
.addFilterBefore(oAuth2AuthenticationProcessingFilter,AbstractPreAuthenticationdProcessingFilter.class)
.csrf().disable()
.anonymous().disable()
.例外处理()
.accessDeniedHandler(oAuth2AccessDeniedHandler)
.authenticationEntryPoint(LoginUrauThenticationEntryPoint);
}
}));
resource.setOrder(有序的、最高的_优先级);
返回资源;
}
@配置
@EnableAuthorizationServer
公共静态类OAuth2AuthorizationServerConfig扩展了AuthorizationServerConfigurerAdapter{
等
}
}
所拾取的类似的
配置:

<sec:http pattern="/api/**" create-session="stateless"
          entry-point-ref="loginUrlAuthenticationEntryPoint">
    <sec:anonymous enabled="false" />
    <sec:csrf disabled="true"/> <!-- csrf tokens don't make sense for a 3rd party API -->
    <sec:custom-filter ref="oauth2ProcessingFilter" before="PRE_AUTH_FILTER"/>
    <sec:access-denied-handler ref="oauthAccessDeniedHandler" />
    <sec:intercept-url pattern="/api/**" access="hasRole('OAUTH_USER')" />
</sec:http>

如果同时使用基于Java和基于XML的安全配置,则只会选择基于XML的安全配置。您是否尝试过禁用/注释所有基于XML的http安全配置,并查看是否会激活基于Java的http安全配置

我以前也有过类似的问题。我在Spring Security Gitter频道寻求帮助,并做了一些调查。见下文:


应该猜到了。谢谢@Sofia!
<sec:http pattern="/api/**" create-session="stateless"
          entry-point-ref="loginUrlAuthenticationEntryPoint">
    <sec:anonymous enabled="false" />
    <sec:csrf disabled="true"/> <!-- csrf tokens don't make sense for a 3rd party API -->
    <sec:custom-filter ref="oauth2ProcessingFilter" before="PRE_AUTH_FILTER"/>
    <sec:access-denied-handler ref="oauthAccessDeniedHandler" />
    <sec:intercept-url pattern="/api/**" access="hasRole('OAUTH_USER')" />
</sec:http>