授权框架使得第三方获取对用户的资源的访问
eg:bilibili可以去微信服务器获取你的头像、昵称、openid等
| 术语 | |
| 专业称呼 | 通俗叫法 |
| 资源所有者 | 用户 |
| 资源服务器 | 保存用户信息的服务器并且能验证令牌是否合法 |
| 客户端 | 第三方应用服务 |
| 授权服务器 | 发送令牌到第三方服务器 |
1、授权码模式、
最常用的模式,前后端分离,避免令牌泄露。
隐式模式、密码模式、客户端模式
一、spring security OAuth2
1.1、授权服务器
-
- authorize endpoint:授权端点,进行授权
- token endpoint : 令牌端点,授权拿到对应的token
- introspection endpoint: 校验端点,校验token合法性
- revocat endpoint : 撤销端点,撤销授权
二、实际案例 授权码模式
@Configuration
@EnableWebSecurity //启用security
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()//关闭csrf
.authorizeRequests()
.antMatchers("/oauth
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest()
.authenticated()//所有请求需要认证
.and()
.requestMatchers()
.antMatchers("/user
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager)
.userDetailsService(userService);
}
直接申请token 不许要回调发送授权码在申请token
{
"access_token": "8217fb9e-3e2a-4467-bcfd-541206afca4b",
"token_type": "bearer",
"expires_in": 3599,
"scope": "all"
}
{
"password": null,
"username": "sen",
"authorities": [
{
"authority": "ROLE_sale"
},
{
"authority": "admin"
}
],
"accountNonExpired": true,
"accountNonLocked": true,
"credentialsNonExpired": true,
"enabled": true
}



