Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.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
Java OAUTH2与使用注释的spring_Java_Spring_Spring Security_Oauth 2.0_Spring Annotations - Fatal编程技术网

Java OAUTH2与使用注释的spring

Java OAUTH2与使用注释的spring,java,spring,spring-security,oauth-2.0,spring-annotations,Java,Spring,Spring Security,Oauth 2.0,Spring Annotations,有人能给我举个简单的例子,用spring使用注释实现OAUTH2吗?客户端将是android应用程序 我在谷歌上搜索了很多,没有简单的实现 非常感谢您的帮助 错误: 严重:上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.filterChains”的bean时出错:使用键[0]设置bean属性“sourceList”时,无法解析对bean“org.s

有人能给我举个简单的例子,用spring使用注释实现OAUTH2吗?客户端将是android应用程序

我在谷歌上搜索了很多,没有简单的实现

非常感谢您的帮助

错误: 严重:上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.filterChains”的bean时出错:使用键[0]设置bean属性“sourceList”时,无法解析对bean“org.springframework.security.web.DefaultSecurityFilterChain”的引用;嵌套异常为org.springframework.beans.factory.beancreatitionException:创建名为“org.springframework.security.web.DefaultSecurityFilterChain#0”的bean时出错:无法创建[org.springframework.security.web.context.request.async.WebAsyncManagerIntrangeFilter]类型的内部bean'(内部bean)]使用键[1]设置构造函数参数时;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“(内部bean)#2”的bean时出错:bean实例化失败;嵌套的异常是java.lang.NoClassDefFoundError:org/springframework/web/context/request/async/CallableProcessingInterceptor 位于org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)

遵循我尝试过的内容

rest调度器servlet security.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
           xmlns:sec="http://www.springframework.org/schema/security"
           xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.2.xsd
           http://www.springframework.org/schema/security/oauth2
           http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd">

        <http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="clientAuthenticationManager"
          xmlns="http://www.springframework.org/schema/security">
        <intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY"/>
        <anonymous enabled="false"/>
        <http-basic entry-point-ref="clientAuthenticationEntryPoint"/>
        <!-- include this only if you need to authenticate clients via request parameters -->
        <custom-filter ref="clientCredentialsTokenEndpointFilter" after="BASIC_AUTH_FILTER"/>
        <access-denied-handler ref="oauthAccessDeniedHandler"/>
        </http>

        <http pattern="/school/**"
          create-session="never"
          entry-point-ref="oauthAuthenticationEntryPoint"
          access-decision-manager-ref="accessDecisionManager"
          xmlns="http://www.springframework.org/schema/security">
        <anonymous enabled="false"/>
        <intercept-url pattern="/school/**"
                       access="ROLE_USER"/>
        <custom-filter ref="resourceServerFilter"
                       before="PRE_AUTH_FILTER"/>
        <access-denied-handler ref="oauthAccessDeniedHandler"/>
        </http>

        <bean id="oauthAuthenticationEntryPoint"
          class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
        <property name="realmName" value="dstest"/>
    </bean>

    <bean id="clientAuthenticationEntryPoint"
          class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
        <property name="realmName" value="dstest/client"/>
        <property name="typeName" value="Basic"/>
        </bean>

        <bean id="oauthAccessDeniedHandler"
          class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler"/>

        <bean id="clientCredentialsTokenEndpointFilter"
          class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
        <property name="authenticationManager" ref="clientAuthenticationManager"/>
        </bean>

        <bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased"
          xmlns="http://www.springframework.org/schema/beans">
        <constructor-arg>
            <list>
                <bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter"/>
                <bean class="org.springframework.security.access.vote.RoleVoter"/>
                <bean class="org.springframework.security.access.vote.AuthenticatedVoter"/>
            </list>
        </constructor-arg>
        </bean>

        <authentication-manager id="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security">
        <authentication-provider user-service-ref="clientDetailsUserService"/>
        </authentication-manager>

        <authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security">
        <authentication-provider>
            <user-service id="userDetailsService">
                <user name="admin" password="password" authorities="ROLE_USER"/>
            </user-service>
            </authentication-provider>
        </authentication-manager>

        <bean id="clientDetailsUserService"
          class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
        <constructor-arg ref="clientDetails"/>
        </bean>

        <bean id="tokenStore"     class="org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore"/>

        <bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">    
            <property name="tokenStore" ref="tokenStore"/>
            <property name="supportRefreshToken" value="true"/>
            <property name="clientDetailsService" ref="clientDetails"/>
            <!-- VIV -->
        <property name="accessTokenValiditySeconds" value="10"/>
        </bean>

        <bean id="userApprovalHandler"
          class="org.springframework.security.oauth2.provider.approval.TokenStoreUserApprovalHandler">
        <property name="tokenServices" ref="tokenServices"/>
        </bean>

        <oauth:authorization-server client-details-service-ref="clientDetails" token-services-ref="tokenServices"
                                user-approval-handler-ref="userApprovalHandler">
        <oauth:authorization-code/>
        <oauth:implicit/>
        <oauth:refresh-token/>
        <oauth:client-credentials/>
        <oauth:password/>
        </oauth:authorization-server>

        <oauth:resource-server id="resourceServerFilter"
                           resource-id="dstest"
                           token-services-ref="tokenServices"/>

        <oauth:client-details-service id="clientDetails">

        <oauth:client client-id="my-trusted-client"
                      authorized-grant-types="password,authorization_code,refresh_token,implicit,redirect"
                      authorities="ROLE_CLIENT, ROLE_TRUSTED_CLIENT"
                      redirect-uri="/web"
                      scope="read,write,trust"
                      access-token-validity="30"
                      refresh-token-validity="600"/>

        </oauth:client-details-service>

        <sec:global-method-security pre-post-annotations="enabled" proxy-    target-class="true">
        <sec:expression-handler ref="oauthExpressionHandler"/>
        </sec:global-method-security>
        <oauth:expression-handler id="oauthExpressionHandler"/>
        <oauth:web-expression-handler id="oauthWebExpressionHandler"/>

        </beans>
   <!--  Spring security -->
     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/rest-dispatcher-servlet-security.xml
        </param-value>
    </context-param>

     <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
   <!-- end of Spring security -->

