oAuth2 feign 授权模式

简介: 客户端模式bootstrap.ymlsecurity: oauth2: client: clientId: paascloud-browser ...

客户端模式

bootstrap.yml

security:
  oauth2:
    client:
      clientId: paascloud-browser
      clientSecret: paascloudClientSecret
      access-token-uri: http://localhost:7979/uac/oauth/token
      user-authorization-uri: http://localhost:7979/uac/oauth/authorize
    resource:
      id: browser-service
      user-info-uri: http://localhost:7979/uac/user
paascloud:
  oauth2:
    client:
      id: ${security.oauth2.resource.id}
      accessTokenUrl: http://localhost:7979/uac/oauth/token
      clientId: ${security.oauth2.client.clientId}
      clientSecret: ${security.oauth2.client.clientSecret}
      clientAuthenticationScheme: header

Oauth2ClientConfig.java

@Configuration
@EnableConfigurationProperties(Oauth2ClientProperties.class)
public class Oauth2ClientConfig {

    @Autowired
    private Oauth2ClientProperties oauth2ClientProperties;

    @Bean("paascloudClientCredentialsResourceDetails")
    public ClientCredentialsResourceDetails resourceDetails() {
        ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
        details.setId(oauth2ClientProperties.getId());
        details.setAccessTokenUri(oauth2ClientProperties.getAccessTokenUrl());
        details.setClientId(oauth2ClientProperties.getClientId());
        details.setClientSecret(oauth2ClientProperties.getClientSecret());
        details.setAuthenticationScheme(AuthenticationScheme.valueOf(oauth2ClientProperties.getClientAuthenticationScheme()));
        return details;
    }

    @Bean("paascloudOAuth2RestTemplate")
    public OAuth2RestTemplate oAuth2RestTemplate() {
        final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails(), new DefaultOAuth2ClientContext());
        oAuth2RestTemplate.setRequestFactory(new Netty4ClientHttpRequestFactory());

        return oAuth2RestTemplate;

    }
}

Oauth2ClientProperties.java

@ConfigurationProperties(prefix = "paascloud.oauth2.client")
@Data
public class Oauth2ClientProperties {
    private String id;
    private String accessTokenUrl;
    private String clientId;
    private String clientSecret;
    private String clientAuthenticationScheme;
}

OAuth2FeignAutoConfiguration.java

@Configuration
public class OAuth2FeignAutoConfiguration {

    @Bean
    public RequestInterceptor oauth2FeignRequestInterceptor(@Qualifier("paascloudOAuth2RestTemplate") OAuth2RestTemplate oAuth2RestTemplate) {
        return new OAuth2FeignRequestInterceptor(oAuth2RestTemplate);
    }

    @Bean
    public RestClientErrorDecoder errorDecoder() {
        return new RestClientErrorDecoder();
    }

    @Bean
    public Contract feignContract() {
        return new feign.Contract.Default();
    }

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

OAuth2FeignRequestInterceptor.java

public class OAuth2FeignRequestInterceptor implements RequestInterceptor {

    private final Logger LOGGER = LoggerFactory.getLogger(getClass());

    private static final String AUTHORIZATION_HEADER = "Authorization";

    private static final String BEARER_TOKEN_TYPE = "Bearer";

    private final OAuth2RestTemplate oAuth2RestTemplate;


    public OAuth2FeignRequestInterceptor(OAuth2RestTemplate oAuth2RestTemplate) {
        Assert.notNull(oAuth2RestTemplate, "Context can not be null");
        this.oAuth2RestTemplate = oAuth2RestTemplate;
    }

    @Override
    public void apply(RequestTemplate template) {
        LOGGER.debug("Constructing Header {} for Token {}", AUTHORIZATION_HEADER, BEARER_TOKEN_TYPE);
        template.header(AUTHORIZATION_HEADER,
                String.format("%s %s",
                        BEARER_TOKEN_TYPE,
                        oAuth2RestTemplate.getAccessToken().toString()));

    }
}

密码模式

@Configuration
public class ConfigurationForRestClient {

    @Bean(name = "paascloudOAuth2ProtectedResourceDetails")
    protected OAuth2ProtectedResourceDetails resource() {

        ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
        resource.setAccessTokenUri("http://localhost:7979/uac/oauth/token");

        resource.setClientAuthenticationScheme(AuthenticationScheme.header);
        resource.setClientId("paascloud-browser");
        resource.setClientSecret("paascloudClientSecret");

        resource.setGrantType("password");
        resource.setScope(Arrays.asList("all"));

        resource.setUsername("admin");
        resource.setPassword("123456");

        return resource;
    }

