java - Spring Security OAuth2 纯资源服务器

标签 java spring spring-security oauth-2.0

我们已经搭建了一个OAuth2授权服务器,所以我需要创建一个对应的资源服务器(单独的服务器)。我们计划使用 Spring Security OAuth2 项目。他们设置资源服务器的文档:

https://github.com/spring-projects/spring-security-oauth/wiki/oAuth2#resource-server-configuration

token-services-ref 应该指向 token 处理 bean。然而, token 处理似乎是由服务器本身完成的,即使它是资源服务器。似乎没有任何远程 token 服务类或与远程服务器相关的任何配置。这与 CloudFoundary UAA (https://github.com/cloudfoundry/uaa/blob/master/samples/api/src/main/webapp/WEB-INF/spring-servlet.xml) 形成对比,后者具有:

<bean id="tokenServices"
  class="org.cloudfoundry.identity.uaa.oauth.RemoteTokenServices">
  <property name="checkTokenEndpointUrl" value="${checkTokenEndpointUrl}" />

有没有办法将 Spring Security OAuth2 用于与单独的 OAuth2 授权服务器通信的资源服务器?如何设置通信端点?

最佳答案

只要授权服务器和资源服务器访问共享的 tokenStore(例如,将 JdbcTokenStore 与公共(public)的 dataSource 一起使用),这是可能的>)。您可以使用 DefaultTokenServices 来引用您的共享 tokenStore。下面是一个示例 Spring 配置,您应该可以对其进行调整以满足您的需求:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:security="http://www.springframework.org/schema/security"
   xmlns:oauth2="http://www.springframework.org/schema/security/oauth2"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.1.xsd
    http://www.springframework.org/schema/security/oauth2
    http://www.springframework.org/schema/security/spring-security-oauth2.xsd">

<bean id="tokenStore" class="org.springframework.security.oauth2.provider.token.JdbcTokenStore">
    <constructor-arg name="dataSource" ref="dataSource" />
</bean>

<bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">
    <property name="tokenStore" ref="tokenStore" />
</bean>

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

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

<bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased">
    <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>

<!-- This is not actually used, but it's required by Spring Security -->
<security:authentication-manager alias="authenticationManager" />

<oauth2:expression-handler id="oauthExpressionHandler" />

<oauth2:web-expression-handler id="oauthWebExpressionHandler" />

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

<oauth2:resource-server id="myResource" resource-id="myResourceId" token-services-ref="tokenServices" />

<security:http pattern="/myPattern/**" create-session="never"
    entry-point-ref="authenticationEntryPoint" access-decision-manager-ref="accessDecisionManager">
    <security:anonymous enabled="false" />
    <security:intercept-url pattern="/**" access="SCOPE_READ" method="GET" />
    <security:intercept-url pattern="/**" access="SCOPE_READ" method="HEAD" />
    <security:intercept-url pattern="/**" access="SCOPE_READ" method="OPTIONS" />
    <security:intercept-url pattern="/**" access="SCOPE_WRITE" method="PUT" />
    <security:intercept-url pattern="/**" access="SCOPE_WRITE" method="POST" />
    <security:intercept-url pattern="/**" access="SCOPE_WRITE" method="DELETE" />
    <security:custom-filter ref="myResource" before="PRE_AUTH_FILTER" />
    <security:access-denied-handler ref="oauthAccessDeniedHandler" />
    <security:expression-handler ref="oauthWebExpressionHandler" />
</security:http>
</beans>

关于java - Spring Security OAuth2 纯资源服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20384543/

相关文章:

java - 即使应用程序关闭也运行时钟或计时器 - Android

java - 将网络驱动器上的源编译到本地驱动器上

Java awt 复选框构造函数

java - 可反序列化异常: local class is not compatible cause of serialVersionID

mustache 的 Java Spring 不断在服务器上寻找文件

spring - POST 请求 "Full authentication is required to access this resource"

spring-security - 从数据库检索的 HTML 显示不正确

java - Mysql JDBC 驱动 ClassNotFoundException

grails - 无法在Grails中正确登录

java - 嵌入式 Jetty 环境中的 Spring Boot 和 Spring Security 集成