web.xml的安全部分

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
           xmlns:sec="http://www.springframework.org/schema/security"
           xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.2.xsd
           http://www.springframework.org/schema/security/oauth2
           http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd">

        <http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="clientAuthenticationManager"
          xmlns="http://www.springframework.org/schema/security">
        <intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY"/>
        <anonymous enabled="false"/>
        <http-basic entry-point-ref="clientAuthenticationEntryPoint"/>
        <!-- include this only if you need to authenticate clients via request parameters -->
        <custom-filter ref="clientCredentialsTokenEndpointFilter" after="BASIC_AUTH_FILTER"/>
        <access-denied-handler ref="oauthAccessDeniedHandler"/>
        </http>

        <http pattern="/school/**"
          create-session="never"
          entry-point-ref="oauthAuthenticationEntryPoint"
          access-decision-manager-ref="accessDecisionManager"
          xmlns="http://www.springframework.org/schema/security">
        <anonymous enabled="false"/>
        <intercept-url pattern="/school/**"
                       access="ROLE_USER"/>
        <custom-filter ref="resourceServerFilter"
                       before="PRE_AUTH_FILTER"/>
        <access-denied-handler ref="oauthAccessDeniedHandler"/>
        </http>

        <bean id="oauthAuthenticationEntryPoint"
          class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
        <property name="realmName" value="dstest"/>
    </bean>

    <bean id="clientAuthenticationEntryPoint"
          class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
        <property name="realmName" value="dstest/client"/>
        <property name="typeName" value="Basic"/>
        </bean>

        <bean id="oauthAccessDeniedHandler"
          class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler"/>

        <bean id="clientCredentialsTokenEndpointFilter"
          class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
        <property name="authenticationManager" ref="clientAuthenticationManager"/>
        </bean>

        <bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased"
          xmlns="http://www.springframework.org/schema/beans">
        <constructor-arg>
            <list>
                <bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter"/>
                <bean class="org.springframework.security.access.vote.RoleVoter"/>
                <bean class="org.springframework.security.access.vote.AuthenticatedVoter"/>
            </list>
        </constructor-arg>
        </bean>

        <authentication-manager id="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security">
        <authentication-provider user-service-ref="clientDetailsUserService"/>
        </authentication-manager>

        <authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security">
        <authentication-provider>
            <user-service id="userDetailsService">
                <user name="admin" password="password" authorities="ROLE_USER"/>
            </user-service>
            </authentication-provider>
        </authentication-manager>

        <bean id="clientDetailsUserService"
          class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
        <constructor-arg ref="clientDetails"/>
        </bean>

        <bean id="tokenStore"     class="org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore"/>

        <bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">    
            <property name="tokenStore" ref="tokenStore"/>
            <property name="supportRefreshToken" value="true"/>
            <property name="clientDetailsService" ref="clientDetails"/>
            <!-- VIV -->
        <property name="accessTokenValiditySeconds" value="10"/>
        </bean>

        <bean id="userApprovalHandler"
          class="org.springframework.security.oauth2.provider.approval.TokenStoreUserApprovalHandler">
        <property name="tokenServices" ref="tokenServices"/>
        </bean>

        <oauth:authorization-server client-details-service-ref="clientDetails" token-services-ref="tokenServices"
                                user-approval-handler-ref="userApprovalHandler">
        <oauth:authorization-code/>
        <oauth:implicit/>
        <oauth:refresh-token/>
        <oauth:client-credentials/>
        <oauth:password/>
        </oauth:authorization-server>

        <oauth:resource-server id="resourceServerFilter"
                           resource-id="dstest"
                           token-services-ref="tokenServices"/>

        <oauth:client-details-service id="clientDetails">

        <oauth:client client-id="my-trusted-client"
                      authorized-grant-types="password,authorization_code,refresh_token,implicit,redirect"
                      authorities="ROLE_CLIENT, ROLE_TRUSTED_CLIENT"
                      redirect-uri="/web"
                      scope="read,write,trust"
                      access-token-validity="30"
                      refresh-token-validity="600"/>

        </oauth:client-details-service>

        <sec:global-method-security pre-post-annotations="enabled" proxy-    target-class="true">
        <sec:expression-handler ref="oauthExpressionHandler"/>
        </sec:global-method-security>
        <oauth:expression-handler id="oauthExpressionHandler"/>
        <oauth:web-expression-handler id="oauthWebExpressionHandler"/>

        </beans>
   <!--  Spring security -->
     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/rest-dispatcher-servlet-security.xml
        </param-value>
    </context-param>

     <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
   <!-- end of Spring security -->

