springsecurity+oauth2.0 分布式认证授权-order资源服务器的配置4

一 注解 @EnableResourceServer 的配置

1.1 配置资源服务

1.1.1 使用

使用注解@EnableResourceServer 注解到一个 @Configuration 配置类上,并且必须使用 ResourceServerConfigurer 这个 配置对象来进行配置(可以选择继承自 ResourceServerConfigurerAdapter 然后覆写其中的方法,参数就是这个 对象的实例)

1.1.2 属性介绍

下面是一些可以配置的属性:
ResourceServerSecurityConfigurer 中主要包括:
tokenServices ResourceServerTokenServices 类的实例,用来实现令牌服务。
tokenStore TokenStore 类的实例,指定令牌如何访问,与 tokenServices 配置可选
resourceId :这个资源服务的 ID ,这个属性是可选的,但是推荐设置并在授权服务中进行验证。
其他的拓展属性例如 tokenExtractor 令牌提取器用来提取请求中的令牌。
HttpSecurity 配置与 Spring Security 类似: 请求匹配器,用来设置需要进行保护的资源路径,默认的情况下是保护资源服务的全部路径。
通过 http.authorizeRequests() 来设置受保护资源的访问规则 ,其他的自定义权限保护规则通过 HttpSecurity 来进行配置。
@EnableResourceServer 注解自动增加了一个类型为 OAuth2AuthenticationProcessingFilter 的过滤器链

1.1.3 配置操作

新建一个ResouceServerConfig配置类,加上注解 @EnableResourceServer

 1.2 验证令牌token

1.2.1 属性配置

ResourceServerTokenServices 是组成授权服务的另一半。
1. 如果你的授权服务和资源服务在同一个应用程序上的 话,你可以使用 DefaultTokenServices ,这样的话,你就不用考虑关于实现所有必要的接口的一致性问题。
2. 如果你的授权服务和资源服务是分离开的,那么你就必须要确保能够 匹配授权服务提供的 ResourceServerTokenServices 它知道如何对令牌进行解码
3.令牌解析方法: 使用 DefaultTokenServices 在资源服务器本地配置令牌存储、解码、解析方式。
4. 使用 RemoteTokenServices 资源服务器通过 HTTP 请求来解码令牌, 每次都请求授权服务器端点 /oauth/check_token,使用授权服务的 /oauth/check_token 端点,你需要在授权服务将这个端点暴露出去,以便资源服务可以进行访问
我们在授权服务(spt-ds-uaa-server )中配置了 /oauth/check_token 和 /oauth/token_key 这两个端点

1.2.2 代码操作

在资源 服务配置 RemoteTokenServices ,在 ResouceServerConfifig 中配置:

 1.3 配置安装策略

  1.4 编写资源

 1.5 启动服务测试

1.5 .1 启动服务

1.5 .2 使用授权码方式获取token

1.访问:http://localhost:53020/uaa/oauth/authorize?client_id=c1&response_type=code&scope=all&redirect_uri=http://www.baidu.com

2.登录

 3.授权

4.授权码:fkg8a5

 5.获取token:67a44089-8343-48a5-ba6d-60df43d03cd7

 1.5 .3 携带token访问资源

按照 oauth2.0 协议要求,请求资源需要携带 token ,如下:
token 的参数名称为: Authorization ,值为: Bearer token
访问地址:
请求方式:GET
参数:Authorization       Bearer 67a44089-8343-48a5-ba6d-60df43d03cd7
Bearer+空格 +上面求得的token值

 1.5 .4  验证token

http://localhost:53020/uaa/oauth/check_token

方式:post

参数:  token=67a44089-8343-48a5-ba6d-60df43d03cd7

 1.5 .5  遇到坑

启动类放到了和controller平级的子目录里,造成访问资源不成功!

 后来发现此问题,将controller,config等目录均放到oauth目录下,将启动类OrderApp和这些目录作为平级,成功解决了问题:

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,你想了解如何在Spring Boot中整合Spring Security和OAuth 2.0来实现token认证。下面是一个基本的步骤: 1. 添加依赖:在你的`pom.xml`文件中添加Spring Security和OAuth 2.0的依赖。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> </dependency> ``` 2. 配置Spring Security:创建一个继承自`WebSecurityConfigurerAdapter`的配置类,并覆盖`configure`方法,配置Spring Security。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/oauth2/**", "/login/**", "/logout/**") .permitAll() .anyRequest() .authenticated() .and() .oauth2Login() .loginPage("/login") .and() .logout() .logoutUrl("/logout") .logoutSuccessUrl("/") .deleteCookies("JSESSIONID"); } } ``` 这个配置类指定了哪些URL需要进行认证,`antMatchers`方法指定了不需要认证的URL。 3. 配置OAuth 2.0客户端:创建一个继承自`WebSecurityConfigurerAdapter`的配置类,并使用`@EnableOAuth2Client`注解开启OAuth 2.0客户端。 ```java @Configuration @EnableOAuth2Client public class OAuth2ClientConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { // 配置HTTP Security } @Bean public OAuth2AuthorizedClientService authorizedClientService( OAuth2ClientRegistrationRepository clientRegistrationRepository) { return new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository); } } ``` 这个配置类可以用来配置OAuth 2.0的客户端,你可以在`configure`方法中添加一些额外的配置。 4. 配置OAuth 2.0客户端注册:在`application.properties`文件中配置OAuth 2.0的客户端注册信息。 ```properties spring.security.oauth2.client.registration.my-client-id.client-id=your-client-id spring.security.oauth2.client.registration.my-client-id.client-secret=your-client-secret spring.security.oauth2.client.registration.my-client-id.scope=your-scopes spring.security.oauth2.client.registration.my-client-id.authorization-grant-type=authorization_code spring.security.oauth2.client.registration.my-client-id.redirect-uri=your-redirect-uri ``` 这个配置文件中的`my-client-id`是你自己指定的客户端ID,你需要将其替换为你自己的信息。 这些步骤是实现Spring Boot中整合Spring Security和OAuth 2.0实现token认证的基本步骤。你可以根据自己的需求进行进一步的配置和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值