带弹簧的OAUTH2使用注释

时间:2015-07-01 07:23:03

标签: java spring spring-security oauth-2.0 spring-annotations

任何人都可以给我一个简单的例子来使用注释实现带弹簧的OAUTH2吗?客户端将是Android应用程序。

我已经搜索了很多关于它的内容,没有简单的实现。

任何帮助都非常感激。

错误: 严重:上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.security.filterChains'的bean时出错:在设置bean属性'sourceList'时无法解析对bean'org.springframework.security.web.DefaultSecurityFilterChain#0'的引用用键[0];嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.security.web.DefaultSecurityFilterChain#0'的bean时出错:无法创建[org.springframework.security]类型的内部bean'(内部bean)' .web.context.request.async.WebAsyncManagerIntegrationFilter]用key [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)

按照我的尝试

其余-调度-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>

web.xml的安全部分

   <!--  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 -->

Spring Security Dependencies

<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>

控制器

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;
    }
}

0 个答案:

没有答案