上下文配置位置
/WEB-INF/rest-dispatcher-servlet-security.xml
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
Spring安全依赖项

<dependency>
    <groupId>org.springframework.security.oauth</groupId>
    <artifactId>spring-security-oauth2</artifactId>
    <version>2.0.7.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
    <version>4.0.1.RELEASE</version>
</dependency>

org.springframework.security.oauth
spring-security-oauth2
2.0.7.1发布
org.springframework.security
spring安全内核
4.0.1.1发布
控制器

package com.school.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.school.bo.school.SchoolBoImpl;
import com.school.custom.pojo.SchoolListingRequest;
import com.school.errorhandling.WebServiceException;
import com.school.model.School;

@Controller
@RequestMapping("/school")
public class SchoolController// extends ExceptionHandlerController
{
    @Autowired
    SchoolBoImpl schoolhome;

    @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
     public List<School> fetchAll() {
            System.out.println("SchoolDao: fetchAll");
            List<School> fetchedSchool = schoolhome.fetchAllSchool();
            return fetchedSchool;
        }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public School fetchById(@PathVariable int id) throws WebServiceException {
        School fetchedSchool = schoolhome.fetchSchoolById(id);
        if(fetchedSchool==null){
               throw new WebServiceException("NOT FOUND");
        }
        return fetchedSchool;
    }
}
包com.school.controller;
导入java.util.List;
导入java.util.Map;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.ResponseBody;
导入com.school.bo.school.SchoolBoImpl;
导入com.school.custom.pojo.SchoolListingRequest;
导入com.school.errorhandling.WebServiceException;
导入com.school.model.school;
@控制器
@请求映射(“/school”)
公共类SchoolController//Extended ExceptionHandlerController
{
@自动连线
学校之家;
@RequestMapping(value=”“,method=RequestMethod.GET,products=“application/json”)
@应答器
公共列表fetchAll(){
System.out.println(“SchoolDao:fetchAll”);
List fetchedSchool=schoolhome.fetchAllSchool();
返校;
}
@RequestMapping(value=“/{id}”,method=RequestMethod.GET,products=“application/json”)
@应答器
公立学校fetchById(@PathVariable int id)引发WebServiceException{
School fetchedSchool=schoolhome.fetchSchoolById(id);
if(fetchedSchool==null){
抛出新的WebServiceException(“未找到”);
}
返校;
}
}

请发布您已经尝试过的内容。相应地更新了问题。您整理好了吗?我现在正在调查。你找到解决办法了吗?