java - HTTP header 中的 Spring OAuth2 访问 token

标签 java spring oauth spring-security spring-security-oauth2

我正在尝试使用应用程序中的授权服务器和独立应用程序中的资源服务器来实现 Spring Oauth2 设置。

我会从一开始就说,由于当前的系统限制,我不得不使用旧版本的 Spring/Spring Security,直到我们可以计划系统升级为止:

  • Spring :3.1.1 Spring
  • 安全:3.2.7
  • Spring OAuth:1.0.5

我一切正常,但是,当我从资源服务器请求受限资源时,我必须提供 access_token 作为查询参数。

我更愿意将其作为 HTTP header 提供。 Spring Security Oauth 中是否有内置功能可以执行此操作?

资源服务器配置

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

    <!-- Resource Server Filter -->
    <oauth:resource-server id="resourceServerFilter" resource-id="someResourceId" token-services-ref="tokenServices"/>
    <!-- END Resource Server Filter -->

    <!-- Protected resources -->
    <http pattern="/rest/BasicService/**" create-session="stateless" xmlns="http://www.springframework.org/schema/security" entry-point-ref="oauthAuthenticationEntryPoint">  
        <anonymous enabled="false"/>
        <intercept-url pattern="/rest/BasicService/**" access="ROLE_USER"/>
        <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER"/>
        <access-denied-handler ref="oauthAccessDeniedHandler"/> 
    </http>   
    <!-- END Protected resources -->

    <!-- Authentication -->
    <sec:authentication-manager xmlns="http://www.springframework.org/schema/security" /> 
    <bean id="oauthAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint" xmlns="http://www.springframework.org/schema/beans"/>
    <bean id="oauthAccessDeniedHandler" class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler"/>
    <!-- END Authentication -->         

    <!-- Token Store -->    
    <bean id="tokenStore" class="org.springframework.security.oauth2.provider.token.JdbcTokenStore">
        <constructor-arg ref="myJdbcTokenStore" />
    </bean>

    <bean id="myJdbcTokenStore" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/someJNDIDatabaseName"/>
    </bean>

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

</beans>

最佳答案

正如 prtk_shah 在评论中提到的, token 类型需要添加到授权 header 中。

授权:不记名

关于java - HTTP header 中的 Spring OAuth2 访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30348237/

相关文章:

java - AsynchronousByteChannel 的线程含义

java - Spring 测试MVC : Testing controller that redirects to another controller

spring - 将Spring Boot应用程序提交给Spark时出错

java - Spring容器引入aspect后不注入(inject)依赖

java - Java 所有原始类型的参数?

python - Twitter Oauth 问题——我需要一个网站来注册一个应用程序

java - NoClassDefFoundError:com/google/api/client/auth/oauth2/Credential

node.js - NodeJS 实际上使用 OAuth (Twitter)

java - fragment.isVisible() 总是返回 false

java - 在Spring MVC中设置拦截器