当前位置: 首页 > 知识库问答 >
问题:

oauth2在身份验证后返回令牌的Java*配置是什么

阮喜
2023-03-14

嗨,我在我的项目中使用spring boot,所以我没有使用xml进行任何配置,只有Java。我在github上使用这个项目作为参考https://github.com/techdev-solutions/jaxenter-showcase。

当我对令牌发出请求(http://localhost:8081/oauth/authorize?client_id=web&response_type=token,标题中有用户名和密码)时,它返回重定向html站点,而不是令牌。如何配置oauth2以在响应中返回令牌。

如果我使用curl发送一个请求,它会给出我想要的结果:curl curl:password@localhost:8081/oauth/token\?grant_type=client_credentials

如果我试图通过http客户端http://localhost:8081/oauth/token模拟相同的请求?client_secret=password&client_id=curl&grant_type=client_credentials

我得到401未经授权

package de.techdev.jaxenter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;

import javax.sql.DataSource;

/**
 * @author Moritz Schulze
 */
@Configuration
@EnableAuthorizationServer
public class OAuthConfiguration extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Bean
    public TokenStore tokenStore() {
        return new JdbcTokenStore(dataSource);
    }

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

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
        .withClient("curl") //curl curl:password@localhost:8081/oauth/token\?grant_type=client_credentials
        .authorities("ROLE_ADMIN")
        .resourceIds("jaxenter")
        .scopes("read", "write")
        .authorizedGrantTypes("client_credentials")
        .secret("password")
        .and()
        .withClient("web") //http://localhost:8081/oauth/authorize?client_id=web&response_type=token
        .redirectUris("http://github.com/techdev-solutions/")
        .authorities("ROLE_ADMIN")
        .resourceIds("jaxenter")
        .scopes("read, write")
        //.authorizedGrantTypes("implicit")
        .authorizedGrantTypes("implicit","client_credentials")
        .autoApprove(true)
        .secret("password")
        .and()
        .withClient("my-trusted-client")
            .authorizedGrantTypes("password","authorization_code","refresh_token","implicit","redirect")
            .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
            .scopes("read", "write", "trust")
            .redirectUris("http://localhost:8080")
            .authorizedGrantTypes("implicit")
            .accessTokenValiditySeconds(60)
            .refreshTokenValiditySeconds(30);
    }
}

package de.techdev.jaxenter;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * @author Moritz Schulze
 */
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("John").roles("ADMIN").password("password")
            .and()
            .withUser("Mary").roles("BASIC").password("password");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/**").authenticated()
            .and().httpBasic().realmName("OAuth Server");
    }
}

共有1个答案

蓬威
2023-03-14

首先,您必须提供授予类型

例如,如果您已经用curl获取了令牌并试图访问资源,可以尝试在web浏览器beacuse中传递它,您不再需要身份验证,只需添加承载e2cb0291-596c-48e0-8e93-2b29b2881406(示例令牌)作为头部,这一次就可以工作了

 类似资料:
  • 我有一个LaravelAPI(实际上是LumenAPI)服务于VueJS前端。Vue应用程序允许用户登录到谷歌。然后将Google令牌发送回Lumen API,后者使用Google验证令牌,然后验证电子邮件地址是否为有效用户。然后它生成一个令牌,与用户一起存储在数据库中,并返回用户对象。 我没有使用Passport或jwt auth之类的东西。那么现在,我如何使用默认的Auth中间件来验证(现在已

  • 我在springboot 1.5.6.RELEASE中使用oauth2,在oauth2中使用jdbc认证。 我添加了属性: 1-授权服务器配置适配器: 2资源服务器配置 3-安全配置 我正在尝试使用postman生成一个令牌,并向url<code>发送post请求http://localhost:8082/dawam2/oauth/token?grant_type=password我使用基本身份验

  • 我刚刚开始在.NET中开发我的第一个REST API。由于它将是无状态的,我将使用令牌进行身份验证: 基本思想(System.Security.Cryptography): null 检查凭据是否有效(用户名,将哈希密码与db值进行比较) 如果为真,则加密数据对象 对生成的令牌使用HMAC,并将其存储到数据库 将令牌(不带HMAC)返回给用户(cookie/字符串) 对需要身份验证的方法的请求:

  • 我有一个REST Jersey web服务。 php中基于令牌的身份验证 它在答复中提到; “然后它发送此令牌和请求的某些特征的哈希来验证请求,例如sha1(令牌+时间戳+请求URL+请求正文)。您的服务器可以对此进行验证,而客户端不必在每个请求上以纯文本发送令牌。” 另一个问题是,一旦服务器接收到这个令牌的哈希值(包括时间戳和用户ID等),服务器如何在没有存储令牌的查找表或数据库的情况下从这个令

  • 有人在springboot应用程序中找到了这样的工作示例吗? /auth控制器,用户在其中提供ActiveDirectory凭据(通过基本身份验证或POST json)并在提供有效AD凭据的情况下接收JWT令牌。不应涉及LDIF文件,Springboot应用程序将根据ldaps://ActiveDirectoryhost: 636endpoint验证凭据 /myapi控制器仅在步骤1(上图)中的有