Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
以xml形式从spring oauth2令牌请求获取响应_Spring_Spring Mvc_Jackson_Spring Security Oauth2 - Fatal编程技术网

以xml形式从spring oauth2令牌请求获取响应

以xml形式从spring oauth2令牌请求获取响应,spring,spring-mvc,jackson,spring-security-oauth2,Spring,Spring Mvc,Jackson,Spring Security Oauth2,我正在使用Spring4和SpringOAuth2实现一个OAuth2REST服务器。 响应可以是客户机请求头指定的xml或json。 但是,当我试图访问只支持JSON(application/JSON)的令牌时,我从oauth2得到了问题,它不支持xml(application/xml) 我得到的错误是: HTTP Status 406 - type Status report message description The resource identified by this reques

我正在使用Spring4和SpringOAuth2实现一个OAuth2REST服务器。 响应可以是客户机请求头指定的xml或json。 但是,当我试图访问只支持JSON(application/JSON)的令牌时,我从oauth2得到了问题,它不支持xml(application/xml)

我得到的错误是:

HTTP Status 406 -
type Status report
message
description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
Apache Tomcat/7.0.54
我的安全配置如下:

@Configuration 
@EnableWebSecurity(debug = true) 
@ComponentScan(basePackages = { "org.bluez.logiczweb.config.security.handler" }) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 
    private final static String applicationName = "crm"; 

    @Configuration 
    @EnableResourceServer 
    protected static class ResourceServerConfiguration extends 
            ResourceServerConfigurerAdapter { 

        @Autowired 
        private HeaderOnlyOAuth2ExceptionRenderer headerOnlyExceptionRender; 

        @Override 
        public void configure(ResourceServerSecurityConfigurer resources) { 
            resources.resourceId(applicationName); 
        } 

        public void configure(HttpSecurity http) throws Exception { 

            http.csrf().disable(); 
            http.sessionManagement().sessionCreationPolicy( 
                    SessionCreationPolicy.STATELESS); 
            http.requestMatchers().and().authorizeRequests() 
                    .antMatchers("/rest/accounts") 
                    .access("#oauth2.hasScope('read')").and().httpBasic() 
                    .authenticationEntryPoint(clientAuthenticationEntryPoint()); 

            // // .apply(new OAuth2ServerConfigurer()) 
            // .tokenStore(new InMemoryTokenStore()) 
            // .resourceId(applicationName); 

        } 

        @Bean 
        public OAuth2AccessDeniedHandler oauthAccessDeniedHandler() { 
            OAuth2AccessDeniedHandler accessDeniedHandler = new OAuth2AccessDeniedHandler(); 
            accessDeniedHandler.setExceptionRenderer(headerOnlyExceptionRender); 
            return accessDeniedHandler; 
        } 

        @Bean 
        public OAuth2AuthenticationEntryPoint clientAuthenticationEntryPoint() { 
            OAuth2AuthenticationEntryPoint authenticationEntryPoint = new OAuth2AuthenticationEntryPoint(); 

            authenticationEntryPoint.setRealmName("sparklr2/client"); 
            authenticationEntryPoint.setTypeName("Basic"); 
            authenticationEntryPoint 
                    .setExceptionRenderer(headerOnlyExceptionRender); 
            return authenticationEntryPoint; 
        } 
    } 

    @Configuration 
    @EnableAuthorizationServer 
    protected static class AuthorizationServerConfiguration extends 
            AuthorizationServerConfigurerAdapter { 

        @Autowired 
        private TokenStore tokenStore; 

        @Autowired 
        private AuthenticationManager authenticationManager; 

        public void configure(ClientDetailsServiceConfigurer clients) 
                throws Exception { 
            final String scopes[] = "read,write,trust".split(","); 
            final String secret = "123456"; 
            final String[] authorizedGrantTypes = { "password", 
                    "authorization_code", "refresh_token" }; 
            final String authorities = "ROLE_USER"; 
            clients.inMemory().withClient("android-crm") 
                    .resourceIds(applicationName).scopes(scopes) 
                    .authorities(authorities) 
                    .authorizedGrantTypes(authorizedGrantTypes).secret(secret) 
                    .and().withClient("ios-crm").resourceIds(applicationName) 
                    .scopes(scopes).authorities(authorities) 
                    .authorizedGrantTypes(authorizedGrantTypes).secret(secret); 
        } 

        @Override 
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) 
                throws Exception { 
            endpoints.tokenStore(tokenStore).authenticationManager( 
                    authenticationManager); 
        } 

        @Override 
        public void configure(AuthorizationServerSecurityConfigurer oauthServer) 
                throws Exception { 
            oauthServer.realm("sparklr2/client"); 
        } 

        @Bean 
        public ApprovalStore approvalStore() throws Exception { 
            TokenApprovalStore store = new TokenApprovalStore(); 
            store.setTokenStore(tokenStore); 
            return store; 
        } 

        /* 
         * @Bean public OAuth2AccessDeniedHandler oauthAccessDeniedHandler(){ 
         * OAuth2AccessDeniedHandler accessDeniedHandler=new 
         * OAuth2AccessDeniedHandler(); 
         *  
         * return accessDeniedHandler; } 
         */ 

    } 

    @Bean 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
        return super.authenticationManagerBean(); 
    } 

    @Bean 
    PasswordEncoder passwordEncoder() { 
        return NoOpPasswordEncoder.getInstance(); 
    } 

    @Bean 
    TextEncryptor textEncryptor() { 
        return Encryptors.noOpText(); 
    } 

    @Bean 
    public UserDetailsService userDetailsServiceBean() throws Exception { 
        return super.userDetailsServiceBean(); 
    } 

    protected void configure(AuthenticationManagerBuilder authManagerBuilder) 
            throws Exception { 

        authManagerBuilder.userDetailsService(userDetailsServiceBean()) 
                .passwordEncoder(passwordEncoder()).and() 
                .inMemoryAuthentication().withUser("admin").password("admin") 
                .roles("ADMIN", "USER"); 
    } 

    @Bean 
    public TokenStore tokenStore() { 
        return new InMemoryTokenStore(); 
    } 
}
@EnableWebMvc 
@Configuration 
public class WebMVCConfiguration extends WebMvcConfigurationSupport { 
    @Bean 
    public ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver() { 
        ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver(); 
        exceptionResolver.setOrder(0); 
        exceptionResolver.setMessageConverters(messageConverters()); 
        return exceptionResolver; 
    } 