    @Bean(name = "paascloudOauth2ClientContext")
    public OAuth2ClientContext oauth2ClientContext() {

        return new DefaultOAuth2ClientContext(new DefaultAccessTokenRequest());
    }

    @Bean
    public RestClientErrorDecoder errorDecoder() {

        return new RestClientErrorDecoder();
    }

    @Bean
    protected RequestInterceptor oauth2FeignRequestInterceptor(
            @Qualifier("paascloudOauth2ClientContext") OAuth2ClientContext context,
            @Qualifier("paascloudOAuth2ProtectedResourceDetails") OAuth2ProtectedResourceDetails resourceDetails) {

        return new OAuth2FeignRequestInterceptor(context, resourceDetails);
    }

    @Bean
    @Primary
    public OAuth2RestTemplate oauth2RestTemplate(
            @Qualifier("paascloudOauth2ClientContext") OAuth2ClientContext context,
            @Qualifier("paascloudOAuth2ProtectedResourceDetails") OAuth2ProtectedResourceDetails resourceDetails) {

        OAuth2RestTemplate template = new OAuth2RestTemplate(resourceDetails,
                context);

        return template;
    }

    @Bean
    public Contract feignContract() {
        return new feign.Contract.Default();
    }

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

简单记录一下日后有时间再整理

目录
相关文章
|
7月前
|
JSON 安全 Java
Spring Security 6.x 微信公众平台OAuth2授权实战
上一篇介绍了OAuth2协议的基本原理,以及Spring Security框架中自带的OAuth2客户端GitHub的实现细节,本篇以微信公众号网页授权登录为目的,介绍如何在原框架基础上定制开发OAuth2客户端。
264 4
Spring Security 6.x 微信公众平台OAuth2授权实战
|
存储 安全 Java
Spring Security 认证的三种方式及简单的授权
Spring Security 认证的三种方式及简单的授权
125 0
|
8月前
|
安全 Java Spring
Spring Security的授权&鉴权
该文档介绍了授权和鉴权的概念,主要分为Web授权和方法授权。Web授权通过URL拦截进行,而方法授权利用注解控制权限,粒度更细但耦合度高。在Web授权的案例中,展示了如何在Spring Security中对特定URL设置角色控制。此外,还列举了Spring Security内置的控制操作方法,如permitAll()、denyAll()和hasRole()等,用于定义不同类型的用户访问权限。
186 7
|
8月前
|
存储 Java Maven
SpringCloud Oauth2.0 实现资源验证
SpringCloud Oauth2.0 实现资源验证
78 0
|
8月前
|
安全 Java API
深度解析 Spring Security:身份验证、授权、OAuth2 和 JWT 身份验证的完整指南
Spring Security 是一个用于保护基于 Java 的应用程序的框架。它是一个功能强大且高度可定制的身份验证和访问控制框架,可以轻松地集成到各种应用程序中,包括 Web 应用程序和 RESTful Web 服务。 Spring Security 提供了全面的安全解决方案,用于身份验证和授权,并且可以用于在 Web 和方法级别上保护应用程序。
985 0
|
缓存 安全 JavaScript
Spring Cloud Gateway + Spring Security OAuth2 + JWT 实现统一认证授权和网关鉴权
Spring Cloud Gateway + Spring Security OAuth2 + JWT 实现统一认证授权和网关鉴权
|
存储 安全 JavaScript
Spring中的Websocket身份验证和授权
- http和WebSocket的安全链和安全配置是完全独立的。 - SpringAuthenticationProvider根本不参与 Websocket 身份验证。 - 将要给出的示例中,身份验证不会发生在 HTTP 协商端点上,因为 JavaScript STOMP(websocket)库不会随 HTTP 请求一起发送必要的身份验证标头。 - 一旦在 CONNECT 请求上设置,用户( simpUser) 将被存储在 websocket 会话中,并且以后的消息将不再需要进行身份验证。
370 0
|
存储 SQL 安全
十一.SpringCloud+Security+Oauth2实现微服务授权 - 授权服务配置
SpringCloud+Security+Oauth2实现微服务授权 - 授权服务配置
|
存储 JSON 缓存
九.SpringCloud+Security+Oauth2实现微服务授权 - Oauth2&JWT的认识
SpringCloud+Security+Oauth2实现微服务授权 - Oauth2&JWT的认识
|
负载均衡 Java 数据库
十三.SpringCloud+Security+Oauth2实现微服务授权 - 服务之间授权
SpringCloud+Security+Oauth2实现微服务授权 - 服务之间授权