java oauth2 client,Spring OAuth2 - Client Credentials授权类型中的用户信息

我一直在开发Spring Cloud(使用Netflix OSS堆栈)微服务架构 . 正如您所料,我将授权服务器分离为独立的微服务 . 我的前端应用程序使用“密码”授予类型进行用户登录 . 但是,我正在使用“客户端凭据”授权类型来进行从前端服务到其他后端服务的其余调用 . 客户端凭证授权类型也在其他后端服务中使用 . 通过这样做,我无法得到谁是请求的实际调用者(当前登录用户) . 有没有办法将主体的身份验证和授权信息注入到客户端凭据授予中发出的令牌?

我的授权服务器配置类

@Configuration

@EnableAuthorizationServer

@Order(Ordered.HIGHEST_PRECEDENCE)

public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {

@Autowired

private AuthenticationManager authenticationManager;

@Override

public void configure(final AuthorizationServerSecurityConfigurer oauthServer) throws Exception {

oauthServer.tokenKeyAccess("permitAll()")

.checkTokenAccess("isAuthenticated()");

}

@Override

public void configure(final ClientDetailsServiceConfigurer clients) throws Exception {

clients.inMemory()

.withClient("testclient")

.secret("{noop}testsecret")

.authorizedGrantTypes("authorization_code","password","client_credentials")

.scopes("ui")

.autoApprove(true)

// .accessTokenValiditySeconds(3600)

.and()

.withClient("backend-service")

.secret("{noop}backendsecret")

.authorizedGrantTypes("client_credentials","refresh_token")

.scopes("server")

.autoApprove(true)

}

@Override

public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

endpoints.authenticationManager(authenticationManager);

endpoints.tokenEnhancer(tokenEnhancer());

endpoints.tokenStore(tokenStore());

}

@Bean

public TokenStore tokenStore() {

//return new JdbcTokenStore(dataSource);

return new InMemoryTokenStore();

}

@Bean

@Primary

public AuthorizationServerTokenServices tokenServices() {

DefaultTokenServices tokenServices = new DefaultTokenServices();

tokenServices.setTokenEnhancer(tokenEnhancer());

tokenServices.setTokenStore(tokenStore());

return tokenServices;

}

@Bean

public TokenEnhancer tokenEnhancer() {

return new CustomTokenEnhancer();

}

安全配置类

@Configuration

@Order(1)

public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override

protected void configure(HttpSecurity http) throws Exception {

http.csrf().disable()

.requestMatchers()

.antMatchers("/login", "/oauth/authorize")

.and()

.authorizeRequests()

.antMatchers("/resources/**", "/src/main/webapp/**","/css/**","/images/**").permitAll()

.and()

.formLogin()

.loginPage("/login")

.permitAll()

.and()

.logout()

.permitAll().and().httpBasic().disable();

}

@Override

public void configure(WebSecurity web) throws Exception {

web.ignoring().antMatchers("/static/**","/resources/**", "/src/main/webapp/**","/css/**","/images/**");

}

@Override

@Bean

public AuthenticationManager authenticationManagerBean() throws Exception {

return super.authenticationManagerBean();

}

@Override

protected void configure(final AuthenticationManagerBuilder auth) throws Exception {

auth.inMemoryAuthentication().withUser("admin").password("{noop}a1b2c3#").roles("User");

}

}

我试图实现一个Token Enhancer类来传播令牌中的其他数据 . 但是,我不认为这是我正在努力实现的正确和安全的方式 .

public class CustomTokenEnhancer implements TokenEnhancer {

@Override

public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {

final Map additionalInfo = new HashMap<>();

additionalInfo.put("customInfo", "testdata");

((DefaultOAuth2AccessToken) oAuth2AccessToken).setAdditionalInformation(additionalInfo);

return oAuth2AccessToken;

}

}

非常感谢您的协助 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spring-security-oauth2-authorization-server是一个基于Spring Security的OAuth 2.0授权服务器,用于为客户端提供安全的访问资源的授权服务。您可以按照以下步骤使用该库: 1. 在您的Spring Boot项目添加以下依赖项: ```xml <dependency> <groupId>org.springframework.security.oauth.boot</groupId> <artifactId>spring-security-oauth2-autoconfigure</artifactId> <version>2.1.0.RELEASE</version> </dependency> ``` 2. 配置您的授权服务器 在您的Spring Boot应用程序创建一个配置类,并使用@EnableAuthorizationServer注释启用授权服务器。例如: ```java @Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { // ... } ``` 3. 配置您的客户端 您可以使用ClientDetailsServiceConfigurer配置您的客户端。例如: ```java @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("client") .secret("{noop}secret") .authorizedGrantTypes("authorization_code", "refresh_token") .scopes("read", "write") .autoApprove(true) .redirectUris("http://localhost:8080/login/oauth2/code/") .and() .withClient("resource") .secret("{noop}secret") .authorizedGrantTypes("client_credentials") .scopes("read"); } ``` 上面的代码将在内存配置两个客户端:一个用于授权授权,另一个用于客户端凭证授权。 4. 配置您的用户 您可以使用UserDetailsServiceConfigurer配置您的用户。例如: ```java @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager) .userDetailsService(userDetailsService); } ``` 上面的代码将使用authenticationManager进行身份验证,并使用userDetailsService获取用户信息。 5. 启动您的应用程序 现在,您可以启动您的应用程序,并访问http://localhost:8080/oauth/authorize以获取授权码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值