    @Bean 
    public List> messageConverters() { 
        List> messageConverters = new ArrayList>(); 
        messageConverters.add(jsonHttpMessageConverter()); 
//        messageConverters.add(stringHttpMessageConverter()); 
        messageConverters.add(xmlConverter()); 
        return messageConverters; 
    } 

    @Bean 
    public StringHttpMessageConverter stringHttpMessageConverter() { 
        return new StringHttpMessageConverter(); 
    } 

    @Bean 
    public MappingJackson2HttpMessageConverter jsonHttpMessageConverter() { 
        return new MappingJackson2HttpMessageConverter(); 
    } 

    @Bean 
    public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() { 
        return new ByteArrayHttpMessageConverter(); 
    } 

    @Bean 
    public RequestMappingHandlerAdapter requestMappingHandlerAdapter() { 
        RequestMappingHandlerAdapter adapter = super 
                .requestMappingHandlerAdapter(); 
        adapter.setOrder(0); 

        adapter.getMessageConverters().addAll(messageConverters()); 
        adapter.getMessageConverters().add(byteArrayHttpMessageConverter()); 
        return adapter; 

    } 

    @Bean 
    public Jaxb2Marshaller jaxbMarshaller() { 
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller(); 
        jaxb2Marshaller 
                .setPackagesToScan("org.bluez.logiczweb.core.models.vo.*"); 
        return jaxb2Marshaller; 
    } 

    @Bean 
    public MarshallingHttpMessageConverter xmlConverter() { 
        MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter( 
                jaxbMarshaller()); 
        converter.setSupportedMediaTypes(Arrays 
                .asList(MediaType.APPLICATION_XML)); 
        return converter; 
    } 

} 
作为JSON,我的回答是:

