OAuth2中的Token

两个不同的Token

OAuth2 中主要有两个不同的Token, 其中的区别为是否与用户相关联, 即与用户相关的用户Token, 和与客户端相关的客户端Token,
可以通过用户Token, 查询到用户的相关信息, 客户端Token与用户无关, 一般只用于客户端认证

用户Token

获取用户Token一般有两个方式, 授权码模式和密码模式

授权码模式

授权码模式通过跳转到授权中心来获取token

  • 跳转到认证服务器
  • 认证服务器需要用户登录
  • 用户选择是否授权
  • 授权同意后, 自动跳转回原来的页面, 客户端拿到授权码
  • 客户端凭借授权码, 在服务器上通过接口向认证服务器申请令牌

密码模式

密码模式通过接口直接申请到Token

该接口需要几个参数, client_id, client_secret, grant_type, username, password

{
    "client_id": "客户端ID",
    "client_secret": "客户端密码",
    "grant_type": "授权模式, 此外为 password",
    "username": "用户名",
    "password": "用户密码"
}

客户端Token

通过客户端ID和客户端密码来获取Token

该Token与客户端相关, 与用于无关, 只用于客户端认证, 避免了接口泄露和滥用

OAuth2生成Token的过程通常是用户登录成功后,向授权服务器发送授权请求,授权服务器验证用户身份并生成Token,然后将Token返回给客户端。客户端在接下来的请求中使用Token进行身份验证授权。 以下是一个基于Spring Security OAuth2Token生成示例: 1. 添加Maven依赖 ```xml <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.4.0.RELEASE</version> </dependency> ``` 2. 配置OAuth2服务器 ```java @Configuration @EnableAuthorizationServer public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Autowired private DataSource dataSource; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.jdbc(dataSource); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager); } } ``` 3. 配置Spring Security ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/oauth/**").permitAll().anyRequest().authenticated().and().csrf().disable(); } @Override @Bean public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } } ``` 4. 添加授权请求 ```java @Controller @RequestMapping("/oauth2") public class OAuth2Controller { @Autowired private TokenStore tokenStore; @RequestMapping(value = "/token", method = RequestMethod.POST) @ResponseBody public ResponseEntity<?> getToken(@RequestParam("username") String username, @RequestParam("password") String password) { UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); OAuth2Authentication authentication = (OAuth2Authentication) authenticationManager.authenticate(authenticationToken); OAuth2AccessToken token = tokenStore.getAccessToken(authentication); return ResponseEntity.ok(token); } } ``` 以上代码仅供参考,具体实现需要根据实际场景进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少湖说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值