{
"access_token": "27f93b60-a2ab-4ae6-90c9-81124cc7d10b",
"token_type": "bearer",
"refresh_token": "6bb643cf-3eda-402a-bb6d-5f3b05e56bee",
"expires_in": 43199,
"scope": "read"
}
我希望它是XML

我的MVC配置如下:

@Configuration 
@EnableWebSecurity(debug = true) 
@ComponentScan(basePackages = { "org.bluez.logiczweb.config.security.handler" }) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 
    private final static String applicationName = "crm"; 

    @Configuration 
    @EnableResourceServer 
    protected static class ResourceServerConfiguration extends 
            ResourceServerConfigurerAdapter { 

        @Autowired 
        private HeaderOnlyOAuth2ExceptionRenderer headerOnlyExceptionRender; 

        @Override 
        public void configure(ResourceServerSecurityConfigurer resources) { 
            resources.resourceId(applicationName); 
        } 

        public void configure(HttpSecurity http) throws Exception { 

            http.csrf().disable(); 
            http.sessionManagement().sessionCreationPolicy( 
                    SessionCreationPolicy.STATELESS); 
            http.requestMatchers().and().authorizeRequests() 
                    .antMatchers("/rest/accounts") 
                    .access("#oauth2.hasScope('read')").and().httpBasic() 
                    .authenticationEntryPoint(clientAuthenticationEntryPoint()); 

            // // .apply(new OAuth2ServerConfigurer()) 
            // .tokenStore(new InMemoryTokenStore()) 
            // .resourceId(applicationName); 

        } 

        @Bean 
        public OAuth2AccessDeniedHandler oauthAccessDeniedHandler() { 
            OAuth2AccessDeniedHandler accessDeniedHandler = new OAuth2AccessDeniedHandler(); 
            accessDeniedHandler.setExceptionRenderer(headerOnlyExceptionRender); 
            return accessDeniedHandler; 
        } 

        @Bean 
        public OAuth2AuthenticationEntryPoint clientAuthenticationEntryPoint() { 
            OAuth2AuthenticationEntryPoint authenticationEntryPoint = new OAuth2AuthenticationEntryPoint(); 

            authenticationEntryPoint.setRealmName("sparklr2/client"); 
            authenticationEntryPoint.setTypeName("Basic"); 
            authenticationEntryPoint 
                    .setExceptionRenderer(headerOnlyExceptionRender); 
            return authenticationEntryPoint; 
        } 
    } 

    @Configuration 
    @EnableAuthorizationServer 
    protected static class AuthorizationServerConfiguration extends 
            AuthorizationServerConfigurerAdapter { 

        @Autowired 
        private TokenStore tokenStore; 

        @Autowired 
        private AuthenticationManager authenticationManager; 

        public void configure(ClientDetailsServiceConfigurer clients) 
                throws Exception { 
            final String scopes[] = "read,write,trust".split(","); 
            final String secret = "123456"; 
            final String[] authorizedGrantTypes = { "password", 
                    "authorization_code", "refresh_token" }; 
            final String authorities = "ROLE_USER"; 
            clients.inMemory().withClient("android-crm") 
                    .resourceIds(applicationName).scopes(scopes) 
                    .authorities(authorities) 
                    .authorizedGrantTypes(authorizedGrantTypes).secret(secret) 
                    .and().withClient("ios-crm").resourceIds(applicationName) 
                    .scopes(scopes).authorities(authorities) 
                    .authorizedGrantTypes(authorizedGrantTypes).secret(secret); 
        } 

        @Override 
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) 
                throws Exception { 
            endpoints.tokenStore(tokenStore).authenticationManager( 
                    authenticationManager); 
        } 

        @Override 
        public void configure(AuthorizationServerSecurityConfigurer oauthServer) 
                throws Exception { 
            oauthServer.realm("sparklr2/client"); 
        } 

        @Bean 
        public ApprovalStore approvalStore() throws Exception { 
            TokenApprovalStore store = new TokenApprovalStore(); 
            store.setTokenStore(tokenStore); 
            return store; 
        } 

        /* 
         * @Bean public OAuth2AccessDeniedHandler oauthAccessDeniedHandler(){ 
         * OAuth2AccessDeniedHandler accessDeniedHandler=new 
         * OAuth2AccessDeniedHandler(); 
         *  
         * return accessDeniedHandler; } 
         */ 

    } 

    @Bean 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
        return super.authenticationManagerBean(); 
    } 

    @Bean 
    PasswordEncoder passwordEncoder() { 
        return NoOpPasswordEncoder.getInstance(); 
    } 

    @Bean 
    TextEncryptor textEncryptor() { 
        return Encryptors.noOpText(); 
    } 

    @Bean 
    public UserDetailsService userDetailsServiceBean() throws Exception { 
        return super.userDetailsServiceBean(); 
    } 

    protected void configure(AuthenticationManagerBuilder authManagerBuilder) 
            throws Exception { 

        authManagerBuilder.userDetailsService(userDetailsServiceBean()) 
                .passwordEncoder(passwordEncoder()).and() 
                .inMemoryAuthentication().withUser("admin").password("admin") 
                .roles("ADMIN", "USER"); 
    } 

    @Bean 
    public TokenStore tokenStore() { 
        return new InMemoryTokenStore(); 
    } 
}
@EnableWebMvc 
@Configuration 
public class WebMVCConfiguration extends WebMvcConfigurationSupport { 
    @Bean 
    public ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver() { 
        ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver(); 
        exceptionResolver.setOrder(0); 
        exceptionResolver.setMessageConverters(messageConverters()); 
        return exceptionResolver; 
    } 

    @Bean 
    public List> messageConverters() { 
        List> messageConverters = new ArrayList>(); 
        messageConverters.add(jsonHttpMessageConverter()); 
//        messageConverters.add(stringHttpMessageConverter()); 
        messageConverters.add(xmlConverter()); 
        return messageConverters; 
    } 

    @Bean 
    public StringHttpMessageConverter stringHttpMessageConverter() { 
        return new StringHttpMessageConverter(); 
    } 

    @Bean 
    public MappingJackson2HttpMessageConverter jsonHttpMessageConverter() { 
        return new MappingJackson2HttpMessageConverter(); 
    } 

    @Bean 
    public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() { 
        return new ByteArrayHttpMessageConverter(); 
    } 

    @Bean 
    public RequestMappingHandlerAdapter requestMappingHandlerAdapter() { 
        RequestMappingHandlerAdapter adapter = super 
                .requestMappingHandlerAdapter(); 
        adapter.setOrder(0); 

        adapter.getMessageConverters().addAll(messageConverters()); 
        adapter.getMessageConverters().add(byteArrayHttpMessageConverter()); 
        return adapter; 

    } 

    @Bean 
    public Jaxb2Marshaller jaxbMarshaller() { 
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller(); 
        jaxb2Marshaller 
                .setPackagesToScan("org.bluez.logiczweb.core.models.vo.*"); 
        return jaxb2Marshaller; 
    } 

    @Bean 
    public MarshallingHttpMessageConverter xmlConverter() { 
        MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter( 
                jaxbMarshaller()); 
        converter.setSupportedMediaTypes(Arrays 
                .asList(MediaType.APPLICATION_XML)); 
        return converter; 
    } 

} 

请帮助我。

封送HttpMessageConverter
不够智能,无法转换访问令牌。这就是为什么SpringOAuth有一个
JaxbOAuth2AccessTokenMessageConverter
(还有一个
JaxbOAuth2ExceptionMessageConverter
)。您需要这些,并且在
/token
端点安全性中使用的
OAuth2AuthenticationEntryPoint
OAuth2AccessDeniedHandler
中也需要它们(假设您的客户端也希望以XML呈现安全错误)。当前的
@EnableAuthorizationServer
支持允许您注入自己的
AuthenticationEntryPoint
,但您需要自己扩展
AuthorizationServerSecurityConfiguration
来覆盖
AccessDeniedHandler